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

#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

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

		}
	}

Re-post: http://www.dreamincode.net/forums/topic/165645-exception-handling/

In your loop in main(), you are using "i" as the index to the array as you insert items. How could you use "numCars" in the same way, but yet at the end of the loop, have it contain the index of the last item in the array?

Also, your input code is missing the code to ask the user for mileage.

In your loop in main(), you are using "i" as the index to the array as you insert items. How could you use "numCars" in the same way, but yet at the end of the loop, have it contain the index of the last item in the array?

Also, your input code is missing the code to ask the user for mileage.

Not sure I understand what you mean. I may have done something wrong but I am unsure...And thanks for the heads up on the mileage...i fixed it!

Here is what I have, how does this look guys??

#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

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

Hey guys I have one more question! I am trying to throw some exception handling in and I am having a problem.

Here is my 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!

UPDATE:

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!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.