4

まず第一に、私を助けてくれたすべての人に感謝します!

スペースと特殊文字をそのまま使用した文字列をに格納しようとしていますMessageToAdd

私は使用getline (cin,MessageToAdd);していて、私も試しcin >> MessageToAdd;ました。

私はとても困惑しています!サンプル入力を入力すると

テスト

すべてが意図したとおりに機能します。しかし、私が使用する場合

テストテストテスト

を押すまで、コンソール全体が速く点滅しCtrlCます。

変数を一番上に置くという私のスタイルは時代遅れです。私はまだ自分自身を教えているので、私を許してください、そしてそれは単に習慣の力です。私はこれを解決した後すぐに私のスタイルを変更します:)

void AddMessage() {
    ifstream myReadFile;
    string str;
    string MessageToAdd;
    string myMessages[10];
    int i; // of course my famous i
    static string rowHtmlCloseTags;
    static string rowHtmlOpenTags;
    string replacement;

    myReadFile.open("C:\\Users\\Andrews\\Documents\\Visual Studio 2010\\Projects\\computerclass\\Debug\\outages.htm",ios::in);
    i = 0; //the start of my array
    rowHtmlCloseTags = "</b></td>"; // value that I want to replace with nothing
    rowHtmlOpenTags = "<td><b>";

    if(!myReadFile) // is there any error?
    {
        cout << "Error opening the file! Aborting…\n";
        exit(1);
    }

    if (myReadFile.is_open())
    {
        cout << endl;

        while (!myReadFile.eof())
        {
            getline(myReadFile, str);

            if (str == "<tr>")
            {            
                getline(myReadFile, str); //get the next line cause thats where the <td><b>Outage Message</b></td> is.
                size_t foundIndex = str.find(rowHtmlCloseTags); //does the sought string exist in this this line?
                if (foundIndex != str.npos) //if not no position
                    str.replace(foundIndex, rowHtmlCloseTags.size(), replacement); //replace the string
                else
                    std::cout << "Oops.. didn't find " << rowHtmlCloseTags << std::endl; //else throw a bitch

                foundIndex = str.find(rowHtmlOpenTags); //does the sought string exist in this this line?
                if (foundIndex != str.npos) //if not no position
                    str.replace(foundIndex, rowHtmlOpenTags.size(), replacement); //replace the string
                else
                    std::cout << "Oops.. didn't find " << rowHtmlOpenTags << std::endl; //else throw a bitch

                myMessages[i]=str;
                i++;
            }
        }
    }
    system("cls");
    i=0;
    while (i < 10)
    {
        cout << i << ") " << myMessages[i] << endl;
        i++;
        if (myMessages[i]=="")
        {
            break;
        }
    }
    myReadFile.close();
    cout << endl;
    cout << endl;
    cout << "Enter the message you would like to see on the reader board.\n";
    cout << "Or enter 911 to go back to the main menu: ";
    cin.ignore(1080);
    getline (cin,MessageToAdd);

    if (str == "911") //go back to the main menu
    {
        system("cls");
        mainMenu();
    }
    else //insert the message into a blank spot in the array
    {
        i=0;
        while (i < 10)
        {
            if (myMessages[i].empty())
            {
                myMessages[i]=MessageToAdd;
                break;
            }
            else
            {
                i++;
            }
        }
    }

    //now rebuild the htm file with the new array
    CreateHtmlFile(myMessages);
}
4

1 に答える 1

8

私はあなたのコードですぐに間違っている1つのことをあなたに話します、あなたの特定の問題ではなく、それにもかかわらず毛深い問題です。

あなたのmainMenu()関数がこれを呼び出していると思います。その場合、あなたは次のような誤解を受けているように見えます。

if (str == "911") //go back to the main menu
{
       system("cls");
       mainMenu();
}

メニューに戻ります。それはしません。メインメニューコードを新たに呼び出すと、最終的にスタックスペースが不足します。

私はあなたがしなければならないことはループを持っていることでmainMenu()あり、上記のコードは再帰的return;に呼び出すのではなく、単に使用するべきだと思います。mainMenu()

それと、ではなく比較MessageToAddする必要があると私が思うという事実。"911"str


私がするもう1つのことは、一時的なデバッグコードを次の場所に配置することです。

cout << "DEBUG A\n";
i=0;
while (i < 10)
{
    cout << "DEBUG B " << i << "\n";
    if (myMessages[i].empty())
    {
        cout << "DEBUG C\n";
        myMessages[i]=MessageToAdd;
        break;
    }
    else
    {
        i++;
        cout << "DEBUG D " << i << "\n";
    }
    cout << "DEBUG E\n";
}
cout << "DEBUG F\n";

何が印刷されるかを確認します。もちろん、デバッガーで実行をトレースすることもできますが、その場合は自分で作業を行う必要があります。出力を投稿するだけで(巨大な場合は最初の100行)、何が問題なのかを簡単に知ることができます。


実は、あなたの問題はcin.ignore。私があなたのコードを実行するとき、何も機能しませTestTest Test Test。これは、入力しようとしている最初の1080文字を無視しているためです。これらのステートメントを次のように変更すると、証明が表示されます。

cin.ignore(1);
getline (cin,MessageToAdd);
cout << MessageToAdd << "\n";

estを入力すると出力が得られますtest

行を取り出して、ignore再試行してください。あなたがそれがうまくいくことを示しているように見えるので、私はこれを確信していませんTestが、私はこれが正しいとは思えません。


したがって、これがあなたがする必要があることです(最低限):

  • cin.ignore完全に取り除く。
  • returnではなく使用しますmainMenu()
  • if (MessageToAdd == "911")の代わりに使用してif (str == "911")ください。
  • それではどうなるか教えてください。
于 2010-06-30T03:55:30.240 に答える