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

我会告诉你一件你的代码立即出错的事情,不是你的具体问题,而是一个毛茸茸的问题。

我假设您的mainMenu()函数正在调用这个函数。在这种情况下,您似乎误解了:

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

将返回您的菜单。它不会那样做。它将做的是重新调用您的主菜单代码,最终您将耗尽堆栈空间。

我怀疑你应该做的是有一个循环,mainMenu()上面的代码应该只使用return;而不是mainMenu()递归调用。

那和我认为你应该比较MessageToAdd"911"不是比较的事实str


我要做的另一件事是将一些临时调试代码放入:

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. 当我运行您的代码时,没有任何效果,也Test没有Test 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 回答