我正在編寫一個家庭作業計劃,該計劃可根據品牌,租用日期和行駛里程計算出租汽車價格。整體而言,該程序除了在用戶被提示要計算的汽車數量時起作用外,該程序在數字超過後繼續提示用戶輸入。另外,對於輸入的第一輛車而言,英里的格式是正確的,但隨後的輸入會改變。計數器不工作?
任何與這兩個問題的幫助將不勝感激!
代碼:
#include <iostream>
#include <string>
#include <sstream>
#include <iomanip>
#include <cmath>
using namespace std;
int main()
{
// Change the console's background color.
system ("color F0");
// Declare the variables.
char carType;
string brand, f("Ford"), c("Chevrolet");
int counter = 0, cars = 0;
double days, miles, cost_Day, cost_Miles, day_Total;
cout << "Enter the number of cars you wish to enter: ";
cin >> cars;
cin.ignore();
while (counter <= cars)
{
cout << "Enter the car type (F or C): ";
cin >> carType;
cin.ignore();
cout << "Enter the number of days rented: ";
cin >> days;
cin.ignore();
cout << "Enter the number of miles driven: ";
cin >> miles;
cin.ignore();
if (carType == 'F' || carType == 'f')
{
cost_Day = days * 40;
cost_Miles = miles * .35;
day_Total = cost_Miles + cost_Day;
brand = f;
}
else
{
cost_Day = days * 35;
cost_Miles = miles * .29;
day_Total = cost_Miles + cost_Day;
brand = c;
}
cout << "\nCar Days Miles Cost\n";
cout << left << setw(12) << brand << right << setw(6) << days << right << setw(8) << miles
<< fixed << showpoint << setprecision (2) << setw(8) << right << "$" << day_Total << "\n\n";
counter++;
}
system ("pause");
}
考慮'而(計數器<汽車)'對於初學者。雖然老實說,我只會'(汽車){... - 汽車; }' – WhozCraig 2013-03-20 18:17:29
int counter = 0,cars = 0;在//聲明變量。 – 2013-03-20 18:17:41
@WhozCraig如果我這樣做,它會不會停止一個迭代短的用戶規定的車輛數量進入? – 2013-03-20 18:18:52