0

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

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')当用户输入 as 1 时,循环不会退出insertOutputChoice,无论用户是否输入insertConfirmasy或者n即使它应该在insertConfirm输入 as时退出n

insertOutput看起来如下:

void insertOutput(std::string substrOut)
{
    cout << "\nThe new string generated is:\n";
    cout << substrOut;

    cout << "\n\n1) Generate a log file of this test\n";
    cout << "2) Insert another substring into a string\n\n";
    cout << "0) Finish testing string insertion\n\n\n";

    cout << "Input: ";
} 

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

4

4 回答 4

4

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

于 2013-07-07T15:23:39.737 回答
0

你的代码有很多错误,你确定它编译正确吗?

首先,有2个insertChoice声明

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

在while循环中:

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

编译器应该警告你。If not you must enable warnings with the appropriate options of your compiler ( /W3for cl, -Wall -Wpedanticfor gcc...). 永远不要在没有警告的情况下编译


二、以下字符串有语法错误

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++-0x 中,使用可以嵌入引号或换行符原始字符串文字的生活要容易得多

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: )";
于 2013-09-12T01:14:35.467 回答
0

我认为代码末尾的 if 语句也可能存在问题。您在输入大写和小写时是否尝试过此操作?在检查有效输入时,您将转换为小写进行检查,但在检查响应是否为“n”时不会。

于 2013-07-07T15:32:32.360 回答
0

删除第 9 行:

char insertChoice[2];
于 2013-07-07T16:33:10.317 回答