为什么这个while循环退出?

时间:2013-07-07 15:18:27

标签: c++ loops while-loop

我写了一个控制台程序来帮助我测试我编码的函数库。部分原因是这段代码:

char insertChoice[2] = {'9'};

while (insertChoice[0] != '0')
{
    cout << "\nEnter a string:\n";
    char insertStringInput[256];
    cin.getline(insertStringInput, 255);

    char insertChoice[2];
    insertChoice[0] = '9';

    cout << "\nWhere would you like to insert the substring?\n\n
             1) At the beginning of the string\n
             2) At the end of the string\n\nInput: ";
    cin >> insertChoice;
    cin.ignore();

    while (insertChoice[0] != '1' && insertChoice[0] != '2')
    {
        cout << "\nInvalid input.\nWhere would you like to insert the substring?\n\n
                 1) At the beginning of the string\n
                 2) At the end of the string\n\nInput: ";
        cin >> insertChoice;
        cin.ignore();
    }

    cout << "\nEnter the substring you would like to insert: ";
    char insertSubstring[256];
    cin.getline(insertSubstring, 255);

    std::string used = "", substr = "";
    used += insertStringInput;
    substr += insertSubstring;

    char insertOutputChoice[2];
    insertOutputChoice[0] = '1';

    if (insertChoice[0] == '1')
        insertOutput(insertInBeginning(used, substr));
    else
        insertOutput(insertInEnd(used, substr));

    cin >> insertOutputChoice;
    cin.ignore();

    if (insertOutputChoice[0] == '1')
    {
        ofstream outfile("logfile.txt", ios::app);

        outfile << "Test type: Insert Substring\n";
        outfile << "Test carried out on: " << __DATE__ << "; " << __TIME__ <<"\n";
        outfile << "PARAMETERS:\n";
        outfile << "usedString: \"" << insertStringInput << "\"\n";
        outfile << "insertString: \"" << insertSubstring << "\"\n";
        outfile << "function used: " 
                << (insertChoice[0]=='1'?"insertInBeginning":"insertInEnd") 
                << "\nOUTPUT:\n";
        outfile << "\"" 
                << (insertChoice[0]=='1'?insertInBeginning(used, substr):insertInEnd(used, substr)) 
                << "\"\n\n";

        outfile.close();

        cout << "\nWould you like to do another string insertion test? [y/n]: ";
        char insertConfirm[2];
        insertConfirm[0] = ' ';

        while (tolower(insertConfirm[0]) != 'y' 
               && tolower(insertConfirm[0] != 'n'))
        {
            cin >> insertConfirm;
            cin.ignore();
            if (tolower(insertConfirm[0]) != 'y' 
                && tolower(insertConfirm[0] != 'n'))
                cout << "\nInvalid input. 
                         Would you like to do another string insertion test? [y/n]: ";
        }

        if (insertConfirm[0] == 'n')
            insertChoice[0] = '0';
    }
}

但是,当用户在while (insertChoice[0] != '0')中输入为1时,insertOutputChoice循环不会退出,无论用户是在insertConfirm中输入y还是{{1}即使在n输入insertConfirm时它应该退出。

n看起来如下:

insertOutput

请原谅凌乱,未经优化的代码。我的首要任务是完成这项工作,我通常会将优化留到最后。

4 个答案:

答案 0 :(得分:3)

在while循环中创建一个新的insertChoice数组。它隐藏了外部的一个。因此,当您修改此数组中的值时,外部值仍未修改。

答案 1 :(得分:0)

我认为代码末尾的if语句可能存在问题。你是在大写和小写的同时输入的吗?在检查有效输入时,您将转换为小写以进行检查,但在检查响应是否为“n”时则不会。

答案 2 :(得分:0)

删除第9行:

char insertChoice[2];

答案 3 :(得分:0)

您的代码中存在许多错误,您确定它是否正确编译?

首先,有2个insertChoice声明

char insertChoice[2] = {'9'};    // <------- 1st

并在while循环中:

    cout << "\nEnter a string:\n";
    char insertStringInput[256];
    cin.getline(insertStringInput, 255);

    char insertChoice[2];        // <------- 2nd
    insertChoice[0] = '9';

其次,以下字符串有语法错误

cout << "\nWhere would you like to insert the substring?\n\n
         1) At the beginning of the string\n
         2) At the end of the string\n\nInput: ";

如果你想在字符串中使用换行符,你必须使用之前的转义符或用双引号括起字符串,如下所示

cout << "\nWhere would you like to insert the substring?\n\n\
1) At the beginning of the string\n\
2) At the end of the string\n\nInput: ";

cout << "\nWhere would you like to insert the substring?\n\n"
        "1) At the beginning of the string\n"
        "2) At the end of the string\n\nInput: ";

在C ++中 - 使用原始字符串文字有时更容易生活

cout << R"(
Where would you like to insert the substring?


1) At the beginning of the string
2) At the end of the string

Input: )";