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/