-1

ターミナルでC++プログラム用に次の構造を作成しようとしています。

Menu:
   [1] Add Number
   [2] View Numbers
   [3] View Average

[CHOICE 1] 
   - Add a Number to array
   - Once finished, show Menu again so user can select another action
[CHOICE 2] 
   - View all numbers in array
   - Once finished, show Menu again so user can select another action
[CHOICE 3] 
   - View average of all numbers in array
   - Once finished, show Menu again so user can select another action

これをどのように設定するか正確にはわかりません。ユーザーが各メニュー項目のそれぞれの番号を入力すると、対応する情報が表示されます。これは、ifステートメントを使用してユーザーが入力した番号を探すので、非常に簡単です。

ただし、ユーザーが別のアクションを選択できるようにメニューを再度表示しようとすると、問題が発生します。ここで何らかのループが必要であることは認識していますが、どうすればよいかわかりません。

これを開始する方法の基本構造を設定するのを手伝ってもらえますか?本当にありがたいです。

4

3 に答える 3

3

コードを別々の関数に分解します。

enum Action { AddNumber, ViewNumbers, ViewAverage, Quit, Error };

Action show_menu_and_read_user_input();   // write this!

int main()
{
    std::vector<int> numbers;

    for (bool again = true; again; )
    {
        switch (show_menu_and_read_user_input())
        {
            case Error:
                std::cout << "Sorry, I did not understand.\n";
                break;

            case Quit:
                std::cout << "Goodbye!\n";
                again = false;
                break;

            // ...
        }
    }
}
于 2013-01-13T23:32:17.853 に答える
3

非常に基本的なレベルで...

for(;;)
{
    // Display the menu
    cout << "Menu:\n"
            "   [1] Add Number\n"
            "   [2] View Numbers\n"
            "   [3] View Average\n";

    // Ask user for input
    int choice;
    cin >> choice;

    // Take action
    if( choice == 1 ) {
        // ...
    } else if( choice == 2 ) {
        // ...
    } else if( choice == 3 ) {
        // ...
    } else {
        cout << "Invalid choice\n";
    }
}

メニューに「exit」オプションがないように見えるため、これは無限ループです。初心者として混乱しやすく、誤って問題が発生する可能性があるため、私はこれifをの代わりにステートメントでレイアウトしました。switchswitch

一度に1ステップ=)

于 2013-01-13T23:43:19.977 に答える
0

メニューエントリを関数に分割して、すべてをクリーンでわかりやすくします(今のところ)。

これは大幅に拡張でき、ある種のメニュー処理などを作成できますが、私は次のようなものに固執します。

void mainmenu() {
    int choice;
    do {
        std::cout << "Menu\n[1] -> Add Number\n[2] -> View Numbers\n[3] -> View Average\n[0] -> Quit" << std::endl;
        std::cin >> choice;
        // now perform actions based on the choice
        switch (choice) {
        case 1:
            addanumber();
            break;
        case 2:
            printnumbers();
            break;
        case 3:
            printaverage();
            break;
        }
    } while (choice); // loop as long as the choice isn't 0 (i.e. user wants to exit)
}

Kerrekがコメントで述べたように、これはユーザーが実際に整数値を選択したかどうかを実際にはチェックしません。これにより、コードは少し複雑になりますが、一般的には良い習慣になります(そのようなことを無視すると、物事のスキャンが厄介になるため):

cin >> choice;上記を次のように置き換えます。

if (!(std::cin >> choice)) {
    choice = -1; // if input provide an invalid choice so the menu just shows again
    // also clear the input buffer of leftovers
    std::cin.clear(); // clear the buffer
    std::cin.ignore(1000, '\n'); // read any leftover chars till a line break (user hit return key)
}
于 2013-01-13T23:38:52.737 に答える