5

私はまったく新しいので、これを他にどのように尋ねるか、何を検索すればよいかさえわかりません。

例: 複数のサブメニューを含むメニューをナビゲートしたい。この例では、「オプション」と「ゲーム」を使用して、意味を説明します。3 つのオプションがあるメニューがあるとします。

1 - 開始

2 - オプション

3 - やめる

オプションを選択すると、別のメニューに移動します。次に、次のようになります

1 - 難易度

2 - サウンド

3 - 戻る

ここからどこに行くかによって、明らかにサブメニューが増えます。do-while ループやあらゆる種類のものをネストしようとしましたが、何が間違っているのかを理解するのに十分な理解がありません。

これが私がこれまでに持っているものです:

#include <cstdlib>
#include <iostream>

using namespace std;

int main(int argc, char *argv[])
{
  int choice;

    do{
    cout << "Main Menu\n";
    cout << "Please make your selection\n";
    cout << "1 - Start game\n";
    cout << "2 - Options\n";
    cout << "3 - Quit\n";
    cout << "Selection: ";
    cin >> choice;

         switch(choice) {
           case 1:
             cout << "Pew pew!\n";
             break;
           case 2:
             cout <<"????\n";
             break;
           case 3:
             cout << "Goodbye!";
             break;
           default:
             cout << "Main Menu\n";
             cout << "Please make your selection\n";
             cout << "1 - Start game\n";
             cout << "2 - Options\n";
             cout << "3 - Quit\n";
             cout << "Selection: ";
             cin >> choice;    
         }
      } while(choice !=3);                            
    system("PAUSE");
    return EXIT_SUCCESS;
}

これは通常のメニューのように機能します。しかし、ここからどこへ行くべきかわかりません。私はいくつかの本を調べましたが、これに少しでも関連するものを見つけることは完全にランダムでした. どんな助けや例も大歓迎です。

大量のループをネストすると、すべてのループが毎回同時に実行されます。これが起こらないようにするにはどうすればよいですか?より多くの選択をしますか?(choice1-2-3 など ? または何?)

4

4 に答える 4

5

わかりました。すべての助けをありがとう。これが私が最終的にたどり着いたものです。それは私が望むように実行され、max_ の例と Mike B の解説により、これはかなりうまく機能すると思います。

皆さん、どうもありがとう =)

#include <iostream>
#include <cstdlib>


using namespace std;

void menu();
void mainMenu();
void optionsMenu();
void options();
int choice1 = 0;
int choice2 = 3;

int main(int argc, char** argv) {



    menu();



    return 0;
}


void menu(){

        do {
        choice2 = 0;
        mainMenu();

        switch(choice1) {

            case 1:
                cout << "Pew pew!\n";
                break;

            case 2:
                options();
                break;

            case 3:
                break;

        }

    } while(choice1 != 3);


}

void options(void) {

    do {
        optionsMenu();

        switch(choice2){

            case 1:
                cout << "So difficult!\n";
                break;

            case 2: 
                cout << "Beep!\n";
                break;

            case 3:
                break;

            default: 
                break;

        }

    } while(choice2 != 3);


}




void mainMenu(void) {



    cout << "Main Menu\n";
    cout << "1 - Start game\n";
    cout << "2 - Options\n";
    cout << "3 - Quit\n";
    cout << "Please choose: ";
            cin >> choice1;

}

void optionsMenu(void) {



    cout << "Options Menu\n";
    cout << "1 - Difficulty\n";
    cout << "2 - Sound";
    cout << "3 - Back\n";
    cout << "Please choose: ";
            cin >> choice2;

}
于 2013-06-02T17:33:58.187 に答える
4

