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

Exception Handling C++

Discussion in 'Technology' started by tarheelfan_08, Apr 1, 2010.

  1. Apr 1, 2010 at 7:20 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
    Hey guys I am trying to do some work with exception handling and I had to create and array with 100 elements in the main to hold cars in inventory, and create and int numCars to hold number of created cars. Then I had to create a for loop that iterates 10 times and each time it should.

    1) Prompt user for make and store in a string
    2) Prompt user for a model and store it in a string
    3) Prompt user for a color and store it in a string
    4) Prompt user for a year and store it in a int
    5) Prompt user for a mileage and store it in a int
    6) Prompt user for a price and store it in a int
    7) Create a new car with the 1-6 variables
    8) Add 1 to numCars and store the new car in the array using NumCars as the subscript. Then I need to use a loop to print all of the information. While making sure to loop from 1>numCars so it only prints out cars there.


    Can someone please help me with step 8 thats all that I am lacking to having my program complete! Then once that is done I asked the teacher for a few extra things to try to that I can further my knowledge. He sent me a second part to the program that is not for a grade just extra practice. But I have to figure out how to get num 8 before I can practice other stuff. Here is my code so far!

    Class
    Code:
     #include <iostream>
    #include <fstream>
    #include <iomanip>
    #include <functional>
    #include <algorithm>
    #include <string>
    #include <cstdlib>
    #include <sstream>
    using namespace std;
    
    //Car Class
    class Car
    {
    protected:
        string make;  //make
        string model; // model
        string color;  // color
        int year;  // year
        int mileage;  // miles on car
        int price; // price of car
    
    public:
                    //Constructor that will set information for a new car
            void New_vehicle (string a, string b, string c, int d, int e, int f) 
            {make = a; model = b; color = c; year = d; mileage = e; price = f;}
            
            Car(); //Default constructor
            Car(string, string, string, int, int, int);
            //mutator and accessor functions
            void setMake(string);
            void setModel(string);
            void setColor(string);
            void setYear(int);
            void setMileage(int);
            void setPrice(int);
    
            string getMake();
            string getModel();
            string getColor();
            int getYear();
            int getMileage();
            int getPrice();
    
                //Check mileage to see if valid
            void valid_mileage(int);
            void car_details();
            string string_car_details();
    
            //Exception Classes
            class YearInvalid
            { };  //Empty Class Declaration
    
            class MilesInvalid
            { }; //Empty Class Declaration
    
            class PriceInvalid
            { };  //Empty Class Declaration
    };
    
    //Sets to default values
    Car::Car() {
            make = " ";
            model = " ";
            color = " ";
            year = 0;
            mileage = 0;
            price = 0;
    }
            // My Vehicle set up(Make, model, color, year, mileage)
    Car::Car(string make, string model, string color, int year, int mileage, int price) {
        this->make =  make;
        this->model = model;
        this->color = color;
        this->year = year;
        this->price = price;
        valid_mileage(mileage);
    }
    
    
    void Car::setMake(string make) {
        Car::make = make;
    }
    
    void Car::setModel(string model) {
        Car::model = model;
    }
    
    void Car::setColor(string color) {
        Car::color = color;
    }
    
    void Car::setYear(int year) {
        Car::year = year;
    }
    
    void Car::setMileage(int mileage) {
        valid_mileage(mileage);
    }
    void Car::setPrice(int price) {
        Car::price = price;
    }
    
    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;
    }
    int Car::getPrice() {
        return price;
    }
    
    void Car::valid_mileage(int mileage) {
        if (mileage>=0)
            Car::mileage=mileage;
        else {
            Car::mileage=0;
            cout << "WARNING! You have entered invalid mileage!\n";
            }
        }
    
            void Car::car_details() {
                cout << "The current car is a " << year << ' ' << color << ' '
                            << make << ' ' << model << " with " << mileage << " miles. Price " << price << "\n\n";
            }
    
    
    
            string Car::string_car_details() {
                stringstream buf;
                buf << "The current car is a " << year << ' ' << color << ' '
                << make << ' ' << model << " with " << mileage << " miles. Price " << price << "\n\n";
                return buf.str();
            }
    
    CPP
    Code:
    #include "CarClass.h"
    #include <vector>  // this allows user to create a vector
    using namespace std;
    
    int main() {
        
            Car Car[100]; // sets amount of cars
            int numCars =0;
            string tempString;
            int tempInt;
    
            {
                for (int i = 0; i <10; i++)
                {
                    cout << "Enter the Make of the Car: ";
                    cin >> tempString;
                    Car[i].setMake(tempString);
    
                    cout << "Enter the Model of the Car: ";
                    cin >> tempString;
                    Car[i].setModel(tempString);
    
                    cout << "Enter the Color of the Car: ";
                    cin >> tempString;
                    Car[i].setColor(tempString);
    
                    cout << "Enter the Year of the Car: ";
                    cin >> tempInt;
                    Car[i].setYear(tempInt);
    
                    cout << "Enter the Price of the Car: ";
                    cin >> tempInt;
                    Car[i].setPrice(tempInt);
    
                    cout << "__________________________________________________________\n"<< endl;
    
            }
        }
    
     
  2. Apr 2, 2010 at 10:32 PM
    #2
    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
    Here is what I have, how does this look guys??


    Code:
    #include <iostream>
    #include <fstream>
    #include <iomanip>
    #include <functional>
    #include <algorithm>
    #include <string>
    #include <cstdlib>
    #include <sstream>
    using namespace std;
    
    //Car Class
    class Car
    {
    protected:
        string make;  //make
        string model; // model
        string color;  // color
        int year;  // year
        int mileage;  // miles on car
        int price; // price of car
    
    public:
                    //Constructor that will set information for a new car
            void New_vehicle (string a, string b, string c, int d, int e, int f) 
            {make = a; model = b; color = c; year = d; mileage = e; price = f;}
            
            Car(); //Default constructor
            Car(string, string, string, int, int, int);
            //mutator and accessor functions
            void setMake(string);
            void setModel(string);
            void setColor(string);
            void setYear(int);
            void setMileage(int);
            void setPrice(int);
    
            string getMake();
            string getModel();
            string getColor();
            int getYear();
            int getMileage();
            int getPrice();
    
                //Check mileage to see if valid
            void valid_mileage(int);
            void car_details();
            string string_car_details();
    
            //Exception Classes
            class YearInvalid
            { };  //Empty Class Declaration
    
            class MilesInvalid
            { }; //Empty Class Declaration
    
            class PriceInvalid
            { };  //Empty Class Declaration
    };
    
    //Sets to default values
    Car::Car() {
            make = " ";
            model = " ";
            color = " ";
            year = 0;
            mileage = 0;
            price = 0;
    }
            // My Vehicle set up(Make, model, color, year, mileage)
    Car::Car(string make, string model, string color, int year, int mileage, int price) {
        this->make =  make;
        this->model = model;
        this->color = color;
        this->year = year;
        this->price = price;
        valid_mileage(mileage);
    }
    
    
    void Car::setMake(string make) {
        Car::make = make;
    }
    
    void Car::setModel(string model) {
        Car::model = model;
    }
    
    void Car::setColor(string color) {
        Car::color = color;
    }
    
    void Car::setYear(int year) {
        Car::year = year;
    }
    
    void Car::setMileage(int mileage) {
        valid_mileage(mileage);
    }
    void Car::setPrice(int price) {
        Car::price = price;
    }
    
    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;
    }
    int Car::getPrice() {
        return price;
    }
    
    void Car::valid_mileage(int mileage) {
        if (mileage>=0)
            Car::mileage=mileage;
        else {
            Car::mileage=0;
            cout << "WARNING! You have entered invalid mileage!\n";
            }
        }
    
            void Car::car_details() {
                cout << "The current car is a " << year << ' ' << color << ' '
                            << make << ' ' << model << " with " << mileage << " miles. Price $" << price << "\n\n";
            }
    
    
    
            string Car::string_car_details() {
                stringstream buf;
                buf << "The current car is a " << year << ' ' << color << ' '
                << make << ' ' << model << " with " << mileage << " miles. Price $" << price << "\n\n";
                return buf.str();
            }
    
    CPP
    Code:
    #include "CarClass.h"
    #include <vector>  // this allows user to create a vector
    using namespace std;
    
    int main() {
        
            Car Car[100]; // sets amount of cars
            int numCars;
            string tempString;
            int tempInt;
    
            {
                for ( numCars=0 ; numCars < 10 ; numCars++ )
                {
                    cout << "Enter the Make of the Car: ";
                    cin >> tempString;
                    Car[numCars].setMake(tempString);
    
                    cout << "Enter the Model of the Car: ";
                    cin >> tempString;
                    Car[numCars].setModel(tempString);
    
                    cout << "Enter the Color of the Car: ";
                    cin >> tempString;
                    Car[numCars].setColor(tempString);
    
                    cout << "Enter the Year of the Car: ";
                    cin >> tempInt;
                    Car[numCars].setYear(tempInt);
    
                    cout << "Enter the Mileage of the Car: ";
                    cin >> tempInt;
                    Car[numCars].setMileage(tempInt);
    
                    cout << "Enter the Price of the Car: ";
                    cin >> tempInt;
                    Car[numCars].setPrice(tempInt);
    
                    cout << "__________________________________________________________\n"<< endl;
    
                }
            }
    
                for(int i=0 ; i <=numCars; i++)
            {
                Car[i].car_details();
            }
    
            return 0;
        }
    
     
  3. Apr 2, 2010 at 11:00 PM
    #3
    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
    Hey guys I have one more question! I am trying to throw some exception handling in and I am having a problem. When an exception occurs the program is still wanting to run...is this normal?? Because I would like for it to say..."Invalid Year entered, please restart program" And it stop there!

    Here is my code
    Code:
    #include "CarClass.h"
    #include <vector>  // this allows user to create a vector
    using namespace std;
    
        
    int YearsInvalid(int tempInt)
        {
            if (tempInt < 1990)
                throw "Error:  Invalid Year Entered.\n";
            else
                return tempInt;
        }
    
    int main() {
    
        
            Car Car[100]; // sets amount of cars
            int numCars;
            string tempString;
            int tempInt;
    
            {
                //Loop that adds cars to array
                for ( numCars=0 ; numCars < 10 ; numCars++ )
                {
                    cout << "Enter the Make of the Car: ";
                    cin >> tempString;
                    Car[numCars].setMake(tempString);
    
                    cout << "Enter the Model of the Car: ";
                    cin >> tempString;
                    Car[numCars].setModel(tempString);
    
                    cout << "Enter the Color of the Car: ";
                    cin >> tempString;
                    Car[numCars].setColor(tempString);
    
                    cout << "Enter the Year of the Car: ";
                    cin >> tempInt;
                    Car[numCars].setYear(tempInt);
                    
                    //Try Catch
    
                        try
                            {
                                YearsInvalid(tempInt);
                            }
                        catch (char *exceptionString)
                            {
                                cout << exceptionString;
                            }
                        cout << "End of program!!\n";
    
    
                    cout << "Enter the Mileage of the Car: ";
                    cin >> tempInt;
                    Car[numCars].setMileage(tempInt);
    
                    cout << "Enter the Price of the Car: ";
                    cin >> tempInt;
                    Car[numCars].setPrice(tempInt);
    
                    cout << "__________________________________________________________\n"<< endl;
    
                }
            }
    
                for(int i=0 ; i <=numCars; i++)
            {
                Car[i].car_details();
            }
    
            return 0;
        }
    
    I have attached my output!!!

    When my code is done I should be able to do this! Test the program and give it 5 valid cars and 5 cars with invalid values. And the loop at the end should print the valid Cars! So I guess I do not need it to start over..but just continue and only store the cars with valid values!

    CommandPrompt.jpg
     

Products Discussed in

To Top