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'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 ステートメントが毎秒押し戻されていることを確認することだけですコードにスターのような影響を与えないようにします。

4

2 に答える 2

2

別の関数の中に関数を作成しました:

int main()
{
   ...

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

   ...

}

通常の関数では違法です(ラムダ関数でも実行できます)。関数とgotoジャンプを混同していると思います(これは使用するのに適していません)。あなたの場合、関数は必要ないと思います。関数nextにはメインのローカル変数への多くの依存関係があるためです。ちなみに、内部関数はメイン関数の外に置くことができます。

また、適切なインデントは、コードを整理してフォーマットするのに役立ちます。

于 2013-11-24T19:21:30.653 に答える
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++ では違法です。関数内の関数も不正です

于 2013-11-24T19:51:11.760 に答える