これはどうですか(コンパイルできるかどうかはわかりません):

     #include <cstdlib>
     #include <iostream>

     using namespace std;

     int GetInput()
     {
         int choice;    
        cin >> choice;
        return choice;
     }

     void DisplayMainMenu()
     {
     cout << "Main Menu\n";
         cout << "Please make your selection\n";
         cout << "1 - Start game\n";
         cout << "2 - Options\n";
         cout << "3 - Quit\n";
         cout << "Selection: ";
     }

     void DisplayOptionsMenu()
     {
         cout << "Options Menu\n";
         cout << "Please make your selection\n";
         cout << "1 - Difficulty\n";
         cout << "2 - Sound\n";
         cout << "3 - Back\n";
         cout << "Selection: ";
     }

     void Options()
     {
        int choice = 0;
        do
        {
            system("cls");
            DisplayOptionsMenu();
            choice = GetInput();
            switch(choice)
            {
                case 1:
                    cout << "difficulty stuff";
                    break;
                case 2:
                    cout << "sound stuff";
                    break;
                case 3:
                    break;
                default:
                    break;
            }
        } while(choice!=3);
     }

     int main(int argc, char *argv[])
     {
         int choice = 0;

         do
         {
            system("cls");
            DisplayMainMenu();
            choice = GetInput();
            switch(choice) {
                    case 1:
                            cout << "Pew pew!\n";
                            break;
                    case 2: 
                            Options();
                            break;
                    case 3: 
                            cout << "Goodbye!";
                            break;

                    default: 
                            break;
                   }
           } while(choice!=3);
         system("PAUSE");
         return EXIT_SUCCESS;
     }
于 2013-05-31T08:55:32.937 に答える
4

ここでいくつか変更することをお勧めします。オブジェクト指向設計に精通していますか? そうでない場合、C++ でコードを記述しようとしている場合は、それについて読むことを強くお勧めします (または、多くのプログラミング言語の非常に重要な側面であるため、一般的にコードを記述します)。

各メニューとサブメニューを個別のオブジェクトとして扱うことを検討してください。ループに入るたびに、オブジェクト ポインターを使用して、現在のメニュー テキストを出力するメソッドを呼び出します。

次に、通常どおりユーザーからの入力を受け取り、現在使用しているメニュー オブジェクトを変更します。

これは、コンソール メニューを実行するための最も理想的な方法ではないかもしれませんが、オブジェクト指向プログラミングがどのように機能するかについて非常に強力な基礎を築くことができます。

例を添付しました:

#include <iostream>
#include <string>

class BaseMenu
{
public:
    BaseMenu() { m_MenuText = "This shouldn't ever be shown!"; } // This is the constructor - we use it to set class-specific information. Here, each menu object has its own menu text.
    virtual ~BaseMenu() { } // This is the virtual destructor. It must be made virtual, else you get memory leaks - it's not a quick explaination, I recommend you read up on it
    virtual BaseMenu *getNextMenu(int iChoice, bool& iIsQuitOptionSelected) = 0; // This is a 'pure virtual method', as shown by the "= 0". It means it doesn't do anything. It's used to set up the framework
    virtual void printText() // This is made virtual, but doesn't *have* to be redefined. In the current code I have written, it is not redefined as we store the menu text as a string in the object
    {
        std::cout << m_MenuText << std::endl;
    }

protected:
    std::string m_MenuText; // This string will be shared by all children (i.e. derived) classes
};

class FirstMenu : public BaseMenu // We're saying that this FirstMenu class is a type of BaseMenu
{
    FirstMenu()
    {
        m_MenuText = "Main Menu\n"                         // What we are doing here is setting up the string to be displayed later
                    + "Please make your selection\n"       // What we are doing here is setting up the string to be displayed later
                    + "1 - Start game\n"                   // What we are doing here is setting up the string to be displayed later
                    + "2 - Options\n"                      // What we are doing here is setting up the string to be displayed later
                    + "3 - Quit\n"                         // What we are doing here is setting up the string to be displayed later
                    + "Selection: ";                       // What we are doing here is setting up the string to be displayed later
    }

    BaseMenu *getNextMenu(int choice, bool& iIsQuitOptionSelected) // This is us actually defining the pure virtual method above
    {
        BaseMenu *aNewMenu = 0; // We're setting up the pointer here, but makin sure it's null (0)

        switch (choice) // Notice - I have only done "options". You would obviously need to do this for all of your menus
        {
            case 2:
            {
                aNewMenu = new SecondMenu; // We're creating our new menu object here, and will send it back to the main function below
            }

            case 3:
            {
                // Ah, they selected quit! Update the bool we got as input
                iIsQuitOptionSelected = true;
            }

            default:
            {
                // Do nothing - we won't change the menu
            }

        }

        return aNewMenu; // Sending it back to the main function
    }

};

