1. Welcome to Tacoma World!

    You are currently viewing as a guest! To get full-access, you need to register for a FREE account.

    As a registered member, you’ll be able to:
    • Participate in all Tacoma discussion topics
    • Communicate privately with other Tacoma owners from around the world
    • Post your own photos in our Members Gallery
    • Access all special features of the site

C++ Help

Discussion in 'Technology' started by tarheelfan_08, Jan 28, 2010.

  1. Jan 28, 2010 at 8:12 PM
    #1
    tarheelfan_08

    tarheelfan_08 [OP] Carolina Alliance

    Joined:
    Jan 4, 2008
    Member:
    #4098
    Messages:
    2,944
    Gender:
    Male
    First Name:
    Justin
    North Carolina
    Vehicle:
    2010 White 4x4 Double Cab Tacoma Short Bed
    Chrome Grill, Chrome accessories(door handles and mirror covers), LEER Super White Cover, Mirrors with Turn Signals, Window Vent Visors, Step Rails, WeatherTech Digital Floor Mats, Mini Maglight Holder, Chrome Bull Bar
    When I run this program, my cout and cin are all showing up with red underlines. And the error is "Declaration has no Storage"! Can someone please help me get this going. What the program is trying to do us use a class and get the information about a car then print the information. I am new to classes and I may have messed up!
    Code:
    #include <iostream>
    #include <functional>
    #include <algorithm>
    #include <cstring>
    #include <cctype>
    using namespace std;
    
        //Class Declaration
        class Car
        {
            private:
                string make;  //make
                string    model; // model
                string    color;  // color 
                string liscensePlate;  // liscense plate number
                int    year;  // year
                int mileage;  // miles on car
    
            public:
                Car(); //constructor            
                string getMake;
                string getModel;
                string getColor;
                string getLiscensePlate;
                int getYear;
                int getMileage;
                void validation(int);
                void printCarDetails();
                void CarDetails ( string, string, string, string, int, int);
    
        };
    
    
        // Constructor to initialize each object to always start the same way
        Car::Car()
        {
            make = ""; 
            model = ""; 
            color = "";   
            liscensePlate = "";  
            year = 0;
            mileage = 0;
        }
        
    
        
        cout << "What is the car make? ";
        cin >> make;
        cout << "What the car model? ";
        cin >> model;
        cout << "What year is the car? ";
        cin >> year;
        cout << "What color is the car? ";
        cin >> color;
        cout << "What is the liscense plate number? ";
        cin >> liscensePlate;
        cout << "How many miles does the car have on it? ";
        cin >> mileage;
        while (mileage < 0 )
        {    
            cout << " This is not a valid mileage.\n";
            cout << " Enter the miles for this car: ";
            cin >> mileage;
        }
    
        cout << "The current car is a " << year << " " << color << " " << model << " with " << miles << " miles!  And the Liscense Plate number is " << liscensePlate << endl; 
    
        return 0;
    
    
     
  2. Jan 29, 2010 at 6:43 AM
    #2
    XrunnIT

    XrunnIT Well-Known Member

    Joined:
    Jul 6, 2008
    Member:
    #7770
    Messages:
    845
    Gender:
    Male
    Austin, TX
    Vehicle:
    1GR-GZE X-Runner
    Built Motor by LC Engineering Boosted by TRD
    I haven't coded in C++ in a LONG time, but it looks like your missing your int Main().

    You will need to call your class from a function. in this case your int Main() is going to be the main function the program runs in.
     
  3. Jan 29, 2010 at 9:02 AM
    #3
    Chickenmunga

    Chickenmunga Nuggety

    Joined:
    Apr 10, 2008
    Member:
    #5877
    Messages:
    7,574
    Gender:
    Male
    First Name:
    Mike
    Keizer, Oregon
    Vehicle:
    08 TRD Offroad DC 4x4 with stuff
    All the normal TW BS
    It's been about 4+ years for me too. Here we go:



    You are missing the int Main()

    you spelled license wrong

    Why are you including <functional> and <algorithm>? Unless those are required by your compiler or you are adding more functionality, I don't think you need them. They add processing time and cause your executable to grow.

    You have two constructors listed. Was this your intent?

    Your getter's are being incorrectly defined as variables instead of functions, you need ().

    You need setters. SetColor, SetYear, and so on.

    you need to define your functions.

    within int main(), you need to create a car instance. Don't forget to delete it at the end!

    you need a destructor

    your last cout statement should really be inside your printDetails()

    Once you get all that in place, you'll most likely find some more elegant approaches to your cin/cout steps.


    If you don't have one already, it sounds like you need some good reading material. I suggest C++ How to Program by Dietel & Dietel. There's possibly better books, but this is the one I had. They are up to their 7th edition, but you can pick up the 5th edition for half the cost. The language hasn't changed, so there's nothing you would necessarily miss in the new book.

    Another good source is Microsoft, who does a pretty good job with tutorials, code galleries, and most of all, the MSDN library. The MSDN library is basically your giant electronic tome of knowledge. Here's a sample, I searched for "cin".

    What IDE are you using? If your class hasn't specified something, I suggest using Microsoft Visual C++ Express. It's an industry-level programming atmosphere that's free to use. It has some really awesome tools that can make coding actually really fun - autocomplete, autosense, a really nice debugger, and direct tie-in with the MSDN Library.

    There's other IDEs (Eclipse, Code::Blocks, others), Visual Studio just happens to be what I'm familiar with.
     
  4. Jan 29, 2010 at 9:18 AM
    #4
    jimker55

    jimker55 Well-Known Member

    Joined:
    Nov 13, 2009
    Member:
    #25877
    Messages:
    149
    Gender:
    Male
    First Name:
    Jim
    Scottsdale, AZ
    Vehicle:
    2007 Taco
    now I remember why I got out of programming. looks like Chickenmunga has some good points to check out to solve your problems. good luck!
     
  5. Jan 29, 2010 at 9:26 AM
    #5
    Chickenmunga

    Chickenmunga Nuggety

    Joined:
    Apr 10, 2008
    Member:
    #5877
    Messages:
    7,574
    Gender:
    Male
    First Name:
    Mike
    Keizer, Oregon
    Vehicle:
    08 TRD Offroad DC 4x4 with stuff
    All the normal TW BS
    That's why I'm an analyst/implementer now :D

    I make promises, the developers have to make them reality :laugh:



    For anyone reading this, I recommend reading this as well. More important than anything else I've said here.
     
  6. Jan 30, 2010 at 1:02 PM
    #6
    tarheelfan_08

    tarheelfan_08 [OP] Carolina Alliance

    Joined:
    Jan 4, 2008
    Member:
    #4098
    Messages:
    2,944
    Gender:
    Male
    First Name:
    Justin
    North Carolina
    Vehicle:
    2010 White 4x4 Double Cab Tacoma Short Bed
    Chrome Grill, Chrome accessories(door handles and mirror covers), LEER Super White Cover, Mirrors with Turn Signals, Window Vent Visors, Step Rails, WeatherTech Digital Floor Mats, Mini Maglight Holder, Chrome Bull Bar
    OK, this may just be a completly dumb question but I have added my Int Main() and now my variables in my cin are not working. To get this program to work with the class should I do anything special or just add the variale names like normal after the Int main().


    This is what I have.

    Code:
    #include <iostream>
    #include <functional>
    #include <algorithm>
    #include <cstring>
    #include <cctype>
    using namespace std;
    
        //Class Declaration
        class Car
        {
            private:
                string make;  //make
                string    model; // model
                string    color;  // color 
                string liscensePlate;  // liscense plate number
                int    year;  // year
                int mileage;  // miles on car
    
            public:
                Car(); //constructor            
                string getMake;
                string getModel;
                string getColor;
                string getLiscensePlate;
                int getYear;
                int getMileage;
                void printCarDetails();
                void CarDetails ( string, string, string, string, int, int);
    
        };
    
    
        // Constructor to initialize each object to always start the same way
        Car::Car()
        {
            make = ""; 
            model = ""; 
            color = "";   
            liscensePlate = "";  
            year = 0;
            mileage = 0;
        }
        
        
    int main()
    {    
    
        cout << "What is the car make? ";
        cin >> make;
        cout << "What the car model? ";
        cin >> model;
        cout << "What year is the car? ";
        cin >> year;
        cout << "What color is the car? ";
        cin >> color;
        cout << "What is the liscense plate number? ";
        cin >> liscensePlate;
        cout << "How many miles does the car have on it? ";
        cin >> mileage;
        while (mileage < 0 )
        {    
            cout << " This is not a valid mileage.\n";
            cout << " Enter the miles for this car: ";
            cin >> mileage;
        }
        
        cout << "The current car is a " << year << " " << color << " " << model << " with " << miles << " miles!  And the Liscense Plate number is " << liscensePlate << endl; 
    
    return 0;            
    }
    

    This is what I am talking about doing( idk if this is right or wrong)

    Code:
    #include <iostream>
    #include <functional>
    #include <algorithm>
    #include <cstring>
    #include <cctype>
    using namespace std;
    
        //Class Declaration
        class Car
        {
            private:
                string make;  //make
                string    model; // model
                string    color;  // color 
                string liscensePlate;  // liscense plate number
                int    year;  // year
                int mileage;  // miles on car
    
            public:
                Car(); //constructor            
                string getMake;
                string getModel;
                string getColor;
                string getLiscensePlate;
                int getYear;
                int getMileage;
                void printCarDetails();
                void CarDetails ( string, string, string, string, int, int);
    
        };
    
    
        // Constructor to initialize each object to always start the same way
        Car::Car()
        {
            make = ""; 
            model = ""; 
            color = "";   
            liscensePlate = "";  
            year = 0;
            mileage = 0;
        }
        
        
    int main()
    {    
    
                string make;  //make
                string    model; // model
                string    color;  // color 
                string liscensePlate;  // liscense plate number
                int    year;  // year
                int mileage;  // miles on car
    
    
        cout << "What is the car make? ";
        cin >> make;
        cout << "What the car model? ";
        cin >> model;
        cout << "What year is the car? ";
        cin >> year;
        cout << "What color is the car? ";
        cin >> color;
        cout << "What is the liscense plate number? ";
        cin >> liscensePlate;
        cout << "How many miles does the car have on it? ";
        cin >> mileage;
        while (mileage < 0 )
        {    
            cout << " This is not a valid mileage.\n";
            cout << " Enter the miles for this car: ";
            cin >> mileage;
        }
        
        cout << "The current car is a " << year << " " << color << " " << model << " with " << mileage << " miles!  And the Liscense Plate number is " << liscensePlate << endl; 
    
    return 0;            
    }
    
    but if i do this then I get the following error. No operator ">>" matches these opperands
     
  7. Jan 30, 2010 at 1:58 PM
    #7
    Evil Monkey

    Evil Monkey There's an evil monkey in my truck

    Joined:
    Aug 8, 2007
    Member:
    #2352
    Messages:
    8,262
    Gender:
    Male
    First Name:
    Robert
    Escondido, CA
    Vehicle:
    07 4x4 DC SR5 TRD Off-road
    Weathertech front & rear mats, rear suspension TSB, Toytec AAL for TSB, Hi-Lift Jack, Bilstein 5100 & Toytec Adjustable coilovers, Built Right UCAs, KMC XD 795 Hoss Wheels, Definity Dakota MTs 285/75R16, Leer XR, Thule Tracker II & Thule MOAB basket
    A couple of things. You're not using the class. Also, your getters and setters are still not initialized correctly (they'll simply be attributes).
    e.g string getModel; should be string getModel(); as it's a function that returns a string.

    For your cin problem, change your strings to char[40] and see if it will work. If it does, you may be using a string library that doesn't support cin, which is why you get the >> error. The operator >> isn't overloaded for strings so it doesn't know what you're trying to do.
     
  8. Jan 30, 2010 at 2:00 PM
    #8
    tarheelfan_08

    tarheelfan_08 [OP] Carolina Alliance

    Joined:
    Jan 4, 2008
    Member:
    #4098
    Messages:
    2,944
    Gender:
    Male
    First Name:
    Justin
    North Carolina
    Vehicle:
    2010 White 4x4 Double Cab Tacoma Short Bed
    Chrome Grill, Chrome accessories(door handles and mirror covers), LEER Super White Cover, Mirrors with Turn Signals, Window Vent Visors, Step Rails, WeatherTech Digital Floor Mats, Mini Maglight Holder, Chrome Bull Bar
    Ok guys I had to make some changes based on what the teacher wants....and here is my new code.

    Code:
    #include <iostream>
    #include <string>
    #include <functional>
    #include <algorithm>
    #include <cctype>
    using namespace std;
    
        //Class Declaration
        class Car
        {
            private:
                string make;  //make
                string    model; // model
                string    color;  // color 
                string liscensePlate;  // liscense plate number
                int    year;  // year
                int mileage;  // miles on car
    
            public:
                Car(); //constructor            
                void printCarDetails();
                void CarDetails ( string, string, string, string, int, int);
    
        };
    
    
        // Constructor to initialize each object to always start the same way
        Car::Car()
        {
            make = ""; 
            model = ""; 
            color = "";   
            liscensePlate = "";  
            year = 0;
            mileage = 0;
        }
        
        void Car::CarDetails(string make, string model, string year, string color, int liscensePlate, int mileage)
    
        {
        cout << "What is the car make? ";
        cin >> make;
        cout << "What the car model? ";
        cin >> model;
        cout << "What year is the car? ";
        cin >> year;
        cout << "What color is the car? ";
        cin >> color;
        cout << "What is the liscense plate number? ";
        cin >> liscensePlate;
        cout << "How many miles does the car have on it? ";
        cin >> mileage;
        while (mileage < 0 )
        {    
            cout << " This is not a valid mileage.\n";
            cout << " Enter the miles for this car: ";
            cin >> mileage;
        }
        }
    
        void Car::printCarDetails()
        {
        cout << "The current car is a " << year << " " << color << " " << model << " with " << mileage << " miles!  And the Liscense Plate number is " << liscensePlate << endl; 
        }
    return 0;            
    }
    
    but it will not run for some reason. My 2 errors I am getting are with the return 0 and the final }. The return is expecting a declaration and the } is expecting a declaration! Sorry for this change but please advise me as to what I did wrong. I may have missed something small but if I did I do not see it at all!
     
  9. Jan 30, 2010 at 2:06 PM
    #9
    Evil Monkey

    Evil Monkey There's an evil monkey in my truck

    Joined:
    Aug 8, 2007
    Member:
    #2352
    Messages:
    8,262
    Gender:
    Male
    First Name:
    Robert
    Escondido, CA
    Vehicle:
    07 4x4 DC SR5 TRD Off-road
    Weathertech front & rear mats, rear suspension TSB, Toytec AAL for TSB, Hi-Lift Jack, Bilstein 5100 & Toytec Adjustable coilovers, Built Right UCAs, KMC XD 795 Hoss Wheels, Definity Dakota MTs 285/75R16, Leer XR, Thule Tracker II & Thule MOAB basket
    No main(). You're returning 0 so it's looking for a function declaration that returns an integer.

    You need an int main(){

    }
    Inside the main, you declare an instance of your class and then run the functions using the class.

    for example,

    Car myCar = new Car();
    myCar.CarDetails();
    myCar.printCarDetails();

    The way you have carDetails written, you don't need to pass in variables as the Car class already has it's own and you're getting your input from cin. A better way is to pass in the variables using the function declaration like you did, but enter the variables in the main and then calling the class function instead of using cin/cout inside the class' function.

    So in main, after you declare an instance of the class, you would call the function carDetails.
    e.g.
    myCar.carDetails("Toyota", "Tacoma", "2007", "Silver Streak Mica", 7878777, 32000);
    or if you're going to use cin, declare some variables in the main, initialize them with cin and then pass them to the class

    e.g.
    cout<<"Enter the make:"
    cin>>make;
    cout<<"Enter the model:"
    cin>>model;
    cout<<"Enter the year:"
    cin>>year;
    cout<<"Enter the license plate:"
    cin>>licensePlate;
    cout<<"Enter the mileage:"
    cin>>mileage;
    myCar.carDetails(make, model, year, licensePlate, mileage);

    myCar.printCarDetails();
    return 0;


    Also, your license plate should be a string variable as it's possible to have letters in the plate number. You might do the same for the mileage as the user may put commas in their mileage unless you specify numbers only in the cout statement.

    Realize too that the variables you're passing in the CarDetails function are pass by value. Once the function exits, they'll go away along with any data you've entered into them. As a result, your print function will only have empty strings. As a result you should assign them to your class variables inside carDetails.
    e.g. this.licensePlate = licensePlate;

    It's also good practice to use the this operator when sending data to your member variables for clarity. It distinguishes a class' member variable vs a passed in variable.

    e.g. cin>>this.make; means use the class' member variable named make.

    Some also encourage using m_ before all class member variables. It makes it more clear as to which variable you're trying to initialize.
    e.g.
    string m_make;
    string m_model;
    int m_year;

    Also, make sure your functions' names are done in a consistent manner. Only class names should start with a capital letter. No capital letter at the beginning of a function. So CarDetails should be carDetails() like your printCarDetails() function.
     
  10. Jan 30, 2010 at 3:53 PM
    #10
    tarheelfan_08

    tarheelfan_08 [OP] Carolina Alliance

    Joined:
    Jan 4, 2008
    Member:
    #4098
    Messages:
    2,944
    Gender:
    Male
    First Name:
    Justin
    North Carolina
    Vehicle:
    2010 White 4x4 Double Cab Tacoma Short Bed
    Chrome Grill, Chrome accessories(door handles and mirror covers), LEER Super White Cover, Mirrors with Turn Signals, Window Vent Visors, Step Rails, WeatherTech Digital Floor Mats, Mini Maglight Holder, Chrome Bull Bar
    I do not exactly get what you are saying with the main stuff... I know it needs to be there but I have no clue where to put it and then idk what you mean by declaring and instance of my class!
     
  11. Jan 30, 2010 at 3:57 PM
    #11
    tarheelfan_08

    tarheelfan_08 [OP] Carolina Alliance

    Joined:
    Jan 4, 2008
    Member:
    #4098
    Messages:
    2,944
    Gender:
    Male
    First Name:
    Justin
    North Carolina
    Vehicle:
    2010 White 4x4 Double Cab Tacoma Short Bed
    Chrome Grill, Chrome accessories(door handles and mirror covers), LEER Super White Cover, Mirrors with Turn Signals, Window Vent Visors, Step Rails, WeatherTech Digital Floor Mats, Mini Maglight Holder, Chrome Bull Bar
    Ok guys I got the first part I think, and now I am on to this second part. I got no clue what to do please help again!



    Create a program with a main() that uses the car class. It is going to store a set of cars for the user in an array. Write a program that creates an array of 5 cars, each car in the array is a car in a service bay at this Automotive Service Store.
    Go ahead and create all 5 cars in the array using the default constructor.
    Have the following menu for the user to work with:
    1. Choose Service Bay (1-5)
    2. Change make of car in the current Service Bay
    3. Change model of car in the current Service Bay
    4. Change color of car in the current Service Bay
    5. Change year of car in the current Service Bay
    6. Change mileage of car in the current Service Bay
    7. Display car in current service bay
    8. Save
    9. Exit
    Place this menu in a loop like you used for the review for week 1. When the user enters 9 it should end the loop and the program.
    Have the other choices by the user do the following.
    1. Should change the value of an integer you are using to show the current position in the array of cars the user is working with. Prompt the user for a value and store 0-4 (whatever number the user gives you…subtract 1 from it because arrays start at 0.)
    2. Have this option display the make of the car in the current service bay for the user using the accessor function. Then prompt the user for the new value of the car. Use the mutator function
    Example of output:
    The make of the car in the current service bay is Ford.
    Please Enter the change to the make of the car: Ferrarri
    (The value of the make of the car in the current place in the array is now Ferrarri.)
    3. Have this option display the model of the car in the current service bay for the user using the accessor function. Then prompt the user for the new value of the car. Use the mutator function. (This is the same as 2., except changing the model.)
    4. Have this option display the color of the car in the current service bay for the user using the color function. Then prompt the user for the new value of the car. Use the mutator function. (This is the same as 2., except changing the model.)
    5. Have this option display the year of the car in the current service bay for the user using the year function. Then prompt the user for the new value of the car. Use the mutator function. (This is the same as 2., except changing the model.)
    6. Have this option display the mileage of the car in the current service bay for the user using the mileage function. Then prompt the user for the new value of the car. Use the mutator function. (This is the same as 2., except changing the model.)
    7. Have this option call the car_details function for the current car. (Which will display the details of the car for the user.)
    8. Have this option write all of the data for each car in the array to a data file. (Open a data file as you know how to do, and then use a loop and loop through all 5 positions in the array and write each of the attributes of each car using its set functions to the data file and then close it.)
    9. This option will end the program.
     
  12. Jan 30, 2010 at 5:32 PM
    #12
    tarheelfan_08

    tarheelfan_08 [OP] Carolina Alliance

    Joined:
    Jan 4, 2008
    Member:
    #4098
    Messages:
    2,944
    Gender:
    Male
    First Name:
    Justin
    North Carolina
    Vehicle:
    2010 White 4x4 Double Cab Tacoma Short Bed
    Chrome Grill, Chrome accessories(door handles and mirror covers), LEER Super White Cover, Mirrors with Turn Signals, Window Vent Visors, Step Rails, WeatherTech Digital Floor Mats, Mini Maglight Holder, Chrome Bull Bar
  13. Jan 30, 2010 at 7:07 PM
    #13
    Evil Monkey

    Evil Monkey There's an evil monkey in my truck

    Joined:
    Aug 8, 2007
    Member:
    #2352
    Messages:
    8,262
    Gender:
    Male
    First Name:
    Robert
    Escondido, CA
    Vehicle:
    07 4x4 DC SR5 TRD Off-road
    Weathertech front & rear mats, rear suspension TSB, Toytec AAL for TSB, Hi-Lift Jack, Bilstein 5100 & Toytec Adjustable coilovers, Built Right UCAs, KMC XD 795 Hoss Wheels, Definity Dakota MTs 285/75R16, Leer XR, Thule Tracker II & Thule MOAB basket
    The main should go outside of your class declaration (typically at the end).[

    so
    Code:
    class Car {
     //private variables
     //constructors
     //destructor
     //class functions
    };
    int main(){
     //do useful things with your Car type.
    return 0;
    }
    
    The main section is the program that runs (like your first helloworld program). All code you generate has to be run in a main somewhere.

    The class declaration is the creation of a new type. Just like you have ints, strings, longs and doubles, now you have a new type called Car. Like any type, it has to be declared and initialized before you can use it.
    You declare an int as such:
    int myVariable = 0;

    here you've created a variable called myVariable of type int that can have integers assigned to it.

    In like manner, you have to create an instance of your class before you can use it.
    Car myCar = new Car();
    Here I've created a variable called myCar which is of type Car which uses the default constructor. The new operator puts the class in memory and creates a pointer on the stack.

    All this is done in the main section (the program that runs).

    One mistake I made in my earlier sample is that the functions are called with the -> operator instead of the . operator when using new.
    So myCar->printCarDetails() vs myCar.printCarDetails();

    This is done when you use the new operator. (perhaps you're not there yet).
    If you declare it without new, then you use the . operator.
    Car myCar = Car();
    myCar.printCarDetails();

    Hopefully I haven't confused you further.
     
  14. Jan 30, 2010 at 9:47 PM
    #14
    tarheelfan_08

    tarheelfan_08 [OP] Carolina Alliance

    Joined:
    Jan 4, 2008
    Member:
    #4098
    Messages:
    2,944
    Gender:
    Male
    First Name:
    Justin
    North Carolina
    Vehicle:
    2010 White 4x4 Double Cab Tacoma Short Bed
    Chrome Grill, Chrome accessories(door handles and mirror covers), LEER Super White Cover, Mirrors with Turn Signals, Window Vent Visors, Step Rails, WeatherTech Digital Floor Mats, Mini Maglight Holder, Chrome Bull Bar
    Had a class mate assist me and I managed to get the first part....here it is!

    Code:
    
    
    #include <iostream>
    #include <string>
    using namespace std;
    class Car {
    private:
        string make;  //make
        string model; // model
        string color;  // color
        int    year;  // year
        int mileage;  // miles on car
    
    public:
        Car(); //constructor
        Car(string, string, string, int ,int);
        string getMake();
        string getModel();
        string getColor();
        int getYear();
        int getMileage();
        void car_details();
    };
    
    int main() {
        Car myCar("Ford", "Mustang", "Blue", 2010, 7500);
        myCar.car_details();
        return 0;
    }
    //----------------------------------------------------------------
    //class definition..
    Car::Car() {
        make = " ";
        model = " ";
        color = " ";
        year = 0;
        mileage = 0;
    }
    
    Car::Car(string make, string model, string color, int year, int mileage) {
        Car::make =  make;
        Car::model = model;
        Car::color = color;
        Car::year = year;
        Car::mileage = mileage;
    }
    
    string Car::getMake() { return make; }
    string Car::getModel() { return model; }
    string Car::getColor() { return color; }
    int Car::getYear() { return year; }
    int Car::getMileage() { return mileage; }
    
    void Car::car_details() {
        cout << "The current car is a " << year << ' ' << color << ' '
        << make << " with " << mileage << " miles..\n";
    }
    
    
    
    


    Now if I am correct, I should just have to add 4 more cars and then my menu to this program and I will have my second program. Please tell me if I am thinking about this wrong!
     
  15. Jan 31, 2010 at 7:32 AM
    #15
    Evil Monkey

    Evil Monkey There's an evil monkey in my truck

    Joined:
    Aug 8, 2007
    Member:
    #2352
    Messages:
    8,262
    Gender:
    Male
    First Name:
    Robert
    Escondido, CA
    Vehicle:
    07 4x4 DC SR5 TRD Off-road
    Weathertech front & rear mats, rear suspension TSB, Toytec AAL for TSB, Hi-Lift Jack, Bilstein 5100 & Toytec Adjustable coilovers, Built Right UCAs, KMC XD 795 Hoss Wheels, Definity Dakota MTs 285/75R16, Leer XR, Thule Tracker II & Thule MOAB basket
    The main should go outside the class. Your constructors and function implimentations need to go inside the class. You don't need to prototype the functions in the class when your main is in the same code as your class. Later when you separate out your class from the main (separate file) you'll use .h files as prototypes and import them into your main program.

    I've rearranged the code below. I also added a destructor. You can get away with not having one since it will destruct by default but it's better to get used to putting one in. You'll control how a object destroys itself when delete is called. It's important to help prevent memory leaks. Also, you might consider returning a string for printCarDetails instead of using cout in the class. Save the cout for use in your main function. The reason for this is it makes your class more useable because you can capture the output of the class for use elsewhere (e.g. in Windows programming, you might want to send it to a textbox. If you cout, you have no way of getting the final string and the cout will only dump to the console window, which an end user of the program won't see). Note the use of the this in the constructors vs how you were trying to initialize the member variables:
    Code:
    #include <iostream>
    #include <string>
    using namespace std;
    class Car {
    private:
        string make;  //make
        string model; // model
        string color;  // color
        int    year;  // year
        int mileage;  // miles on car
     
    public:
         //----------------------------------------------------------------
         //class constructors.
         Car::Car() {
             make = " ";
             model = " ";
             color = " ";
             year = 0;
             mileage = 0;
         }
     
         Car::Car(string make, string model, string color, int year, int mileage) {
             this.make =  make;
             this.model = model;
             this.color = color;
             this.year = year;
             this.mileage = mileage;
         }
         //destructor
         Car::~Car() {
         }
     
         string Car::getMake() { return make; }
         string Car::getModel() { return model; }
         string Car::getColor() { return color; }
         int Car::getYear() { return year; }
         int Car::getMileage() { return mileage; }
     
        //renamed to match your other function calls.  Keep your formats similar to avoid confusion.
        string Car::getCarDetails() {
             //I don't remember how c++ strings are concatenated so change as necessary.
             string details = "The current car is a " + year + " " + color + " " + make + " "  
                                    + model + " with " + mileage + " miles. \n";
             return details;
         }
     
    };
     
    int main() {
        //see how this is initialized vs how you were doing it
        Car myCar = new Car("Ford", "Mustang", "Blue", 2010, 7500);
     
        //cout is in the main.  myCar->getCarDetails() returns a string so it's like cout<<string
        cout<<myCar->getCarDetails();
     
        //anything created with new should be destroyed with delete otherwise you'll have a memory leak.
        delete myCar;
        return 0;
    }
    
    In your main for your project, you'll need to create an array of cars.
     

Products Discussed in

To Top