CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 4 of 4
  1. #1
    Join Date
    Jan 2013
    Posts
    2

    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

  2. #2
    GCDEF is offline Elite Member Power Poster
    Join Date
    Nov 2003
    Location
    Florida
    Posts
    12,635

    Re: Need help passing variables

    Please use code tags.

    What is this line supposed to be doing?
    void (*func)(string maker, int year, float price,float mileage, string color);

  3. #3
    Join Date
    Jan 2013
    Posts
    2

    Re: Need help passing variables

    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.

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

  4. #4
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,822

    Re: Need help passing variables

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

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured