0

我正在编写一个 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");
}

提前致谢!

4

1 回答 1

1

brand是 char 类型,但您为其分配了字符串。brand必须是一个字符串。

我还建议养成更好的命名约定的习惯:cf不是好的选择。还要考虑可扩展性:如果添加丰田、马自达、法拉利等会怎样?

于 2013-02-27T23:27:51.380 回答