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 <fstream>
#include <string>
using namespace std;
#include "Car.h"
void readFile (ifstream&, Car[]);
void sortByModel (Car*[]);
void searchByModel (Car[], Car*[], string, int&);
void assignPointers (Car[], Car*[]);
int main()
{
Car* carPtr[10];
Car carArray[10];
ifstream fin;
string ifName;
string modelSearch;
int searchResult;
cout << "Enter the name of the file you want to open: ";
cin >> ifName;
fin.open(ifName.c_str());
if (fin.fail())
{
cout << "Could not open the specified file.";
}
readFile (fin, carArray);
assignPointers(carArray, carPtr);
sortByModel (carPtr);
for (int z=0; z < 10; z++)
{
carPtr[z]->print();
}
cout << endl << endl << "Enter a vehicle model to search: ";
cin >> modelSearch;
searchByModel (carArray, carPtr, modelSearch, searchResult);
if (searchResult >= 0)
{
cout << endl << "Your vehicle model was found in array position "
<< searchResult << "." << endl << endl;
}
else
{
cout << endl << "Your vehicle model was not found in the array."
<< endl << endl;
}
}
void readFile (ifstream&inFile, Car carArr[])
{
for (int i=0; i < 10; i++)
{
string aMake, aModel;
int aYear;
double aValue;
getline(inFile, aMake);
getline(inFile, aModel);
inFile >> aYear;
inFile >> aValue;
carArr[i].setMake(aMake);
carArr[i].setModel(aModel);
carArr[i].setYear(aYear);
carArr[i].setValue(aValue);
inFile.ignore(100, '\n');
inFile.ignore(100, '\n');
if(inFile.eof())
{
break;
}
}
}
void assignPointers(Car carArr[], Car*carPoint[])
{
for (int j=0; j < 10; j++)
{
carPoint[j] = &(carArr[j]);
}
}
void sortByModel(Car* carPt[])
{
bool swap;
Car* tempCar;
do
{
swap = false;
for (int count=0; count < 9; count++)
{
if (carPt[count]->getModel() > carPt[count+1]->getModel())
{
tempCar = carPt[count];
carPt[count] = carPt[count+1];
carPt[count+1] = tempCar;
}
swap = true;
}
}
while (swap);
}
//void sortByModel (Car carArr[], Car*carPt[])
//{
// int minPos = 0;
// int tempVal;
//
// for (int i=10; i < 10; i++)
// {
// for (int k=minPos+1; k < 10; k++)
// {
// if (carArr[k].getModel() < carArr[minPos].getModel())
// {
// tempVal = k;
// k = minPos;
// minPos = tempVal;
//
// }
//
// }
// minPos++;
// }
//}
void searchByModel (Car carArr[], Car*carPt[], string modelIn, int&itemFound)
{
string tempModel;
itemFound = -1;
for (int i=0; i < 10; i++)
{
tempModel = carArr[i].getModel();
if (tempModel == modelIn)
{
itemFound = i;
}
}
}
|