當我輸入字符串「NOMORE」時,它會繼續下一行,詢問車速。它只在完成for循環時停止循環。如何在「NOMORE」輸入時立即停止?謝謝你的幫助。我真的很感激它。爲什麼我的C++標記循環在輸入標記值後繼續?
cout << "Enter a license plate number --> ";
cin >> plate;
if (plate == "NOMORE") break; // add this
cout << "Enter current vehicle's speed --> ";
...
在for
循環的條件僅之間檢查:很遺憾浪費了,如果我沒:(
#include <iostream>
#include <string>
#include <iomanip>
using namespace std;
int main()
{
string plate;
int totalCount;
int ticketCount = 0;
double speed;
double base = 150;
double limit;
double ticket;
double overspeed;
for (totalCount = 0; plate != "NOMORE"; totalCount++)
{
cout << "Enter a license plate number --> ";
cin >> plate;
cout << "Enter current vehicle's speed --> ";
cin >> speed;
cout << "Enter speed limit in the zone --> ";
cin >> limit;
overspeed = speed - limit;
if (overspeed >= 5 && overspeed <= 20)
{
ticket = base + 5 * overspeed;
cout << "A ticket of " << setprecision(2) << fixed << ticket << " is issued to " << plate << "\n\n";
ticketCount++;
}
else if (overspeed > 20 && overspeed <= 50)
{
ticket = base + 10 * overspeed;
cout << "A ticket of " << setprecision(2) << fixed << ticket << " is issued to " << plate << "\n\n";
ticketCount++;
}
else if (overspeed > 50)
{
ticket = base + 1000 + (10 * overspeed);
cout << "A ticket of " << setprecision(2) << fixed << ticket << " is issued to " << plate << "\n\n";
ticketCount++;
}
else
cout << "No ticket is issued to " << plate << ".\n\n";
}
cout << ticketCount << " out of " << totalCount << " times\n";
return 0;
}
'CIN >>板;如果(板== 「曲子」)破;' –
你的循環之所以這麼做,是因爲這就是你編寫程序的原因。 –
這是否意味着我不需要一個哨兵呢? – vincelam1998