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';
    }
}

ただし、ユーザーが as と入力したかどうかに関係なく、ユーザーが as と入力したときにループが終了しませwhile (insertChoice[0] != '0')ん。insertOutputChoiceinsertConfirmyninsertConfirmn

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

9 行目を削除します。

char insertChoice[2];
于 2013-07-07T16:33:10.317 に答える
0

コードに多くのエラーがあります。正しくコンパイルされていますか?

まず、2つのinsertChoice宣言があります

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

while ループでは次のようになります。

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

コンパイラはそれについて警告するはずです。そうでない場合は、コンパイラの適切なオプションで警告を有効にする必要があります ( /W3cl の場合-Wall -Wpedantic、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: ";

連続したリテラルは、コンパイラによって自動的に 1 つに結合されます

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 に答える