功能定义和功能结束

时间:2019-04-19 20:25:33

标签: function

我还是编码和C ++的新手,但想超越简单的程序挑战自我。我开始写“选择自己的冒险”,虽然我似乎已经掌握了大部分内容,但我仍然停留在这一部分,但这是我最后的错误。我知道这可能是我没有看到的菜鸟错误

我已经在相关的函数中添加了{},并将其移到了代码的不同部分,我尝试了多个选项,但似乎没有任何效果。我可以利用所有人提供的任何见解。

#include <iostream>
#include <string>


using namespace std;


class Player
{

public:
  string name;
  int damage;

};


void rightDoor ()
{

  std::cout << "You spot an open window. Do you jump to try and escape the fire or find another exit?" << std::endl;
};

void leftDoor ()
{
  string answer;
  std::cout <<
    "There is a staircase ahead of you. Would you like to go up or down it?"
    << std::endl;
  std::cin >> answer;
  if (answer == "up")
    {
      std::cout <<
    "There is an open window. Would you like to jump out of it?" << std::
    endl;
    }
  else
    (answer == "down");
  {

    std::cout << "You've found yourself in the dungeon." << std::endl;
  }

};


void doorChoice ()
{
  string answer;
  if (answer == "door one")
    {
      std::cout <<
    "This room doesn't seem to be on fire. You're winning already." << std::endl;
      rightDoor ();
    }
  else {
      std::cout <<"The fire has swept through this part of the house." << std::endl;
      leftDoor ();
    }



  int main ()
  {

      newGame();

    Player Player1;
    Player1.name;
    Player1.damage = 0;

    if (Player1.damage == 3)
      {
    std::cout << "Game Over" << std::endl;
      }


void newGame () {
    std::cout << "Greetings player. Before you start on your quest tell me your name." << std::endl;
    std::cin >> Player1.name;
    std::cout << "Are you ready for an adventure " << Player1.name << "?" << std::endl;
    std::cout << "You awake groggily in room that is filled with flames." << std::endl;
    std::cout << "Ahead of you are two doors, which do you choose? Door number 1 or door number 2?"
      << std::endl;
    cin >> doorChoice ();
    std::cout << "Game Over" << std::endl;
};

    return 0;
  }

我得到的错误是:

main.cpp: In function ‘void doorChoice()’:
main.cpp:86:3: error: a function-definition is not allowed here before ‘{’ token
   {
   ^
main.cpp:117:3: error: expected ‘}’ at end of input
   }
   ^

我希望获得一个能正常工作的“选择自己的冒险”游戏,该游戏在整个代码中都调用我的不同功能。

0 个答案:

没有答案