程序跳过第二个cin [英] Program skips second cin

查看:97
本文介绍了程序跳过第二个cin的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在编写一个C ++ Mind Reader程序,该程序几乎已经完成。但是,感觉有必要跳过第二个cin。我已经搜索过,但不确定是否出问题了。我检查了代码,然后打赌我做了一些愚蠢的事情,但是我仍然对此感到困惑。跳过的cin在第32行,这是我的代码:

  #include< iostream> 
#include< string>
#include< cstdlib>

使用命名空间std;

int main()
{
//声明要在以后使用的变量
字符串名称;
个字符串国家;
int年龄;

//标题低于
cout<< @@@@@@@@@@@@@@-MIND READER-@@@@@@@@@@@@@@@ nnn;

//询问用户是否要继续,否则终止
cout<< 想让您读懂吗?输入y表示是,输入n表示否。 <<恩德尔
cout<< 如果您不选择继续,该程序将终止。 <<恩德尔
字符串exitOrNot;
//接收用户输入的
cin>> exitOrNot;
//处理输入内容是否为y
if(exitOrNot == y){
cout<< 好吧,首先,您需要将您的思维与该程序同步。您将必须回答以下问题才能进行同步。

//询问问题
cout<< 首先,请输入您的全名,并使用正确的大写字母:\n\n;
cin>>名称;

cout<< 现在请输入您现在所在的国家:\n\n;
cin>>国家; //< -----第32行

cout<< 这将是最后一个问题;请提供您的年龄:\n\n;
cin>>年龄;

//要求用户开始同步
cout<< 有足够的信息可以开始同步。输入p开始同步... \n\n;
字符串继续;
cin>>继续;
//检查是否继续进行,并这样做
if(proceed == p){
//提供心智读取结果
cout<< 同步完成。 <<恩德尔
cout<< 您的思想已同步并已阅读。\n\n;
cout<< 但是,由于过多的干扰,您只需要有限的数据即可。 <<恩德尔
cout<< 这是从你心中读到的:\n\n;

//将变量放入句子
cout<< 您的名字是<<名称<< ,您就是<<年龄<<岁。您的所在地是<<国家<< 。 << endl<< \n\n;

cout<< 感谢您使用Mind Reader,祝您愉快。输入e退出。 <<恩德尔
//终止程序,程序
字符串终止;
cin>>终止
if(终止== e){
exit(0);
}

}

}
//如果输入为'n'则终止程序
if(exitOrNot == n ){
exit(0);
}

返回0;
}

编辑:这是我运行它时发生的事情:

  @@@@@@@@@@@@@-MIND READER-@@@@@@@@@@@@@@ 

您想读懂自己的思想吗?输入y表示是,输入n表示否。
如果您不选择继续,该程序将终止。
y
好​​的,首先,您需要将您的想法与该程序同步。您将必须
回答以下问题才能进行同步。

首先,请输入您的全名,并使用正确的大写字母:

John Smith
现在,请输入您当前所在的国家:

这是最后一个问题;请提供您的年龄:

13
有足够的信息可以开始同步。输入p开始同步。
..

p
同步完成。
您的想法已同步并已阅读。

但是,由于干扰太多,仅从您的m
ind中获取了有限的数据。
这是从您的脑海中读取的内容:

您的名字叫约翰,您13岁。您来自史密斯。


感谢您使用Mind Reader,祝您愉快。输入e退出。
e

进程返回0(0x0)执行时间:78.220 s
按任意键继续。

这是一个清晰的屏幕截图: http://puu.sh/4QZb3.png
我不能将其附加在这篇文章中,因为我没有足够的代表。 p>

还值得注意的是它如何使用用户的姓氏作为国家。
我相信问题与输入不是整数有关。



谢谢。

解决方案

问题是您使用的是: cin>> 从用户那里获取字符串。如果用户输入的单词多于1个,则会导致代码中的各行彼此跳过。要解决此问题,我们将使用: getLine(cin,yourStringHere)从用户那里获取字符串。这是所有已修正的代码:

  #include< iostream> 
#include< string>
#include< cstdlib>

使用命名空间std;

int main()
{
字符串名称;
个字符串国家;
int年龄;
cout<< @@@@@@@@@@@@@@-MIND READER-@@@@@@@@@@@@@@@ nnn;

cout<< 想让您读懂吗?输入y表示是,输入n表示否。 <<恩德尔
cout<< 如果您不选择继续,该程序将终止。 <<恩德尔
字符串exitOrNot;
getline(cin,exitOrNot); / *< -----从cin更改为getLine * /
if(exitOrNot == y){
cout<< 好吧,首先,您需要将您的思维与该程序同步。您将必须回答以下问题才能进行同步。

cout<< 首先,请输入您的全名,并使用正确的大写字母:\n\n;
getline(cin,name); / *<-另一个字符串* /

cout<< 现在请输入您现在所在的国家:\n\n;
getline(cin,country); / *<-另一个字符串* /

cout<< 这将是最后一个问题;请提供您的年龄:\n\n;
cin>>年龄;

cout<< 有足够的信息可以开始同步。输入p开始同步... \n\n;
字符串继续;
cin>>继续;
if(proceed == p){
cout<< 同步完成。 <<恩德尔
cout<< 您的思想已同步并已阅读。\n\n;
cout<< 但是,由于过多的干扰,您只需要有限的数据即可。 <<恩德尔
cout<< 这是从你心中读到的:\n\n;

cout<< 您的名字是<<名称<< ,您就是<<年龄<<岁。您的所在地是<<国家<< 。 << endl<< \n\n;

cout<< 感谢您使用Mind Reader,祝您愉快。输入e退出。 <<恩德尔
字符串终止;
cin>>终止
if(终止== e){
exit(0);
}

}

}
if(exitOrNot == n){
exit(0);
}

返回0;
}