class SecondMenu : public BaseMenu
{
    SecondMenu()
    {
        m_MenuText = "OptionsMenu\n"
                    + "Please make your selection\n"
                    + "1 - ????"
                    + "2 - dafuq?";
    }

    BaseMenu *getNextMenu(int choice, bool& iIsQuitOptionSelected) // This is us actually defining the pure virtual method above
    {
        BaseMenu *aNewMenu = 0; // We're setting up the pointer here, but makin sure it's null (0)

        switch (choice) // Notice - I have only done options. You would obviously need to do this for all of your menus
        {
            case 1:
            {
                aNewMenu = new FirstMenu; // We're creating our new menu object here, and will send it back to the main function below
            }
            break;
            case 2:
            {
                aNewMenu = new FirstMenu; // We're creating our new menu object here, and will send it back to the main function below
            }
            break;

            default:
            {
                // Do nothing - we won't change the menu
            }

        }

        return aNewMenu; // Sending it back to the main function
    }
};

int main (int argc, char **argv)
{
    BaseMenu* aCurrentMenu = new FirstMenu; // We have a pointer to our menu. We're using a pointer so we can change the menu seamlessly.
    bool isQuitOptionSelected = false;
    while (!isQuitOptionSelected) // We're saying that, as long as the quit option wasn't selected, we keep running
    {
        aCurrentMenu.printText(); // This will call the method of whichever MenuObject we're using, and print the text we want to display

        int choice = 0; // Always initialise variables, unless you're 100% sure you don't want to.
        cin >> choice;

        BaseMenu* aNewMenuPointer = aBaseMenu.getNextMenu(choice, isQuitOptionSelected); // This will return a new object, of the type of the new menu we want. Also checks if quit was selected

        if (aNewMenuPointer) // This is why we set the pointer to 0 when we were creating the new menu - if it's 0, we didn't create a new menu, so we will stick with the old one
        {
            delete aCurrentMenu; // We're doing this to clean up the old menu, and not leak memory.
            aCurrentMenu = aNewMenuPointer; // We're updating the 'current menu' with the new menu we just created
        }
    }

    return true;    
}

これは、最初は少し複雑かもしれないことに注意してください。人々が投稿した他の回答を読むことを強くお勧めします。それを行う方法についていくつかのアプローチを提供する必要があり、基本的なものからより複雑なものまで、各変更を調べることができます。

于 2013-05-31T09:06:34.790 に答える
1

あなたがやろうとしていることを見て、ユーザーがまだ最初にゲームをプレイしたいと思っていることを確認する方法を変更します。while ループを使用して、変数が true か false かを確認してください (人々はこれにブール変数 (ブール値) を使用する傾向があります。1 または 0 に設定された int は同じことを行います)。これにより、do-while の必要がなくなります。制御ロジック (if/else、while、for ループ) と論理演算子 (&& - and、|| - or、!= - not equal to) を読むことをお勧めします。制御ロジックはコードにさまざまな処理をさせます。ブール値は yes/no のシナリオをすばやくチェックし、論理演算子は 1 つの if ステートメントで複数の項目をチェックできるようにします。読み物:ループ

編集:資料を読むためのリンクを増やしてください。それらを投稿する担当者はいません。

次に、別の変数 (int または任意のもの) を使用して、現在の画面を追跡します。この選択に基づいて、さまざまなオプションを表示しますが、入力 1、2、3 を使用して次のアクションを決定します。

いくつかのひどい疑似コードでは、ここに私が傾くものがあります:

main()
{
   int choice
   int screen = 1
   bool running = true

   while(running) {
       //Screen 1, Main menu
       if(screen == 1) {
       cout << stuff
       cout << stuff
       cout << option 1
       cout << option 2
       cout << option 3
       cout << selection:
       cin >> choice
       }
       else if(screen == 2){
       //options screen here

       }
       else {
       //default/error message
       }

       //add some choice logic here
       if(screen == 1 && choice == 3){
           //being on screen one AND choice three is quit
           running = false;
       }
       else if(screen == 1 && choice == 2){
           //etc..
       }

  }

}

これは私の最初の適切な回答です。ひどい批判はすべて受け入れられます。

于 2013-05-31T08:49:59.367 に答える