2012-04-15 10 views
0

無限ループで1を押すと誰かが私のメニューで間違っていることを誰でも見ることができます。私はメニューの周りにループがあるので、メニューはいつでも選択肢を通って戻ることができます。C++メニュー。メニューの無制限ループ

#include <iostream> 
using namespace std; 

int main() 
{ 
    int choice; 
    bool menu = true; 
    cout <<"========Welcome to the database menu========\n"; 

    cout << "Press 1 to insert a new record at a particular position\n" 
      "Press 2 to delete a record from a particular position\n" 
      "Press 3 to search the database and print results\n" 
      "Press 5 to find the average experience points of players at a particular level\n" 
      "Press 6 to find and remove all duplicate entries\n" 
      "Press 0 to quit\n\n\n\n\n\n\n\n\n"; 

    cout << "Choice: "; 
    cin >> choice; 
    //***************************************************************************** 
    // Switch menu to display the menu. 
    //***************************************************************************** 
    while(menu) 
    { 
     switch (choice) 
     { 
      case 1: 
       cout << "dixie"; 
       break; 
      case 2: 
       cout << "bexie"; 
       break; 
      default: 
       cout<< "That is not a choice!!!\n"; 
     } 
    }  
    getchar(); 
    getchar(); 
} 

答えて

0

while (menu)と書かれています。これは、メニューをfalseに設定するまで続けます。

また、ループ内にcin >> choiceを追加したいと思うか、それとも何度も何度も選択を繰り返すだけです。

+0

あなたのアドバイスに感謝しました。どうもありがとう。 – Pendo826

2

あなたwhileループ内menuまたはchoiceのいずれかを変更することができます何のコードがありません: はここに私のコードです。一度それが行くと、それは決して止まらないでしょう。

+0

ケースを選択したにもかかわらずメニューオプションが表示されたら、ループインを止めるにはどうすればいいですか? – Pendo826

+0

問題は、ループが間違った場所にあることです。ユーザーが '0'を選択するまでのループは問題ありませんが、' switch'ステートメントで '0'を処理する必要があります。 'while'ループが必要な解決策については私の答えを見てください。 –

0

私は、whileループは、メニューオプションを印刷し、ユーザーがそのようなオプションを選択した含めるべきであると仮定します:

while (menu) 
{ 
    cout <<"========Welcome to the database menu========\n"; 
    cout << "Press 1 to insert a new record at a particular position\n" 
      "Press 2 to delete a record from a particular position\n" 
      "Press 3 to search the database and print results\n" 
      "Press 5 to find the average experience points of players at a particular level\n" 
      "Press 6 to find and remove all duplicate entries\n" 
      "Press 0 to quit\n\n\n\n\n\n\n\n\n"; 

    cout<< "Choice: "; 
    cin>> choice; 

    switch (choice) 
    { 
     case 1: 
      cout << "dixie"; 
      break; 
     case 2: 
      cout << "bexie"; 
      break; 
     default: 
      cout<< "That is not a choice!!!\n"; 
    } 
} 

別の可能性は、ちょうどcout << "Choice: "行の前にwhileループを開始することです。

0

menu変数は常にループ内でtrueです。現時点ではwhile(true)と同じです。