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
|
#include <iostream>
#include <iomanip>
#include <fstream>
#include <cstdlib>
using namespace std;
/* ******************** class Car ********************
create class Car
*/
class Car
{
private:
string reportingMark;
int carNumber;
string kind;
bool loaded;
string destination;
public:
Car(); // default constructor
Car(const Car &oldObject); // coy constructor
Car(string reportingMark,int carNumber, string kind, bool loaded, string destination){setUp(reportingMark,carNumber,kind, loaded, destination);}
~Car(){}; // display nothing
void output();
void setUp(string, int, string, bool, string);
friend bool operator==(const Car &, const Car &); //friend
};
void input();
int main()
{
input();
return 0;
}
/* ******************** Car::Car ********************
default constructor
*/
Car::Car()
{
reportingMark = "";
carNumber = 0;
kind = "other";
loaded = false;
destination = "NONE";
}
/* ******************** Car::Car ********************
copy constructor
*/
Car::Car(const Car &oldObject)
{
reportingMark = oldObject.reportingMark;
carNumber = oldObject.carNumber;
kind = oldObject.kind;
loaded = oldObject.loaded;
destination = oldObject.destination;
}
/* ******************** Car::showInput ********************
uses the input from user and displays it onto the screen
*/
void Car::output()
{
cout << endl;
cout << setw(14)<< left<< "reportingMark:" << setw(5)<< left << reportingMark << endl;
cout << setw(14)<< left<<"carNumber:" << setw(5)<< left << carNumber << endl;
cout << setw(14)<< left<<"kind:" << setw(5)<< left << kind << endl;
switch(loaded)
{
case 1:
cout << setw(14)<< left<<"loaded:" << setw(5)<< left << "true" << endl;
break;
default:
cout << setw(14)<< left<<"loaded:" << setw(5)<< left << "false" << endl;
break;
}
cout <<setw(14)<< left<< "destination:" << setw(5)<< left << destination << endl;
}
/* ******************** Car::setUp ********************
puts everything from user input into the class
*/
void Car::setUp(string ReportingMark, int CarNumber, string Kind, bool Loaded, string Destination)
{
reportingMark = ReportingMark;
carNumber = CarNumber;
kind = Kind;
loaded = Loaded;
destination = Destination;
}
/* ******************** bool operator== ********************
checks if car1.reportingMark and car2..reportingMark are same
checks if car1.carNumber and car2.carNumber are the same
*/
bool operator==(const Car &car1, const Car &car2)
{
if((car1.reportingMark == car2.reportingMark) && (car1.carNumber == car2.carNumber))
return true;
}
/* ******************** Input ********************
ask for user input on reporting mark, car number, kind, loaded, and destination
*/
void input()
{
string type;
string ReportingMark;
int CarNumber;
string Kind;
bool Loaded;
string Destination;
ifstream inputFile;
inputFile.open("car.txt", ios::in);
if(inputFile.fail())
{
cerr << "File has failed to open." << endl;
exit(1);
}
while(inputFile.peek() != EOF)
{
while(inputFile.peek() == ' ')
{
inputFile.get();
}
inputFile >> type;
inputFile >> ReportingMark;
inputFile >> CarNumber;
inputFile >> Kind;
inputFile >> Loaded;
getline(inputFile, Destination);
Car temp(ReportingMark, CarNumber, Kind, Loaded, Destination);
temp.output();
}
inputFile.close();
}
|