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
|
using namespace std;
//function prototypes
void addCar(string nameOfFile);
void disCar(string nameOfFile);
bool removeCar(string);
int main()
{
const string FILE_NAME = "cars.txt";
int input = 0;
bool carRemoved = false;
string carMake= "";
do
{
cout << "1 enter car " << endl;
cout << "2 display cars " << endl;
cout << "3 Delete car form garage" << endl;
cout << "4 end" <<endl;
cout <<"enter option" << endl;
cin>>input;
cin.ignore(100, '\n');
cout <<endl;
if (input == 1)
addCar(FILE_NAME);
else if (input == 2)
disCar(FILE_NAME);
else if (input == 3) {
disCar(FILE_NAME);
cout << "^^delete car from above " << endl;
cout << "Enter car make to be removed " << endl;
getline (cin, carMake);
carRemoved == removeCar (carMake);
if (carRemoved == true)
cout << " car was not found " << endl;
else
{
cout << " Car was deleted from garage " << endl;
cout << " below are the availible cars in your garage " << endl;
disCar(FILE_NAME);
}
}
} while(input!=4);
}
void addCar(string nameOfFile)
{
//saves vehicles to file.
string name = "";
string make = "";
int year = 0;
ofstream outFile;
//open file for append.
outFile.open(nameOfFile.c_str(), ios::app);
if (outFile.is_open());
{
cout << "Enter car manufacturer: (-1 to end)";
getline(cin, make);
while (make != "-1")
{
//get car name.
cout << "Enter vehicle name: ";
getline(cin, name);
cout <<"Enter year of vehicle: ";
cin >> year;
cin.ignore();
outFile << make << '#'
<< name << '#'
<< year << endl;
cout << " Enter car Manufacturer: (-1 to end) " << endl;
getline (cin, make);
}//end while
//close file
outFile.close();
}
}
void disCar(string nameOfFile)
{
string name = "";
string make = "";
int year =0;
ifstream inFile;
inFile.open(nameOfFile.c_str(), ios::in);
if (inFile.is_open())
{
getline(inFile, make, '#');
getline (inFile, name,'#');
inFile >> year;
while (!inFile.eof())
{
//display.
cout << make << ", " <<
name << ", " << year << endl;
//read another record.
getline(inFile, make);
getline(inFile, name);
inFile >> year;
}//end while
inFile.close();
}
}
bool removeCar(string carMake)
{
string carInfo[100];
string name, make, year;
bool error = false;
int i = 0;
ifstream inFile;
inFile.open("cars.txt");
if (inFile.is_open())
{
while (isalpha(inFile.peek()))
getline(inFile, make);
if (make != carMake)
{
carInfo[i] = make;
i++;
getline(inFile, carInfo[i]);
i++;
getline(inFile, carInfo[i]);
i++;
}
else
{
getline (inFile, name);
getline(inFile, year);
}
}
if (name == "")
{
error =true;
cout << " File could not be found " << endl;
}
else
{
cout << " file could not be found " << endl;
}
inFile.close();
ofstream outFile;
outFile.open("cars.txt");
if (outFile.is_open())
{
for (int x=0; x < i; x++)
outFile << carInfo[x] << endl;
outFile.close();
}
else
{
error = true;
cout << " file could not be opend" << endl;
}
return error;
}
|