而循环搞乱了

son*_*ool 0 c++ while-loop

我正在尝试编写一个while循环,当我选择actres或电影标题时,它存在while循环.但由于某种原因,它不能告诉我,行中有错误while (selecton).有人有想法吗?

string selection;

while ( selection )
{
    cin >> selection;

    if ( selection == "actress" )
    {
        cout << "hey" << endl;
        break;
    }
    else if ( selection == "movie title" )
    {
        cout << "bye";
        break;
    }
    else
    {
        cout << "Please choose a selection";
    }
}
Run Code Online (Sandbox Code Playgroud)

Rap*_*ptz 6

它应该是 while(cin>>selection)

但是最好这样写,while(getline(cin,selection))所以你不要跳过白线.