而循环搞乱了

时间:2012-09-08 02:25:05

标签: c++ while-loop

当我选择actres或电影标题时,我正在尝试编写一个存在while循环的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";
    }
}

1 个答案:

答案 0 :(得分:6)

应该是while(cin>>selection)

但是最好写while(getline(cin,selection)),这样就不会跳过白线。