2017-03-05 41 views
0

我不知道如何去問這個問題,因爲我在C++中是新手,而im不擅長英語。C++在用戶輸入上添加多個變量

所以標識由C++中,其中有一個在菜單項列表的餐廳的菜單,但問題是我只能選擇1項,概括起來講,我如何才能選擇例如像菜單的多個項目

菜單列表

  1. 食品1
  2. 食物2
  3. 食物3

選擇上面的項目:1 3 然後顯示什麼是用戶輸入和總結它的價格及其價格。

我想使用while循環,雖然用戶輸入字符「C」指結算它將概括了一切

回答

2

你需要做的就是用一個標誌變量來確定用戶是否完成將東西添加到購物車。

例如,界面看起來像這樣

Menu list 
- 
food 1 
food 2 
food 3 
- 
quit (q) 

那麼它只是看到如果input == "q"並打破循環的一個問題,如果它是

while(true){ 
    cin << input; 
    if(input == "q") break; 
    else //other-logic-here 
} 
0

方式來實現你的想法:

const int menu[]{ 10,20,30 }; 
int a=0, b=0, c=0; 

cout << "enter your mix.(a=10,b=20 and c=30). Press other keys to sum." << endl; 
char input; 
cin >> input; 

while (input != 'q') 
{ 
    switch (input) 
    { 
     case 'a' :a += menu[0]; break; 
     case 'b' :b += menu[1]; break; 
     case 'c' :c += menu[2]; break; 
     default: 
      cout << a << " + " << b << " + " << c <<" = "<< a + b + c << endl; 
      a = b = c = 0; 
      cout << "enter a,b,or c." << endl; 
    } 
    cin >> input; 
} 

結果:

enter your mix.(a=10,b=20 and c=30). Press other keys to sum. 
b 
b 
c 
c 
c 
7 
0 + 40 + 90 = 130 
enter a,b,or c.