C ++后无法输入C>>回答;

时间:2017-04-26 17:08:48

标签: c++ cin

我正在写一个基本的程序,类似于ELIZA(在线治疗师)简单的问题和答案,但我最后都被困住了。在cin>>之后回答;我无法写任何东西。

int main () {
    short number;
    string color;
    string sport;
    int answer;
    string travel;


    // Greets user
    cout << "Hello, I'm Samantha" << endl;
    // Asks user for their favorite sport
    cout << "What's your favorite sport?";
    cin >> sport;
    cout << "I like " << sport << " too!" << endl;
    cout << "How about your favorite color?";
    cin >> color;
    cout << "Not my favorite color but it's nice!" << endl;
    cout << "Tell me something you've never told anyone before";
    cin >> answer;
    cout << "Don't worry, your secret is safe with me!" << endl;
    cout << "Hows your life going?";
    cin >> answer;

return 0;
}

1 个答案:

答案 0 :(得分:2)

您的变量名为&#34;回答&#34;有一个数据类型整数。当您第一次提示用户从控制台输入内容时(显然是一个字符串),cin对象会尝试初始化&#34;回答&#34;用一个字符串(可能是因为提示不要求一个数字)杀死cin对象,这将不允许对象接受指令......所以下次你想要使用它时没有cin对象。

只需更改&#34; answer&#34;的数据类型串起来。

Dr t