2016-03-24 5 views
-1

の文字列を入力したときに私のメニュー自体が何回も繰り返されるので、ここで私が唯一の文字を読み取り、結果として何かをするメニューを作成しようとしてきた私は文字

do 
{ 
    cout << "Welcome to our Coffee Shop! Here are the options: " << endl; 
    cout << "C - Coffee ($1.50)" << endl; 
    cout << "T - Tea ($1.00)" << endl; 
    cout << "S - Soda ($1.00)" << endl; 
    cout << "J - Juice ($1.50)" << endl; 
    cout << "M - Manager Special ($2.00)" << endl; 
    cout << "X - Finish Order" << endl; 
    cout << "What drink would you like? Enter C, T, S, J, M, or X." << endl; 
    cin >> input; 

    //does certain actions for a certain character 
    if(input) 
    { 
     switch(input) 
     { 
      case 'C': 
      case 'c': total += coffeePrice; 
        coffeeCount++; 
       cout << "Thanks! You have ordered coffee." << endl; 
       break; 
      case 'T': 
      case 't': total += teaPrice; 
       teaCount++; 
       cout << "Thanks! You have ordered tea." << endl; 
       break; 
      case 'S': 
      case 's': total += sodaPrice; 
       sodaCount++; 
       cout << "Thanks! You have ordered soda." << endl; 
       break; 
      case 'J': 
      case 'j': total += juicePrice; 
       juiceCount++; 
       cout << "Thanks! You have ordered juice." << endl; 
       break; 
      case 'M': 
      case 'm': total += specialPrice; 
       specialCount++; 
       cout << "Thanks! You have ordered the manager special." << endl; 
       break; 

      default: 
       cin.clear(); 
       cin.ignore(numeric_limits<streamsize>::max(), '\n'); 
       break; 

     } 
    } 
    else 
    { 
     cin.clear(); 
     cin.ignore(numeric_limits<streamsize>::max(), '\n'); 

    } 

私のコードです。問題は、「cccc」のような複数のcを持つ文字列を入力すると、メニューが4回繰り返されることです。または、「ccddcc」のような正しい応答ではないcやその他の文字列を入力すると、メニューが2回コーヒーに感謝してメニューが2回繰り返され、コーヒーにもう一度感謝を言います。これが起こるのを止める方法はありますか?私はちょうどそれが 'c'または正しいメニューオプション以外の応答を無視するようにします。

+0

全文を読む –

+0

'if(input)' - あなたの意図は何ですか?そして、どのような変数型が 'input'ですか? – PaulMcKenzie

答えて

0

ここでの問題は、input 'がcharであると思います。したがって、ccccを入力すると、最初の文字を読み込んで移動し、戻ってきて、処理待ちのstd入力にさらに多くのcがあることがわかるので、その次のcと処理をやり直します。 input varを文字列にすることをお勧めします。文字列がサイズ1より大きい場合は、そのまま残すか、最初のcharを使用してください。

char input[256]; // Could probably be a string, but I am used to C for this 
do 
{ 
cout << "Welcome to our Coffee Shop! Here are the options: " << endl; 
cout << "C - Coffee ($1.50)" << endl; 
cout << "T - Tea ($1.00)" << endl; 
cout << "S - Soda ($1.00)" << endl; 
cout << "J - Juice ($1.50)" << endl; 
cout << "M - Manager Special ($2.00)" << endl; 
cout << "X - Finish Order" << endl; 
cout << "What drink would you like? Enter C, T, S, J, M, or X." << endl; 
cin >> input; 

switch(input[0]) 
{ 
    case 'C': 
    case 'c': total += coffeePrice; 
      coffeeCount++; 
     cout << "Thanks! You have ordered coffee." << endl; 
     break; 
    case 'T': 
    case 't': total += teaPrice; 
     teaCount++; 
     cout << "Thanks! You have ordered tea." << endl; 
     break; 
    case 'S': 
    case 's': total += sodaPrice; 
     sodaCount++; 
     cout << "Thanks! You have ordered soda." << endl; 
     break; 
    case 'J': 
    case 'j': total += juicePrice; 
     juiceCount++; 
     cout << "Thanks! You have ordered juice." << endl; 
     break; 
    case 'M': 
    case 'm': total += specialPrice; 
     specialCount++; 
     cout << "Thanks! You have ordered the manager special." << endl; 
     break; 
    default: 
     cin.clear(); 
     cin.ignore(numeric_limits<streamsize>::max(), '\n'); 
     break; 
    } 
} 
else 
{ 
    cin.clear(); 
    cin.ignore(numeric_limits<streamsize>::max(), '\n'); 

} 
+0

さて、これは今感謝します –

関連する問題