1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196
|
#include <cstdlib>
#include <iostream>
#include <fstream>
using namespace std;
class CarData //defining class "wrapper"
{
private:
double year, mileage;
string make, model, name, phone;
public:
void setYear(double);//mutator functions
void setMileage(double);
void setMake(string);
void setModel(string);
void setName(string);
void setPhone(string);
double getYear();// accessor functions
double getMileage();
string getMake();
string getModel();
string getName();
string getPhone();
void valid_mileage(double);
void car_details();
};
//setYear will assign a value to the private member year
void CarData::setYear(double tempyear)
{
year = tempyear;
}
//setMileage will assign a value to the private member mileage
void CarData::setMileage(double tempmileage)
{
mileage = tempmileage;
}
//setMake will assign a value to the private member make
void CarData::setMake(string tempmake)
{
make = tempmake;
}
//setModel will assign a value to the private member model
void CarData::setModel(string tempmodel)
{
model = tempmodel;
}
//setName will assign a value to the private member name
void CarData::setName(string tempname)
{
name = tempname;
}
//setPhone will assign a value to the private member phone
void CarData::setPhone(string tempphone)
{
phone = tempphone;
}
double CarData::getYear()
{
return year;
}
double CarData::getMileage()
{
return mileage;
}
string CarData::getMake()
{
return make;
}
string CarData::getModel()
{
return model;
}
string CarData::getName()
{
return name;
}
string CarData::getPhone()
{
return phone;
}
void CarData::valid_mileage(double mileage)
{
if (mileage>=0)
CarData::mileage=mileage;
else {
CarData::mileage=0;
cout << "Invalid mileage!\n";
}
}
//main function
int main()
{
CarData customerCar;
int numCar=0, ctr;
double tempyear[numCar], tempmileage[numCar];// local variables
string tempmake[numCar], tempmodel[numCar], tempname[numCar], tempphone[numCar];
cout << "How many new vehicles are being added to the inventory?\n";
cin >> numCar;
if (numCar < 1)
{
system ("PAUSE");
return 0;
}
CarData car[numCar]; //creates an car array of "numCar" amounts of the "CarData4 type
for (ctr = 0; ctr < numCar; ctr++)// for populating the array
{
cout << "\n\nVehicle " << ctr +1; // Helps user keep track vehicle #
cout << "\n\tEnter year #: "; // Input information
cin.ignore();
cin >> tempyear[ctr];
while (tempyear[ctr] < 1910 || tempyear[ctr] > 2014)
{
cout << "Invalid year. Please enter the correct year. ";
cin.clear();
cin.ignore();
cin >> tempyear[ctr];
}
cout << endl;
cout << "\n\tEnter make: "; // Input information
cin.ignore();
getline (cin, tempmake[ctr]);
cout << endl;
cout << "\n\tEnter model: "; // Input information
cin.ignore();
getline (cin, tempmodel[ctr]);
cout << "test";
cout << endl;
cout << "\n\tEnter mileage #: "; // Input information
cin.ignore();
cin >> tempmileage[ctr];
while (tempmileage[ctr] < 0 || tempyear[ctr] > 1000000)
{
cout << "Invalid mileage. Please enter the correct mileage. ";
cin.clear();
cin.ignore();
cin >> tempmileage[ctr];
}
cout << endl;
cout << "\n\tEnter owner's first and last name: (ex. John Smith)"; // Input information
cin.ignore();
getline (cin, tempname[ctr]);
cout << endl;
cout << "\n\tEnter owner's phone #: (ex. (555)555-5555)"; // Input information
getline (cin, tempphone[ctr]);
cout << endl;
}
customerCar.setYear(tempyear[ctr]);
customerCar.setMileage(tempmileage[ctr]);
customerCar.setMake(tempmake[ctr]);
customerCar.setModel(tempmodel[ctr]);
customerCar.setName(tempname[ctr]);
customerCar.setPhone(tempphone[ctr]);
cout << "\n\tName\t|Year\t|Make\t|Model\t|Mileage\t|Phone Number" << endl;
for (ctr = 0; ctr < numCar; ctr++){
cout << "\t" << tempname[ctr];
cout << "\t" << tempyear[ctr];
cout << "\t" << tempmake[ctr];
cout << "\t " << tempmodel[ctr];
cout << "\t " << tempmileage[ctr];
cout << "\t\t " << tempphone[ctr] << endl;
cout << endl << endl;
}
//Trying to output a .txt file
/* ofstream my_file;//create instance of the object my_file
my_file.open ("c:example_1.txt");//creates empty .txt file
my_file << for (ctr = 0; ctr < numCar; ctr++)
cout << "\n" << car[numCar];
*/
system("PAUSE");
return 0;
}
| |