在“void”c ++之前预期的primary-expression

时间:2013-11-24 19:10:23

标签: c++ adventure

嘿我是c ++的新手我没有看到我的代码有任何问题。 我试图制作一个非常基本的自己的冒险游戏。 谢谢你的时间。

#include <iostream>

using namespace std;

void next();

int main()
{

    int x;
    int y;
    int z;

    cout << "Welcome To \n";
    cout << "killing the Dragon! \n";
    cout << endl;

    cout << "There Is a Road infront of you you may go left or right \n";
    cout << "Left = any other number, Right = 1 \n";
    cin >> x;
    if(x == 1){
         do{       
            cout << "A rock blocks your bath \n";
            cout << "You Go back \n"; // Fix loop problem "Can't choose 0 more the  once of it all screws up"
            cout << "Left = 0, Right = 1 \n";
            cin >> x;
            if(x == 0){next();}
           }while(x == 1);

                  void next(){
                  cout << "You Come up to a small village \n";
                  cout << "You find 100 gold coins in your pocket \n";
                  cout << "You may continue your bath or buy a sword at the local blacksmith \n";
                  cout << "Continue = 1, buy sword = any other number \n";
                  cin >> y;  

    if(y == 1){                
    cout << "You buy the sword for 50G's and continue with your adventure \n";
    cout << "You find a dragon down the road luckaly you have your sword! \n";
    cout << "Do you kill the Dragon or let him live? \n";
    cout << "murder the but cack = 1, Let him live = any other Number \n";
    cin >> z;                 
              }else{          
              cout << "You continue your bath and get pwnd by a dragon down the road your a failure \n";
              system("pause");
              return 0;       
                   }          

    if(z == 1){               
    cout << "YA YOU PWNED THE DRAGON GRATZ BRO YOU NOW HAVE THE TITLE DRAGON SLAYER AND YOU MADE 1000000000000000000000000000000000000000000000000000000000000000000000G'S \n";
    system("pause");          
    return 0;                 
              }else{          
              cout << "you got owned by the dragon! ITS a DRAGON what where you thinking letting it \nlive now your dead hope your happy! \n";
              system("pause");
              return 0;       
                   }          
                   }          
                             }

}

另外,如果你能给我一些帮助,可以提供完整的编码建议,以保持代码更加整洁有序,现在我正在做的就是确保所有卷曲的黄铜都是彼此排列的,每一秒都是if语句被推回去,这样它就不会对代码产生一种影响。

2 个答案:

答案 0 :(得分:2)

你在另一个中创造了一个功能:

int main()
{
   ...

   void next() { ... } // Illegal !

   ...

}

对于常规函数来说这是非法的(你也可以通过lambda函数来完成)。我认为你在功能和goto跳跃之间混淆了(使用它不是好习惯)。我认为你不需要一个功能。因为函数next与main的局部变量有许多依赖关系。顺便说一下,你可以把内部函数放在main函数之外。

而且,良好的缩进可以帮助您组织和格式化代码。

答案 1 :(得分:1)

#include <iostream>
#include <cstdlib>
using namespace std;

void next();

int main()
{

    int x;
    int y;


    cout << "Welcome To \n";
    cout << "killing the Dragon! \n";
    cout << endl;

    cout << "There Is a Road infront of you you may go left or right \n";
    cout << "Left = any other number, Right = 1 \n";
    cin >> x;
    if(x == 1){
         do{
            cout << "A rock blocks your bath \n";
            cout << "You Go back \n"; // Fix loop problem "Can't choose 0 more the    once of it all screws up"
            cout << "Left = 0, Right = 1 \n";
            cin >> x;
            if(x == 0){
            next();
            }
           }while(x == 1);


                    }

}
void next()
{
    int y;
    int z;

    cout << "You Come up to a small village \n";
    cout << "You find 100 gold coins in your pocket \n";
    cout << "You may continue your bath or buy a sword at the local blacksmith \n";
    cout << "Continue = 1, buy sword = any other number \n";
    cin >> y;

    if(y == 1){
    cout << "You buy the sword for 50G's and continue with your adventure \n";
    cout << "You find a dragon down the road luckaly you have your sword! \n";
    cout << "Do you kill the Dragon or let him live? \n";
    cout << "murder the but cack = 1, Let him live = any other Number \n";
    cin >> z;
    }else{
        cout << "You continue your bath and get pwnd by a dragon down the road your a failure \n";
        system("pause");
                   }
    if(z == 1){
    cout << "YA YOU PWNED THE DRAGON GRATZ BRO YOU NOW HAVE THE TITLE DRAGON SLAYER AND YOU MADE 1000000000000000000000000000000000000000000000000000000000000000000000G'S \n";
    system("pause");
              }else{
              cout << "you got owned by the dragon! ITS a DRAGON what where you thinking letting it \nlive now your dead hope your happy! \n";
              system("pause");
                   }
                   }

我修复了你的代码,我发现了10个错误并修复了所有错误。如果要使用非常不可取的系统调用,则需要cstdlib头文件。此外,void函数返回一个值。这在c ++中是非法的。函数内部的函数也是非法的