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
|
#include<fstream>
#include<iostream>
#include<iomanip>
#include<string>
using namespace std;
using UINT = unsigned int;
constexpr UINT Max_Number {100};
int loadArrays(string [], int [], UINT&);
void showArrays(string [], int [], UINT);
void sortPrices(string [], int [], UINT);
void lookupPrice(string [], int [], UINT);
int loadArrays(string console[], int price[], UINT& used)
{
used = 0;
ifstream prices("prices.txt");
if (!prices)
return (std::cout << "\n File " << std::quoted("prices.txt") << " did not open" << std::endl), 1;
for (string pr, temp; used < Max_Number && getline(prices, console[used]) && getline(prices, pr) && getline(prices, temp); ++used)
price[used] = stoi(pr);
prices.close();
return (std::cout << "Data loaded OK\n"), 0;
}
void showArrays(string console[], int price[], UINT arrayUsed)
{
cout << '\n' << setw(35) << "Console" << setw(25) << "Price" << "\n";
for (UINT i = 0; i < arrayUsed; ++i)
cout << setw(35) << console[i] << setw(25) << price[i] << "\n";
}
void lookupPrice(string console[], int price[], UINT used)
{
string Console;
while ((cout << "\nEnter the console name: ") && (!getline(cin, Console) || Console.empty())) {
cout << "Invalid name\n";
cin.clear();
}
UINT i = 0;
while ((i < used) && (Console != console[i]))
++i;
if (i < used)
cout << "The current price for " << Console << " is " << price[i] << "\n";
else
cout << "Console not found.\n";
}
void sortPrices(string cons[], int price[], UINT arrayUsed)
{
for (UINT i = 0, min = 0; i < arrayUsed - 1; ++i)
{
min = i;
for (UINT j = i + 1; j < arrayUsed; ++j)
if (price[j] > price[min])
min = j;
int tmp = price[min];
string t = cons[min];
price[min] = price[i];
cons[min] = cons[i];
price[i] = tmp;
cons[i] = t;
}
cout << "Consoles sorted!\n";
}
void HighestPrice(string cons[], int price[], UINT arrayUsed)
{
int max = price[0];
string Console = cons[0];
for (UINT i = 1; i < arrayUsed; ++i)
{
if (price[i] > max)
{
max = price[i];
Console = cons[i];
}
}
cout << "\nThe console that has the highest price is\n" << Console << " at " << max << endl;
}
int main()
{
string cons[Max_Number];
int price[Max_Number] {};
UINT arrayUsed {};
if (loadArrays(cons, price, arrayUsed))
return 1;
bool cont {true};
do {
cout <<
"\n" << std::string(10, ' ') << "Console Pricing - Main Menu\n" << std::string(47, '-') <<
"\n 1. Display all console prices"
"\n 2. Look up the price of a particular console"
"\n 3. Sort prices in descending orders"
"\n 4. Display the console with the highest price."
"\n 5. Exit the program"
"\n\n Enter Your choice: ";
UINT choice;
while (!(cin >> choice)) {
cin.clear();
cin.ignore(1000, '\n');
cout << "Invalid input \nPlease input again: ";
}
cin.ignore(1000, '\n');
switch (choice)
{
case 1:
showArrays(cons, price, arrayUsed);
break;
case 2:
lookupPrice(cons, price, arrayUsed);
break;
case 3:
sortPrices(cons, price, arrayUsed);
break;
case 4:
HighestPrice(cons, price, arrayUsed);
break;
case 5:
cont = false;
break;
default:
cout << "Incorrect choice.\n";
break;
}
} while (cont);
}
|