ожидаемое promary-выражение перед «void» c ++

0

Эй, я новичок в 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 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;       
                   }          
                   }          
                             }

}

Кроме того, если вы могли бы дать мне некоторую помощь в полном совете по кодированию, чтобы код был более чистым и организованным, сейчас все, что я делаю, это убедиться, что все фигурные латуни все выровнены друг с другом, и каждая секунда, если утверждение отталкивается назад так что он не делает вид звездного влияния на код.

  • 3
    Функции выходят за пределы других функций.
  • 0
    И вообще использовать функции ... Это приведет к большому беспорядку операторов if, если вы не научитесь использовать функции. Если вы вернетесь в ту же комнату, вам придется снова ее кодировать и т. Д. Я предлагаю вам использовать приличную IDE (доступную для всех основных ОС бесплатно), тогда о форматировании кода в основном позаботятся.
Теги:
adventure

2 ответа

2

Вы сделали функцию внутри другой:

int main()
{
   ...

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

   ...

}

Это незаконно для регулярных функций (вы также можете делать это с помощью лямбда-функций). Я думаю, вы запутались между функциями и goto (что не является хорошей практикой для его использования). Я думаю, что вам не нужна функция в вашем случае. Поскольку next функция имеет много зависимостей от основных локальных переменных. Кстати, вы можете поместить внутреннюю функцию вне основной функции.

И хороший отступ может помочь вам в организации и форматировании вашего кода.

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 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++. Функция внутри функции также является незаконной

Ещё вопросы

Сообщество Overcoder
Наверх
Меню