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
|
#include<iostream>
#include<string>
#include<iomanip>
#include<fstream>
using namespace std;
//Prototype Statements
void getData(string, string [], string [], int [], double [], int &);
void processUserSelections(string [], string [], string , double [], int [], int &);
int linearSearch(const string [], int &, string);
void displayResults(string [], double [], int [], int );
const int AMT = 40;
int main()
{
string number;
int position;
string filename = "inventory.txt";
int num_inventory = 0;
string inv_num[AMT], description[AMT];
int mileage[AMT];
double price[AMT];
//Call the functions.
getData(filename, inv_num, description, mileage, price, num_inventory);
processUserSelections( inv_num, description, number, price, mileage, num_inventory);
}
void getData(string filename, string inv_num[], string description[], int mileage[], double price[], int &num_inventory)
{
ifstream infile;
infile.open(filename.c_str());
string line;
int c = 0;
while (getline(infile, inv_num[c]))
{
getline(infile, description[c]);
infile >> mileage[c];
infile >> price[c];
infile.ignore();
getline(infile, line);
c++;
}
infile.close();
num_inventory = c;
}
void processUserSelections(string inv_num[], string description[], string number, double price[], int mileage[], int &num_inventory)
{
int position = 0;
cout << "Welcome to the Car Lot Inventory program!\n" << endl;
while(!(number == "q" || number == "Q"))
{
cout << "Enter the cars inventory number <or q to quit>: ";
cin >> number;
cout << "\n";
if(number == "q" || number == "Q")
{
cout << "\nPress ENTER to continue.....\n\n";
cin.ignore();
cin.get();
return;
}
position = linearSearch(inv_num, num_inventory, number);
displayResults(description, price, mileage, position);
}
return;
}
int linearSearch(const string inv_num[], int &num_inventory, string number)
{
for (int c = 0; c < num_inventory; c++)
{
if (inv_num[c] == number)
return c;
}
return -1;
}
void displayResults(string description[], double price[], int mileage[], int position)
{
cout << "Description: " << description[position];"\n";
cout << "Mileage: " << mileage[position] << "\n";
cout << "Price: " << price[position] << "\n";
}
|