在循环中与开关语句有问题

Having an issue with a switch statement inside a loop

本文关键字:语句 有问题 开关 循环      更新时间:2023-10-16

,因此此代码只是我一直在搞砸的东西,但我似乎无法使其正常工作。切换语句完成第一种情况后不会继续循环。我只是想让它本质上用单词更改字母。我不太确定,如何修复它。任何帮助,将不胜感激。

这一直告诉我我需要更多单词,所以我只是随机写作,请不要介意这些单词,他们只是占位符,使系统不太生气,以将我的问题总结为三个句子。

#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。