Need help with C++ code

March 29, 2009 at 15:26:59
Specs: Windows XP
Sorry to bother anyone, but I'm having a problem. I'm coding a rental car rate program for an assignment I have in my programming class, and I just don't understand how to correct what I have to make it work as specified. Here's the assignment:

"In this case project, you will write a program that will assist a rental car agency in tracking cars. In the program, you will define a Car class with the following constructor prototype:

Car(const string vin, const string licensePlate, const double rate);

Define a friend function with the prototype:

double returnCar(const double mileage, Car&);

This function should add the passed-in mileage to the current mileage of the car (which should be initialized to zero in the constructor). The function should return the rental price which is calculated using the passed-in mileage and the rate for the car. Create a main() function which initializes one Car object. Prompt the user to enter a mileage and use the returnCar() function to display the rental price for the car."

I've been over this quite a few times and can't figure out how to do it correctly. Probably just a brain-dump on my part, but hopefully someone can lead me in the right direction. Here's what I have thus far:

(NOTE: I'm using Microsoft Visual C++ Express.)


#include "stdafx.h"

using namespace std;

class Car
friend ostream& operator<<(ostream&, const Car&);
string licensePlate;
double milesDriven;
double rentalRate;
Car(string, double, double);
double operator/(Car);

Car::Car(string plate, double miles, double rate)
licensePlate = plate;
milesDriven = miles;
rentalRate = rate;

double Car::operator/(Car plate)
double rentRate;
rentRate = rentalRate / plate.rentalRate;
return rentRate;

ostream& operator<<(ostream& out, const Car& aCar)
out << "License Plate: " << aCar.licensePlate << endl;
out << "Miles Driven: " << aCar.milesDriven << " miles" << endl;
out << "Rate: " << aCar.rentalRate << " cents per mile" << endl;
return out ;

int main()
Car first("LICENSE", 275, 0.15);
cout << first;
return 0;


My questions are: how do I put user input into it, so instead of automatically being declared by the program, the miles driven (275) and the rate per mile (0.15) can be declared by the user of the program? Also, I need for those two input values to be multiplied so it can display a total cost.

See More: Need help with C++ code

Report •

March 29, 2009 at 15:58:53
You use cout for an ostream, so you can use cin for an istream. The syntax is similar to cout, If you wanted to input for an int, you would write:
int x;
cin >> x; //>> not << like with cout

There are also many other ways to get user input, like the other function of the cin object.

Hope this helps! :)

Report •

March 29, 2009 at 16:04:39
That does help, and it gives me a better understanding of what the ostream and istream are...but how exactly do I implement that into my code? I hate to ask, but my mind is complete blank right now for reasons I'm not understanding.

Report •

March 30, 2009 at 11:11:57
What is getting at is that your function should mirror your operator<< function. In your .H, your declaration is inside the Car class as friend double returnCar(const double, Car&);, while your definition should start as double returnCar(const double mileage, Car& aCar). You should then use aCar instead of this.

Report •

Related Solutions

Ask Question