Need help passing variables

I need help passing some variables between functions. I know I am doing it wrong, I just do not know how to fix it.

Here is the first part of the code which does work.



#include <iostream>
#include <iomanip>
#include <string>

using namespace std;

struct CarType {
string maker;
int year;
float price;
};

void getYourCar( CarType & car );

int main( )
{
CarType myCar, yourCar;

myCar.maker = "Mercedes"; // I wish
myCar.year = 2005;
myCar.price = 45567.75;

getYourCar( yourCar );

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.

#include <iostream>
#include <iomanip>
#include <string>

using namespace std;


using namespace std;

struct CarType {
string maker;
int year;
float price;
string color;
float mileage;
void (*func)(string maker, int year, float price,float mileage, string color);
};

void getYourCar( CarType & car );
void carData(string maker, int year, float price,float mileage, string color);


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)
//myCar.func = carData;
getYourCar( yourCar );

myCar.func(myCar.maker, myCar.year, myCar.price, myCar.mileage, myCar.color);

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;
}
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.

Thank you
I can't tell if you're supposed to do this in C or C++. Your assignment asks you to do it in C++ because it asks you to add member functions to your structure, but you've only added a function pointer and you've defined external functions to act on the struct like in C. Member functions are actually functions that are associated with the structure and are called from AnInstanceOfTheStructure.LikeThis(), giving the function access to the data members in your structure.

Try reading the C++ classes tutorial:
http://www.cplusplus.com/doc/tutorial/classes/
The "struct" and "class" keywords are identical except that classes default to things being private and structs default to things being public, unless otherwise specified of course.
Last edited on
Yes, I am trying to accomplish this in C++. I got to the point I was just trying to get it to complie last night. So if I wanted to do a function inside the struct could it just be void carData(parameters)?

What is making it replace the users input data with the assigned data?


Thank you for your reply.
Chris1251 wrote:
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.
Where do you print the values the user entered? I do not see any such lines in your code. Can you point it out?
1
2
3
4
5
6
getYourCar( yourCar );

myCar.func(myCar.maker, myCar.year, myCar.price, myCar.mileage, myCar.color);

system("PAUSE");
return 0;
Last edited on
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;


So I know for the future, how did you get the numbers to show the lines of code?
Code tags, you put stuff [code]between the code tags[/code] to syntax highlight them and add line numbers. You can also select your code and click the <> button on the right under formatting.


As for that code you posted, that is a function definition, it has nothing to do with the yourCar variable. My point is, you never once in your code do anything that involves printing any information about yourCar.

This is your main function entirely:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
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)
    //myCar.func = carData;
    getYourCar( yourCar );

    myCar.func(myCar.maker, myCar.year, myCar.price, myCar.mileage, myCar.color);

    system("PAUSE");
    return 0;
}
There is not a single line that has anything to do with printing yourCar.
Last edited on
Oh ok, I see. How would I call carData() from main? Do I need the parameters?
I don't know how to explain this without making an example...
1
2
3
4
5
6
7
8
9
struct Car
{
    int mpg;

    void PrintInformation()
    {
        std::cout << "Miles per gallon: " << mpg << std::endl;
    }
};
1
2
3
4
5
6
7
int main()
{
    Car c;
    c.mpg = 40;

    c.PrintInformation();
}
Miles per gallon: 40
It's a pretty basic example but hopefully you get the idea: you define the function inside the struct and it can access the data members, then you can call it from an instance of the struct. You don't need to give any information to the function because C++ does that for you.
Last edited on
Thank you for the example. I got it! Thank you for explaining it to me. All I need now is some formatting and it will look good. Now I can move onto chapter 3!
Topic archived. No new replies allowed.