为什么不会串cout

时间:2014-04-17 18:41:54

标签: visual-c++

O.F社区!我对c ++很新,我决定创建自己的游戏。

我的问题是,在游戏中的某个时刻,玩家被要求回答3件事:

-Gender -age

- 设置

void assessment()
{
    cout << "Now that the characteristics have been selected. Let's review what they are:\n\n";
    cout << "Press any key to continue\n\n";
    cin >> anykey;
    cout << "You are a " << ageNumber << " year old " << gender; //incomplete - needs   //<<  setting;
}

问题在于没有展示性别。它可以cout&lt;&lt; ageNumber很好。但是当玩游戏并且达到这个部分时,这就是它所说的:

现在已经选择了特征。我们来看看它们是什么:

按任意键继续

1 你是16岁(这是我之前为ageNumber提供的)岁。 &lt; - 注意“旧”和“。”有空间吗?好像字符串性别被读为空白。

编辑:应该说:

你是一名16岁的男/女/。

这是决定性别的部分。

void fate()
{
    cout << "Before we begin, a few characteristics must be selected\n\n";
    cout << "Gender?\n\n";
    cout << "1. Male\n";
    cout << "2. Female\n";
    cout << "3. Other\n\n";
    cin >> genderNumber;
    cout << "Now your next characteristic...Press any key to continue\n\n";
    cin >> anykey;
    cout << "What is your age? (Must be younger than 50 to play)\n\n";
    cin >> ageNumber;
    setting2();
if (genderNumber == 1)
{
    string gender = "male";
}
else if (genderNumber == 2)
{
    string gender = "female";
}
else if (genderNumber == 3)
{
    string gender = "'it'";
}
    else
    {
        "invalid input\n\n\n\n\n\n";
        fate();
    }

=============================================== ============================================ 这是完整的代码:

#include <iostream>
#include <string>

using namespace std;

void rules();
void controls();
void start();
void fate();
void assessment();
void setting2();

int fateNumber;
int anykey;
int ageNumber;
int settingNumber;
int genderNumber;
string gender;
string setting;

int main(int argc, char** argv)
{
    cout << "===W  E  L  C  O  M  E   T  O    C  H  O  O  S  E    Y  O  U  R    F  A  T  E===     \n\n";
    cout << "Press any key to continue\n";
    cin >> anykey;
    start();

}
void controls()
{
    cout << "Throughout this game will you be making choices, each choice will have a        corresponding number next to it. Press the letter to the corresponding choice   you want to make. Press any key to continue\n";
    cin >> anykey;
    cout << "Here is an example...you are a cat, you can either\n\n";
    cout << "1. Meow\n\n";
    cout << "2. Hiss\n";
    cout << "Press any key to continue\n";
    cin >> anykey;
    cout << "If I wanted to Hiss I would press the 2 key\n\n";
    cout << "Press any key to continue on to the rules\n\n";
    cin >> anykey;
    rules();
}
void rules()
{

    cout << "#1 You will be given a set number of choices. Press any key to continue\n\n";
    cin >> anykey;
    cout << "#2: There are 2 factors to this game. Press any key to continue\n\n";
    cin >> anykey;
    cout << "#3: The first factor is that either you live or you die. Press any key to       continue\n\n";
    cin >> anykey;
    cout << "#4: The second factor is that if you live, your character will be judged based      on events you've made. You can still live, but you might have a good or bad effect on the people around you. Press any key to continue\n\n";
    cin >> anykey;
    cout << "Let's play shall we?";
}
void start()
{
    cout << "You shall begin a journey that'll decide either you make it or you don't. Press any key to continue\n\n";
    cin >> fateNumber;
    cout << "But before we start...The game needs to be explained.\n\n\n\n";
    cout << "Controls(1 key)---------------------------Rules(2 key)---------------Skip(3 key)                   \n\n";
    cin >> fateNumber;

    if (fateNumber == 1)
    {
        controls();
    }
    else if (fateNumber == 2)
    {
        rules();
    }
    else if (fateNumber == 3)
    {
        fate();
    }
    else
    {
        cout << "invalid key\n\n\n\n\n\n\n";
        start();
    }
}
void fate()
{
    cout << "Before we begin, a few characteristics must be selected\n\n";
    cout << "Gender?\n\n";
    cout << "1. Male\n";
    cout << "2. Female\n";
    cout << "3. Other\n\n";
    cin >> genderNumber;
    cout << "Now your next characteristic...Press any key to continue\n\n";
    cin >> anykey;
    cout << "What is your age? (Must be younger than 50 to play)\n\n";
    cin >> ageNumber;
    setting2();

    if (genderNumber == 1)
    {
        string gender = "male";
    }
    else if (genderNumber == 2)
    {
        string gender = "female";
    }
    else if (genderNumber == 3)
    {
        string gender = "'it'";
    }
    else
    {
        "invalid input\n\n\n\n\n\n";
        fate();
    }
    if (ageNumber >= 50 && ageNumber < 100)
    {
        cout << "Didn't you read the age rating dumbass? Younger than 50...\n\n";
    }
    else if (ageNumber < 50 && ageNumber > 22)
    {
        setting2();
    }
    else if (ageNumber <= 22 && ageNumber >= 16)
    {
        setting2();
    }
    else if (ageNumber < 16 && ageNumber >= 0)
    {
        setting2();
    }
    else if (ageNumber >= 100)
    {
        cout << "More than a century old and you expect me to believe you got this far? Bullshit.";
    }
    else
    {
        cout << "Error - Invalid number\n\n\n\n\n\n";
        cout << "what is your age again?\n\n";
        cin >> ageNumber;
    }
}
void assessment()
{
    cout << "Now that the characteristics have been selected. Let's review what they are:\n\n";
    cout << "Press any key to continue\n\n";
    cin >> anykey;
    cout << "You are a " << ageNumber << " year old " << gender << ". You are " << setting << endl << endl;


    if (settingNumber == 1)
    {
        string setting = "home";
    }
    else if (settingNumber == 2)
    {
        string setting = "jail";
    }

}
void setting2()
{
    cout << "Alright and lastly pick your setting\n\n";
    cout << "1. Home sweet home\n";
    cout << "2. Vacation \n";
    cout << "3. Jail\n\n";
    cin >> settingNumber;
    assessment();
}

=============================================== ======================================== 任何帮助表示赞赏^^代码!

1 个答案:

答案 0 :(得分:0)

buffered中的输出(通过ofstream - 类似于coutC++。你想要

cout << flush;

cout << endl;

要求刷新缓冲区(endl manipulator也会发出新行但flush没有)。

所以代码:

   cout << "You are a " << ageNumber << " year old " << gender << flush;

   cout << "You are a " << ageNumber << " year old " << gender << endl;

此外,您的gender应在更广泛的范围内宣布:

if (genderNumber == 1)    {
    string gender = "male";
}

应成为(引用全球gender

if (genderNumber == 1)    {
    gender = "male";
}

BTW,如果您使用编译器启用了所有警告(例如使用g++ -g -Wall -Wextra编译,如果使用GCC,例如在Linux上),您应该收到警告。当然,您应该使用调试器(例如gdb,至少在Linux上)并使用它进入您的代码。