I'm making a C++ Mind Reader program, which is nearly complete. However, it feels the need to skip the second cin. I've searched and I'm not exactly sure what's wrong. I've examined the code and I bet I've done something stupid, but I am still baffled about it. The skipped cin is on line 32, here's my code:

#include <iostream>
#include <string>
#include <cstdlib>

using namespace std;

int main()
{
    //declaring variables to be used later
    string name;
    string country;
    int age;

    //header goes below
    cout << "                     @@@@@@@@@@@@-MIND READER-@@@@@@@@@@@@\n\n";

    //asks if the user would like to continue and in not, terminates
    cout << "Would like you to have your mind read? Enter y for yes and n for no." << endl;
    cout << "If you do not choose to proceed, this program will terminate." << endl;
    string exitOrNot;
    //receives user's input
    cin >> exitOrNot;
    //deals with input if it is 'y'
    if (exitOrNot == "y"){
        cout << "Okay, first you will need to sync your mind with this program. You will have to answer the following questions to synchronise.\n\n";

        //asks questions
        cout << "Firstly, please enter your full name, with correct capitalisation:\n\n";
        cin >> name;

        cout << "Now please enter the country you are in at the moment:\n\n";
        cin >> country; //<------ Line 32

        cout << "This will be the final question; please provide your age:\n\n";
        cin >> age;

        //asks the user to start the sync
        cout << "There is enough information to start synchronisation. Enter p to start the sync...\n\n";
        string proceed;
        cin >> proceed;
        //checks to see if to proceed and does so
        if (proceed == "p"){
            //provides results of mind read
            cout << "Sync complete." << endl;
            cout << "Your mind has been synced and read.\n\n";
            cout << "However, due to too much interference, only limited data was aquired from your mind." << endl;
            cout << "Here is what was read from your mind:\n\n";

            //puts variables in sentence
            cout << "Your name is " << name << " and you are " << age << " years old. You are based in " << country << "." << endl << "\n\n";

            cout << "Thanks for using Mind Reader, have a nice day. Enter e to exit." << endl;
            //terminates the program the program
            string terminate;
            cin >> terminate;
            if (terminate == "e"){
                exit(0);
            }

        }

    }
    //terminates the program if the input is 'n'
    if (exitOrNot == "n"){
        exit(0);
    }

    return 0;
}

EDIT: Here is what happens when I run it:

                     @@@@@@@@@@@@-MIND READER-@@@@@@@@@@@@

Would like you to have your mind read? Enter y for yes and n for no.
If you do not choose to proceed, this program will terminate.
y
Okay, first you will need to sync your mind with this program. You will have to
answer the following questions to synchronise.

Firstly, please enter your full name, with correct capitalisation:

John Smith
Now please enter the country you are in at the moment:

This will be the final question; please provide your age:

13
There is enough information to start synchronisation. Enter p to start the sync.
..

p
Sync complete.
Your mind has been synced and read.

However, due to too much interference, only limited data was aquired from your m
ind.
Here is what was read from your mind:

Your name is John and you are 13 years old. You are based in Smith.


Thanks for using Mind Reader, have a nice day. Enter e to exit.
e

Process returned 0 (0x0)   execution time : 78.220 s
Press any key to continue.

And here is a screenshot to make it clearer: http://puu.sh/4QZb3.png I can't attach it in this post cos I don't enough rep.

It's also worth noting how it uses the user's last name as a country. I believe the problem is something to do with the input not being an integer.

Thanks.

解决方案

The problem was that you were using: cin >> to get a string from the user. If the user inputs more than 1 word, it will cause lines in your code to skip over one another. To solve this problem, we would use: getLine(cin,yourStringHere) to get a string from the user. Here is your code all fixed up:

#include <iostream>
#include <string>
#include <cstdlib>

using namespace std;

int main()
{
    string name;
    string country;
    int age;
    cout << "                     @@@@@@@@@@@@-MIND READER-@@@@@@@@@@@@\n\n";

    cout << "Would like you to have your mind read? Enter y for yes and n for no." << endl;
    cout << "If you do not choose to proceed, this program will terminate." << endl;
    string exitOrNot;
    getline (cin,exitOrNot); /*<-----Changed from cin to getLine*/
    if (exitOrNot == "y"){
        cout << "Okay, first you will need to sync your mind with this program. You will have to answer the following questions to synchronise.\n\n";

        cout << "Firstly, please enter your full name, with correct capitalisation:\n\n";
        getline(cin,name); /*<--Another string*/

        cout << "Now please enter the country you are in at the moment:\n\n";
        getline(cin,country); /*<--Another string*/

        cout << "This will be the final question; please provide your age:\n\n";
        cin >> age;

        cout << "There is enough information to start synchronisation. Enter p to start the sync...\n\n";
        string proceed;
        cin >> proceed;
        if (proceed == "p"){
            cout << "Sync complete." << endl;
            cout << "Your mind has been synced and read.\n\n";
            cout << "However, due to too much interference, only limited data was aquired from your mind." << endl;
            cout << "Here is what was read from your mind:\n\n";

            cout << "Your name is " << name << " and you are " << age << " years old. You are based in " << country << "." << endl << "\n\n";

            cout << "Thanks for using Mind Reader, have a nice day. Enter e to exit." << endl;
            string terminate;
            cin >> terminate;
            if (terminate == "e"){
                exit(0);
            }

        }

    }
    if (exitOrNot == "n"){
        exit(0);
    }

    return 0;
}

这篇关于程序跳过第二个cin的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