C ++ - 为'if'语句使用特定字符输入

时间:2014-09-26 21:30:20

标签: c++ key

由于缺乏一个更好的词,我是绝对的编程新手。就目前而言,我已经先用两三种语言进行编程。

作为任务的一部分,我们的任务是以随机机会游戏的形式实质上重新创建头/尾翻转。 它的工作方式是玩家有10个学分开始。他们被要求输入赌注,然后他们被问到头或尾。挑选头部或尾部会引发一个数字生成器,并且根据价值,它们最终可能会赢得或失去他们的赌注。

我使用HTML / CSS / JS编写了类似版本,并且它可以正常工作。我会留下一个链接到该文件,因此您可以自己查看它以了解我尝试做什么:http://puu.sh/bP2V7/2ef63f4a1c.html

我试图在功能上以命令应用程序的形式在C ++中做同样的事情。我知道我使用的代码很好,并且编译没有太多障碍。它关闭而不是重置到前一行有点烦人,但是当我到达它时,这是一个障碍我会跳过。

我环顾四周,说实话,虽然有些事情可能会奏效,但我确实相对无能为力,有些程序员说话有点苍蝇。

可能是因为我被扔进了它并且我还没有习惯它,但就目前而言,我需要你的帮助。

我的代码只是如下工作(简化以节省时间):

int main()
{
    int points, wager;
    points = 10;
    output "HEADS OR TAILS?";

    if (player has 1 point or more)
    {
        output "Input wager";
        input wager value;
        output "Wager is (player input)";
        output "Heads or Tails?";
        input h or t;                      //This was what I wanted

        if (player selects 'heads')        //For sake of simplicity, the code
        {                                  //here will account for both heads
            int heads;                     //and tails.
            srand(NULL);
            heads = random number 1 and 2;
            if (heads = 1)
            {
                output "HEADS!";
                output "You win 'wager'!";
                points = points + wager;
            }
            if (heads = 2)
            {
                output "TAILS!";
                output "You lose 'wager'!";
                points = points - wager;
            }
        }
    }
    if (player has 0 points)
    {
        output "GAME OVER";
    }
}

我想要做的是让用户输入' h'或者是'确定他们是否想要正面或反面。

1 个答案:

答案 0 :(得分:-1)

在您的编程课程中,他们会告诉您他们希望您将其用作输入和输出工具,例如char inputChar; cin >> inputChar;或类似工具。使用他们告诉您使用的任何他们希望您使用的样式,例如

cout << HEADS_OR_TAILS_PROMPT;
char inputChar;
cin >> inputChar;
switch(inputChar) {
  case 'h':
  {
    ... // code for the heads case
    break;
  }
  case 't':
  {
    ... // code for the tails case
    break;
  }
  default:
    // whatever you want to do if they didn't input a valid option
}

虽然,老实说,问你的教授会为你提供一个更好的答案,让你得到更好的答案,而不是问我们是什么。