-3
クラスのプロジェクトで作業していて、コード全体がうんざりになっている可能性があります。なぜ私がここに頼んでいるのか)。プログラムでは、3つの選択肢のいずれかを選択して、その項目のどれくらいを選ぶかを尋ねます。ただし、ユーザーがメニューにないものを選択した場合は、エラーメッセージが表示されるはずです。私のコードに動作するelseステートメントを取得することはまだ可能ですか?C++のIf-elseステートメントで混乱しています。
cout << "Would you like a sandwich, a platter, or a salad? ";
cin >> choice;
if (choice == "sandwich")
{
cout << "\n How many sandwiches would you like? ";
cin >> sandwich;
if (sandwich >= 3)
{
cout << "\n Each sandwich costs: $" << SANDWICH_DISCOUNT << endl;
total = SANDWICH_DISCOUNT * sandwich;
cout << "\n The total cost of the sandwich(es) is: $" << total << endl<< endl;
}
else
{
cout << "\n Each sandwich costs: $" << SANDWICH << endl;
total = SANDWICH * sandwich;
cout << "\n The total cost of the sandwich(es) is: $" << total << endl<<endl;
}
}
if (choice == "platter")
{
cout << "\n How many platters would you like? ";
cin >> platter;
if (platter >= 3)
{
cout << "\n Each platter costs $" << PLATTER_DISCOUNT << endl;
total = PLATTER_DISCOUNT * platter;
cout << "\n The total cost of the platter(s) is: $" << total << endl<<endl;
}
else
{
cout << "\n Each platter costs $" << PLATTER << endl;
total = PLATTER * platter;
cout << "\n The total cost of the platter(s) is: $" << total << endl<<endl;
}
}
if (choice == "salad")
{
cout << "\n How many salads would you like? ";
cin >> salad;
if (salad >= 3)
{
cout << "\n Each salad costs $" << SALAD_DISCOUNT << endl;
total = SALAD_DISCOUNT * salad;
cout << "\n The total cost of the salad(s) is: $" << total << endl<<endl;
}
else
{
cout << "\n Each salad costs $" << SALAD << endl;
total = SALAD * salad;
cout << "\n The total cost of the salad(s) is: $" << total << endl<<endl;
}
}
? else節を追加して、悪い入力に対処しようとする。 – Carcigenicate