我正在做一个函数来给出从芝加哥到某个城市的旅行时间。我试图让它循环,以便当用户选择城市时,它会给出所花费的时间并循环回来询问主要问题并让用户选择另一个城市。我还包括一个他们可以退出循环的选项。我到目前为止是这样的:
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 语句有条件并且如果选择不是 11,该函数仍会打印“程序终止。”。我该如何解决这个问题,以便如果choice = 11,程序终止,如果choice 不是11,它会继续并一次又一次地遍历各种函数,直到choice 被选为11?