Kevin Schultz
Kevin Schultz

Reputation: 884

Error trying to call string variable

I am writing a c++ program that takes user input for car type, days rented, and miles driven, calculates the variables against constant values and produces a report. I have written the code with a if statement that looks at the carType, "f" or "c" entered by the user and performs calculations from that input. The output however needs to show the name of the vehicle, ford or chevrolet not the entered "f" or "c".

I am getting an error while trying to take the letter and equate it to the brand. Also, I am getting headers for each entry made by the user, how can I take 5 entries and output them under one heading?

Here is my code:

#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");
}

Thanks in advance!

Upvotes: 0

Views: 95

Answers (1)

John3136
John3136

Reputation: 29265

brand is of type char, but you assign strings to it. brand needs to be a string.

I'd also suggest to get into the habit of better naming conventions: c and f are not good choices. Also consider scalability: what if you add toyota, mazda, Ferrari etc?

Upvotes: 1

Related Questions