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
|
#include<iostream>
#include<iomanip>
#include<string>
#include<fstream>
using namespace std;
struct automobile
{
short int year;
string category, make, model, color, engine;
};
const short int amount_vehicles = 3;
struct garage
{
automobile vehicles[amount_vehicles];
double len, width;
};
void initialize_garage(garage &g);
garage populategarage();
void displaygarage(const garage &g);
void swap_car(garage &mg, garage &ng);
void main()
{
garage mine, neigbors;
cout << "Populate My Garage:";
initialize_garage(mine);
mine = populategarage();
cout << setw(50) << right << "My Populated Garage";
displaygarage(mine);
cout << "\n\n\n";
cout << "Populate Neigbor's Garage:";
initialize_garage(neigbors);
neigbors = populategarage();
cout << setw(50) << right << "Neighbor's Populated Garage";
displaygarage(neigbors);
cout << "\n\n\n";
swap_car(mine, neigbors);
cout << "\nAfter swapping car with neighbor, my garage is as follows:";
displaygarage(mine);
cout << "\nAfter swapping car with neighbor, neighbor's garage is as follows:";
displaygarage(neigbors);
cout << "\n\n\n";
system("pause");
}
void initialize_garage(garage &g)
{
for (int i = 0; i < amount_vehicles; i++)
{
g.vehicles[i].category = "\0";
g.vehicles[i].make = "\0";
g.vehicles[i].model = "\0";
g.vehicles[i].color = "\0";
g.vehicles[i].engine = "\0";
g.vehicles[i].year = 0;
}
g.len = 0.0; g.width = 0.0;
}
garage populategarage()
{
garage tg;
string instruction_prompt;
initialize_garage(tg);
for (int i = 0; i < amount_vehicles; i++)
{
cout << i+1;
getline(cin, tg.vehicles[i].category);
if (tg.vehicles[i].category == "\0")
i = amount_vehicles;
else
{
ifstream ReadFromFile("garage-input-file.txt");
if (ReadFromFile. is_open())
{
while (!ReadFromFile.eof())
{
getline(ReadFromFile, tg.vehicles[i].make);
getline(ReadFromFile, tg.vehicles[i].model);
getline(ReadFromFile, tg.vehicles[i].color);
cin >> (ReadFromFile, tg.vehicles[i].year);
cin.ignore();
getline(ReadFromFile, tg.vehicles[i].engine);
cin.ignore();
}
cin >> (ReadFromFile, tg.len);
cin >> (ReadFromFile, tg.width);
cin.ignore();
ReadFromFile.close();
}
else
cout << "Failed to open the file\n";
}
return tg;
}
}
void displaygarage(const garage &g)
{
for(int i = 0; i < amount_vehicles; i++)
{
cout << "\nVehicle " << i+1 << ":\n";
cout << setw(12) << right << "category: ";
cout << setw(15) << left << g.vehicles[i].category << "\n";
cout << setw(12) << right << "make: ";
cout << setw(20) << left << g.vehicles[i].make << "\n";
cout << setw(12) << right << "model: ";
cout << setw(20) << left << g.vehicles[i].model << "\n";
cout << setw(12) << right << "color: ";
cout << setw(20) << left << g.vehicles[i].color << "\n";
cout << setw(12) << right << "year: ";
cout << setw(20) << left << g.vehicles[i].year << "\n";
}
cout << "\nGarage size: " << g.len * g. width << " square ft. ";
}
void swap_car(garage &mg, garage &ng)
{
int choice = 0;
garage tg;
while (choice < 1 || choice > amount_vehicles)
{
cout << "\nEnter the car you want to swap (1/2/3) or quit with 0: ";
cin >> choice;
if (choice == 0 )
break;
}
if (choice != 0 )
{
int i = --choice;
tg.vehicles[i].category = ng.vehicles[i].category;
tg.vehicles[i].make = ng.vehicles[i].make;
tg.vehicles[i].model = ng.vehicles[i].model;
tg.vehicles[i].color = ng.vehicles[i].color;
tg.vehicles[i].year = ng.vehicles[i].year;
tg.vehicles[i].category = ng.vehicles[i].category;
tg.vehicles[i].make = ng.vehicles[i].make;
tg.vehicles[i].model = ng.vehicles[i].model;
tg.vehicles[i].color = ng.vehicles[i].color;
tg.vehicles[i].year = ng.vehicles[i].year;
tg.vehicles[i].category = ng.vehicles[i].category;
tg.vehicles[i].make = ng.vehicles[i].make;
tg.vehicles[i].model = ng.vehicles[i].model;
tg.vehicles[i].color = ng.vehicles[i].color;
tg.vehicles[i].year = ng.vehicles[i].year;
}
}
|