我正在编写一个c ++程序,它接受用户输入汽车类型,租用天数和里程数,根据常量值计算变量并生成报告。我用if语句编写代码来查看carType," f"或" c"由用户输入并从该输入执行计算。然而输出需要显示车辆的名称,福特或雪佛兰,而不是输入的" f"或" c"。
我在尝试接受信件并将其等同于品牌时遇到错误。此外,我正在为用户输入的每个条目获取标题,如何获取5个条目并将其输出到一个标题下?
这是我的代码:
#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, brand;
string 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 Rental Cost\n";
cout << left << setw(13) << brand << left << setw(13) << days << left << setw(13) << miles
<< fixed << setprecision(2) << showpoint << "$" << setw(13) << right << day_Total << "\n\n";
counter++;
}
system ("pause");
}
提前致谢!
答案 0 :(得分:1)
brand
的类型为char,但您为其指定了字符串。 brand
需要是一个字符串。
我还建议养成更好的命名惯例的习惯:c
和f
不是好选择。还要考虑可扩展性:如果你添加丰田,马自达,法拉利等怎么办?