在C ++中为用户输入分配变量

时间:2015-02-06 22:59:10

标签: c++

这是我目前正在使用C ++编写的代码。你可以说它是一个选择你自己的冒险游戏。我试图弄清楚如何通过用户输入创建两个不同的路径。对代码的任何帮助或调整都会有所帮助。代码

#include <iostream>
#include <string>

using namespace std;

string name;
int gender;

int main()
{

//Name prompt for future reference
    cout << "Hello. I want to play a game. You'll be going on a";
    cout << std::endl;
    cout << "wild ride with it's own ups and downs.";
    cout << std::endl;
    cout << "To start out what is your name?";
    cout << std::endl;
   cin >> name;

//Determining Which Gender the player identifies with
    cout << "Perfect " <<name;
    cout << ". We shall ask a few more questions to determine";
    cout << std::endl;
    cout << "path will be best suited for you.";
    cout << std::endl;
    cout << "What is your gender? If Male enter 1 If Female enter 2";
    cout << std::endl;
    cin >> gender;
    cout << std::endl;



//Now we shall start the path determined by the chosen gender
    if(gender == 1) 
    cout << "Fantastic! You're a male so that means you are very simple. With that being said let us get started.";
    else
    cout << "Hmm... This seems to be a little difficult our systems indicate that putting you into situations seems a little bit more... complex. Never the matter. Let us get started!";
    cout << std::endl;

return 0;

}

1 个答案:

答案 0 :(得分:-4)

您必须在if语句周围使用括号({})。

<强> WRONG:

if(gender == 1) 
    cout << "Fantastic! You're a male so that means you are very simple. With that being said let us get started.";
    else
    cout << "Hmm... This seems to be a little difficult our systems indicate that putting you into situations seems a little bit more... complex. Never the matter. Let us get started!";
    cout << std::endl;


从右

if(gender == 1) 
{
    cout << "Fantastic! You're a male so that means you are very simple. With that being said let us get started.";
}
else
{
    cout << "Hmm... This seems to be a little difficult our systems indicate that putting you into situations seems a little bit more... complex. Never the matter. Let us get started!";
}
cout << std::endl;