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
|
#include<fstream>
#include<iostream>
#include<string>
#include<iomanip>
using namespace std;
struct CellphoneInfo
{
string name;
string manu;
string cellDesign;
string internalMem;
string camera;
string os;
double costPrice;
};
void outputData(CellphoneInfo [],int);
double total(CellphoneInfo [],int);
int searchByName(CellphoneInfo [],int );
int main()
{
const int size = 100;
CellphoneInfo cellphone[size];
fstream dataFile;
dataFile.open("Inventory.txt",ios::in);
int count =0;
if(dataFile)
{
while(getline(dataFile,cellphone[count].name,'#'))
{
getline(dataFile,cellphone[count].manu,'#');
getline(dataFile,cellphone[count].cellDesign,'#');
getline(dataFile,cellphone[count].internalMem,'#');
getline(dataFile,cellphone[count].camera,'#');
getline(dataFile,cellphone[count].os,'#');
dataFile >> cellphone[count].costPrice;
dataFile.ignore(2,'\n');
++count;
}
dataFile.close();
cout << "File read in" << endl;
outputData(cellphone,count);
}
else
{
cout << "Error: Cannot open file.\n";
}
int element = searchByName(cellphone,count);
cout << "\n\nElement: " << element;
if (element != -1)
{
cout << "ID\tCell Model\tManu Name\tDesign\tMemory\tCamera\tOS\t\t\tPrice\n";
cout << fixed << showpoint << setprecision(2);
cout << element << "\t" << cellphone[element].name << "\t" << cellphone[element].manu << "\t" << cellphone[element].cellDesign << "\t" << cellphone[element].internalMem << "\t" << cellphone[element].camera << "\t" << cellphone[element].os << "\t\t" << cellphone[element].costPrice << endl;
}
return 0;
}
int searchByName(CellphoneInfo item [],int size)
{
int choice;
outputData(item,size);
string modelName;
int id;
cout << "\n***Search***\n";
cout << "1. Apple\n";
cout << "2. Blackberry\n";
cout << "3. Nokia\n";
cout << "4. Samsung\n";
cout << "5. Sony\n";
cout << "Enter the corresponding number of the Model that you would like to search for: ";
cin >> choice;
switch(choice)
{
case 1: modelName = "Apple ";
break;
case 2: modelName = "Blackberry";
break;
case 3: modelName = "Nokia ";
break;
case 4: modelName = "Samsung ";
break;
case 5: modelName = "Sony ";
break;
default:
cout << "Invalid choice. To try again, run the program again.\n";
}
cout << "\nEnter ID of the " << modelName << ": ";
cin >> id;
int index = 1;
int position = -1;
bool found = false;
while(index < size && !found)
{
if(item[index].manu == modelName && index == id)
{
found = true;
position = index;
}
index++;
}
return position;
}
void outputData(CellphoneInfo arr [],int count)
{
cout << "ID\tCell Model\tManu Name\tDesign\tMemory\tCamera\tOS\t\t\tPrice\n\n";
cout << fixed << showpoint << setprecision(2);
for(int i = 0;i < count;i++)
{
cout << (i+1) << "\t" << arr[i].name << "\t" << arr[i].manu << "\t" << arr[i].cellDesign << "\t" << arr[i].internalMem << "\t" << arr[i].camera << "\t" << arr[i].os << "\t\t" << arr[i].costPrice << endl;
}
cout << endl;
}
|