C++ getline或cin不接受带空格的字符串,我搜索了谷歌,我仍然难过!

Cod*_*ome 4 c++ string input getline

首先,感谢所有帮助我的人,非常感谢!

我试图存储一个字符串,其中包含空格和特殊字符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);
}
Run Code Online (Sandbox Code Playgroud)

pax*_*blo 8

我会告诉你一件事你的代码是错的,不是你的具体问题,而是一个毛茸茸的问题.

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

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

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

我怀疑你应该做的是有一个循环,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";
Run Code Online (Sandbox Code Playgroud)

并看看打印的内容.当然,您可以在调试器中跟踪执行,但这需要您自己完成工作.如果你只是发布输出(如果它是巨大的前100行),那么我们可以很容易地告诉你什么是错的.


实际上,我认为你的问题是cin.ignore.当我运行你的代码时,没有任何作用,也Test没有Test Test Test.那是因为它忽略了我想输入的前1080个字符.将这些语句更改为时,可以看到证据:

cin.ignore(1);
getline (cin,MessageToAdd);
cout << MessageToAdd << "\n";
Run Code Online (Sandbox Code Playgroud)

est当你进入时你会得到输出test.

拿出ignore线,再试一次.我不确定这个,因为你似乎表明它Test有效,但我不能认为这是正确的.


所以这就是你需要做的事情(至少):

  • cin.ignore完全摆脱了.
  • 使用return而不是mainMenu().
  • if (MessageToAdd == "911")而不是if (str == "911").
  • 让我们知道它是怎么回事.