在循环中与开关语句有问题
Having an issue with a switch statement inside a loop
,因此此代码只是我一直在搞砸的东西,但我似乎无法使其正常工作。切换语句完成第一种情况后不会继续循环。我只是想让它本质上用单词更改字母。我不太确定,如何修复它。任何帮助,将不胜感激。
这一直告诉我我需要更多单词,所以我只是随机写作,请不要介意这些单词,他们只是占位符,使系统不太生气,以将我的问题总结为三个句子。
#include <iostream>
#include <cstdlib>
#include <string>
#include <cstring>
#include <fstream>
#include <cmath>
using namespace std;
int main()
{
char ans;
char fileName[50];
string inp;
int length;
ifstream inpFile;
ofstream outFile("output.txt");
do{
cout << "";
cin.getline(fileName, 50);
inpFile.open(fileName);
if(!inpFile.is_open()){
exit(EXIT_FAILURE);
}
string word;
char enc;
char temp;
length = word.length();
char * cstr = new char [length + 1];
strcpy(cstr, word.c_str());
for(int i = 0; i < length; i++){
switch(cstr[i]){
case 'A':{
temp = cstr[i];
temp = '0';
enc = temp;
break;
}
case 'a':{
temp = cstr[i];
temp = '1';
enc = temp;
break;
}
}
cout << "test1";
}
cout << "Test2";
delete[] cstr;
}while(ans == 'Y' || ans == 'y');
cout << "Test3";
}
您打开文件,但是您从未从中读取任何内容。您的变量单词将始终为空,因此长度为0。
相关文章:
- 我似乎对if/else的基本语句有问题:/
- 我在范围内未声明的错误类有问题
- 为什么布尔开关语句有编译器警告?
- 我的C++语言蛮力算法有问题
- 我关于函数"Assert"的C++代码有问题
- 标准对此指向成员函数类型模板参数有何说明?是我的代码有误,还是 MSVS 16.6 有问题?
- C++类中的友元函数有问题?
- C++ 参考:这两个语句有什么区别?
- 我对数组即 0x6dfe78 有问题
- 我对 std::unique(算法)C++有问题
- 对于单个数据 WORD 的 memcpy 和赋值语句有什么区别?
- 使用 fstream 库并在屏幕上打印的文件有问题?
- 这个模板为什么有问题?如何正确编译
- 我对 MyGraph 属性顶点名称和边权重有问题
- delete[]有问题,如何部分删除内存
- 在C++中使用exit()退出程序有问题吗
- 跳转到if(false)块有问题吗
- 查找有问题的宏
- 在循环中与开关语句有问题
- 这个IF语句有什么问题吗?(C++)