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
|
#include <iostream>
#include <iomanip>
#include <string>
#include <fstream>
using namespace std;
//Global Variables
#define tab2 "\t\t";
#define down3 "\n\n\n";
const int SIZE = 7;
//Prototypes
void showMenu();
void loadArrays(string fruit[], double price[], int& count);
void showArrays(string fruit[], double price[], int& count);
void lookUpPrice(string fruit[], double price[], int& count);
int main()
{
//Variables Declared in Main
int choice = 0;
int count = 0;
double price[SIZE];
bool quit = false;
string fruit[SIZE];
//Set floating point to 2 places for currency display
cout << setprecision(2) << fixed;
loadArrays(fruit, price, count);
while (!quit) //Menu option #3 changes bool to true
{
showMenu();
cout << "\nEnter your selection: ";
cin >> choice;
switch (choice)
{
case 1:
showArrays(fruit, price, count);
break;
case 2:
lookUpPrice(fruit, price, count);
break;
case 3:
cout << "\nProgram will end";
quit = true;
break;
default:
cout << "\nNot a valid entry. Try again.";
break;
}
}
cout << down3;
system("Pause");
return(0);
}
void loadArrays(string fruit[], double price[], int& count)
{
ifstream input;
string fileName;
cout << "\nEnter the name of the file you wish to open: ";
cin >> fileName;
input.open(fileName);
for (int x = 0; x < SIZE; x++)
{
getline(input, fruit[x]);
input >> price[x];
input.ignore();
count++;
}
input.close();
}
void showArrays(string fruit[], double price[], int& count)
{
for (int x = 0; x < count; x++)
{
cout << "Fruit: " << fruit[x] << endl;
cout << "Price: $" << price[x] << endl;
}
}
void showMenu()
{
cout << "\nPlease select an option from the following menu: " << endl;
cout << "\n\t1 - Display all fruits and prices";
cout << "\n\t2 - Look up price of specific fruit";
cout << "\n\t3 - Exit the program";
}
void lookUpPrice(string fruit[], double price[], int& count)
{
string searchedFruit;
price = 0;
cout << "\nEnter the name of the fruit you want the price of: ";
cin >> searchedFruit;
for (int x = 0; x < count; x++)
{
if (searchedFruit == fruit[x])
{
price = price[x];
//cout << "\nPrice: $" << price[x];
}
else
{
cout << "\nThat fruit does not exist in this list. Try again.";
}
}
}
|