2013-01-13 128 views
-1

我試圖創建下面的結構在終端C++程序:混淆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語句來查找用戶輸入的數字。

但是,當我嘗試再次顯示菜單以便用戶可以選擇另一個操作時,出現問題。我認識到某種循環在這裏是必要的,但我不知道如何去做。

你能幫我建立一個如何開始的基本結構嗎?我真的很感激它。

+1

這應該是比較簡單的。你有什麼嘗試? – Rapptz

+3

查看教科書中的基本流量控制。因爲,雖然,如果,如果,其他,切換等,你應該能夠找到最適合你的需求的東西。 –

+0

添加一個選擇「[4]退出」,然後,把所有的主代碼放在一邊(選擇!= 4){你的主代碼在這裏} –

回答

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) 
} 
+0

這是否會在案件「完成」後再次顯示菜單? – codedude

+0

此代碼已損壞。如果輸入操作沒有成功,則讀取「choice」就是UB。同樣的思維錯誤是所有C++ SO問題中90%的核心問題:-(現在你剛剛教過另一個可憐的shlub同樣的錯誤。 –

+0

'do ... while()'會使菜單重新出現,就像Kerrek的for()一樣。選擇你已經學過的任何循環。 – Mario

4

因子代碼爲單獨的功能:

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; 

      // ... 
     } 
    } 
} 
+0

這顯然是一個初學者的問題,所以我想這可能有點矯枉過正,用'enum'等來處理這個問題。 – Mario

+0

yeeaaahhh ...我還沒有結束枚舉。 – codedude

+0

這只是一個「列表」,您可以隨時用數字/整數替換enum元素,例如用'1'代替'Error'並用'0'代替'退出'(並跳過'enum'行)。 – Mario

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"; 
    } 
} 

這是一個無限循環,因爲您的菜單似乎沒有「退出」選項。我已經用if陳述來代替switch,因爲作爲初學者,很容易被switch弄糊塗,並且意外地以問題結束。

一步一個腳印=)

+0

當輸入操作失敗時,這是未定義的行爲。 –