シカゴから特定の都市までの移動にかかる時間を与える関数を作成しています。ユーザーが都市を選択すると、所要時間が表示され、ループバックして主な質問をし、ユーザーが別の都市を選択できるように、ループさせようとしています。また、ループを終了できるオプションも含めています。私がこれまでに持っているのはこれです:
main()
{
TripInfo trip;
int choice;
do
{
cout << "You are in Chicago. Where would you like to drive?\n"
<< "Enter number of city\n" << "1. New York City\n" << "2. Boston\n"
<< "3. Philadelphia\n" << "4. Toronto\n" << "5. Washington D.C.\n"
<< "6. Miami\n" << "7. Indianapolis\n" << "8. Los Angeles\n"
<< "9. San Fransisco\n" << "10. Phoenix\n" << "11. EXIT" << endl;
cin >> choice;
if(choice = 11)
{
cout << "Program terminated." << endl;
break;
}
trip.setDistance(choice);
cout << "The distance from Chicago to " << trip.getDestination() << " is "
<< trip.getDistance() << endl;
trip.setRate();
cout << "The speed you will be travelling at from Chicago to "
<< trip.getDestination() << " is " << trip.getRate() << endl;
trip.calculateTime();
cout << "The time it will take to travel from Chicago to "
<< trip.getDestination() << " at " << trip.getRate()
<< " miles per hour will be:\n " << trip.getTime() << " hours."
<< endl;
}
}
問題は出力にあります。if ステートメントに条件があり、choice が 11 でない場合でも、関数は「プログラムが終了しました」と出力します。これを修正して、choice = 11 の場合はプログラムを終了し、choice が 11 でない場合は、choice が 11 になるまでさまざまな関数を何度も繰り返しループします。