cout << "Your car is a: " << yourCar.maker << endl;
cout << fixed << showpoint << setprecision( 2 ) <<
"I'll offer $" << yourCar.price - 100 << " for your car." << endl;
return 0;
}
void getYourCar( CarType & car )
{
cout << "Enter your maker: ";
cin >> car.maker;
cout << "Enter the year: ";
cin >> car.year;
cout << "Enter the price: $";
cin >> car.price;
}
now the book says to take the following program and add a member
function to the CarType class which prints the values of all of its data
members. Add two more data members which are relevant for cars. Add the use of
these data members to the program (to the assignment statements for MyCar, to
the operator prompt and input inside the getYourCar function, and to the print
function you have created).
Here is my code. Whenever I run it, it takes my assigned variables in MyCar and prints those instead of the one which the user is inputting.
void getYourCar( CarType & car )
{
cout << "Enter your maker for example Chevy or Ford: ";
cin >> car.maker;
cout << "Enter the year: ";
cin >> car.year;
cout << "Enter the price: $";
cin >> car.price;
cout << "Enter the mileage on the vehicle";
cin >> car.mileage;
cout << "Enter the color of the vehicle";
cin >> car.color;
}
void carData(string maker, int year, float price,float mileage, string color)
{
cout << "Your car is a: " << maker << endl;
cout << "Your vehicle has " << mileage << "on it" <<endl;
cout << "And the color is " << color << "wow" <<endl;
cout << fixed << showpoint << setprecision( 2 ) <<
"I'll offer $" << price - 100 << " for your car." << endl;
}
If anyone can point out what I am doing wrong, and explain why it is that way it would mean so much. I am not asking anyone to write my code. I am just trying to learn.
Sorry for not using code tags. I do not know how. I tried the # signs before and after. How do I do
code tags so I know for the future. But I figured out my code problem.
cout << "Your car is a: " << maker << endl;
cout << "Your vehicle has " << mileage << "on it" <<endl;
cout << "And the color is " << color << "wow" <<endl;
cout << fixed << showpoint << setprecision( 2 ) <<
"I'll offer $" << price - 100 << " for your car." << endl;
}
};
void getYourCar( CarType & car );
int main( )
{
CarType myCar, yourCar;
myCar.maker = "Hyundai"; // This is my vechicle
myCar.year = 2012;
myCar.price = 45567.75;
myCar.mileage = 9000; // added information
myCar.color = "green"; // added information(my favorite color is green)
getYourCar(yourCar);
yourCar.printCarData();
system("PAUSE");
return 0;
}
void getYourCar( CarType & car )
{
cout << "Enter your maker for example Chevy or Ford: ";
cin >> car.maker;
cout << "Enter the year: ";
cin >> car.year;
cout << "Enter the price: $";
cin >> car.price;
cout << "Enter the mileage on the vehicle";
cin >> car.mileage;
cout << "Enter the color of the vehicle";
cin >> car.color;
}
To use code tags, Click 'Go Advanced', select code to be included then click the '#' button. Also your code needs to be formatted with indents etc before being posted so that it is readable.
Code:
#include <iostream>
#include <iomanip>
#include <string>
using namespace std;
struct CarType {
string maker;
int year;
float price;
string color;
float mileage;
void printCarData() {
cout << "Your car is a: " << maker << endl;
cout << "Your vehicle has " << mileage << "on it" <<endl;
cout << "And the color is " << color << "wow" <<endl;
cout << fixed << showpoint << setprecision( 2 ) <<
"I'll offer $" << price - 100 << " for your car." << endl;
}
};
void getYourCar(CarType& car);
int main()
{
CarType myCar,
yourCar;
myCar.maker = "Hyundai"; // This is my vechicle
myCar.year = 2012;
myCar.price = 45567.75;
myCar.mileage = 9000; // added information
myCar.color = "green"; // added information(my favorite color is green)
getYourCar(yourCar);
yourCar.printCarData();
system("PAUSE");
return 0;
}
void getYourCar(CarType& car)
{
cout << "Enter your maker for example Chevy or Ford: ";
cin >> car.maker;
cout << "Enter the year: ";
cin >> car.year;
cout << "Enter the price: $";
cin >> car.price;
cout << "Enter the mileage on the vehicle";
cin >> car.mileage;
cout << "Enter the color of the vehicle";
cin >> car.color;
}
Bookmarks