の文字列を入力したときに私のメニュー自体が何回も繰り返されるので、ここで私が唯一の文字を読み取り、結果として何かをするメニューを作成しようとしてきた私は文字
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'または正しいメニューオプション以外の応答を無視するようにします。
全文を読む –
'if(input)' - あなたの意図は何ですか?そして、どのような変数型が 'input'ですか? – PaulMcKenzie