试图让程序退出时,“退出”是键入. C++

icomxhvb  于 2023-03-20  发布在  其他
关注(0)|答案(1)|浏览(129)

我目前正试图让我的程序退出时,“退出”是打出来,但它不会工作,什么也没有发生。有时它终止它。我是超级新的所有编码,所以任何帮助是赞赏。这是我写的:
下面是代码第一部分的阅读行引用的内容。我在编写这些内容时得到了帮助,现在没有了帮助,我很难理解。我想这是学习的时刻。

int main(){
    int length = 1024;
    char *mainInput = new char[length];
    Start(mainInput, length - 1);
    delete []mainInput;
    return 0;
}

void Start(char* &input, int length){
    char message[] = "Type quit to exit program. Type any other line to begin: ";
    ReadLine(input, length, message);
    if(strstr(input, "quit")){
        cout << "Terminating program." << endl;
        exit(0);
    }
    cout << "Youre string is: " << input << endl;
    int option;
    cout << "Choose option: (Vowels_Vs_Consonants (1), Letter_Swap (2), Flip_String (3), Palindrone_Detector(4), Words_Frequency(5)): ";
    cin >> option;
    switch(option){
        case 1:
            vowels_vs_consonants(input);
            break;
        case 2:
            letter_swap(input);
            cout << "Our new string: " << input << endl;
            break;
        case 3:
            reverse_string(input);
            cout << "Original: " << input << endl;
            break;
        case 4:
            if(is_palindrome(input)){
                cout << "It is a palindrome." << endl;
            }
            else{
                cout << "It is not a palindrome." << endl;
            }
            break;
        case 5:
            word_frequency(input);
            break;
        default:
            Start(input, length);
            break;
    }
    cin.clear();
    Start(input, length);
}


Added code for understanding:

void ReadLine(char* &charArray, int length, char* message){
    cin.clear();
    cout << message; cin.getline(charArray, length);
    if(cin.fail()){
        cin.ignore(length,'\n');
        cin.clear();
        ReadLine(charArray, length, message);
    }
    cin.clear();
    cin.ignore();
}

I've tried messing around with the capitalization factor but I can't get it to compile.
yqlxgs2m

yqlxgs2m1#

#include <iostream>

void ReadLine(char*& input, int length, char* message) {
    std::cout << message;
    //std::cin >> input; // this line does not work if you want the cstring to have space. Example :"asd 123" would only return "asd"
    std::cin.getline(input, length);
    //std::cin.getline(input, length, '\n');
}

// I did not change anything below, so I assume the problem is in your ReadLine()
void Start(char*& input, int length) {
    char message[] = "Type quit to exit program. Type any other line to begin: ";
    ReadLine(input, length, message);
    if (strstr(input, "quit")) {
        std::cout << "Terminating program." << std::endl;
        exit(0);
    }
    std::cout << "Youre string is: " << input << std::endl;
    std::cin.clear();
    Start(input, length);
}

int main() {
    int length = 1024;
    char* mainInput = new char[length];
    Start(mainInput, length - 1);
    delete[]mainInput;
    return 0;
}

我认为问题出在你的ReadLine()函数中。因为我不知道你的ReadLine()包含什么,我假设其他评论者说的是这种情况(你也阅读了换行符)。上面的解决方案是有效的,但我不知道这是否是你的问题。
我能想到的另一个解决方案是:
1.修改“input”变量,去掉“strstr”之前结尾处的换行符。
1.正在添加cin.忽略('\n');某处
(Note:我复制了你的代码,并删除了一些,以使我的解决方案更具可读性。我是一名学生,所以我的代码可能不是最合理/优化的太)
希望能有所帮助!

相关问题