1. ホーム
  2. c++

[解決済み] プログラムは2番目のCINをスキップする

2022-02-05 03:53:44

質問事項

C++でマインドリーダーのプログラムを作っているのですが、ほぼ完成しています。しかし、2番目のcinをスキップする必要があると感じています。検索しても、何が悪いのかよくわかりません。コードを調べてみたのですが、きっと何かバカなことをしたのでしょう、でもまだ困惑しています。スキップされたcinは32行目にあり、以下は私のコードです。

#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;
}

編集部:実行するとこんな感じです。

                     @@@@@@@@@@@@-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.

そして、わかりやすくするためにスクリーンショットを掲載します。 http://puu.sh/4QZb3.png この投稿に添付できないのは、リプが足りないからです。

また、ユーザーの姓を国名として使用している点にも注目です。 入力が整数でないことが問題なのだと思います。

ありがとうございます。

解決方法は?

使っていたのが問題だったのですね。 cin >> を使用してユーザーから文字列を取得します。もしユーザーが複数の単語を入力した場合、コード内の行が互いにスキップされることになります。この問題を解決するには、次のようにします。 getLine(cin,yourStringHere) で文字列を取得します。以下は、あなたのコードをすべて修正したものです。

#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;
}