I have displayed an array in which I have added numbers in. Numbers 1 through 9, with 9 being a sentinel value. That I cannot figure out how to assign these numbers to the corresponding indicies. Please, please help
#include <iostream>
#include <iomanip>
#include <string>
#include <fstream>
using namespace std;
const int SENTINEL = 9;
struct menuItemType
{
string menuItem;
double menuPrice;
bool selected;
};
void getData(ifstream & infile, menuItemType menu[], int & size);
void displayMenu(const menuItemType menu[], int & size);
void printCheck(menuItemType menu[], int & size);
int main()
{
menuItemType menu[15];
menuItemType orderNumber;
int size;
ifstream infile;
getData(infile, menu, size);
displayMenu(menu, size);
printCheck(menu, size);
return 0;
}
void getData(ifstream & infile, menuItemType menu[], int & size)
{
int i = 0;
infile.open("menu.txt");
if(infile.fail())
{
cout << "The file could not be uploaded" << endl;
exit(-1);
}
do
{
getline(infile, menu[i].menuItem);
infile >> menu[i].menuPrice;
infile.get();
menu[i].selected = false;
i++;
}
while(infile);
size = i - 1;
}
void displayMenu(const menuItemType menu[], int & size)
{
cout << fixed << showpoint << setprecision(2);
cout << endl << "Menu Choices:" << endl;
for(int i= 0; i < size; i++)
{
cout << left << i + 1 << ". " << setw(18) << menu[i].menuItem << setw(5)
<< right << menu[i].menuPrice << endl;
}
cout << "9. Finished Ordering" << endl;
cout << endl;
}
// This is where I am stuck. The user is supposed to pick breakfast choices from the menu
// using the numbers 1-9. 9 being the sentinel to finish their ordering and formulate a check
// I cannot figure out how to associate the numbers 1. through 9. to the indicies where the
// menu items are stored.
void printCheck(menuItemType menu[], int & size)
{
int i = 0;
char orderNumber; // I Cant figure out how to declare this variable //
cout << "Please enter the number of the choice you'd like to order: " << endl;
cin >> orderNumber;
while(orderNumber != SENTINEL)
{
cout << "Please enter the number of the choice you'd like to order: " << endl;
cin >> orderNumber;
if(orderNumber == 1)
{
cout << left << setw(18) << menu[0].menuItem << setw(5)
<< right << menu[0].menuPrice << endl;
}
else if(orderNumber == 2)
{
cout << left << setw(18) << menu[1].menuItem << setw(5)
<< right << menu[1].menuPrice << endl;
}
else if(orderNumber == 3)
{
cout << left << setw(18) << menu[2].menuItem << setw(5)
<< right << menu[2].menuPrice << endl;
}
else if(orderNumber == 4)
{
cout << left << setw(18) << menu[3].menuItem << setw(5)
<< right << menu[3].menuPrice << endl;
}
else if(orderNumber == 5)
{
cout << left << setw(18) << menu[4].menuItem << setw(5)
<< right << menu[4].menuPrice << endl;
}
else if(orderNumber == 6)
{
cout << left << setw(18) << menu[5].menuItem << setw(5)
<< right << menu[5].menuPrice << endl;
}
else if(orderNumber == 7)
{
cout << left << setw(18) << menu[6].menuItem << setw(5)
<< right << menu[6].menuPrice << endl;
}
else if(orderNumber == 8)
{
cout << left << setw(18) << menu[7].menuItem << setw(5)
<< right << menu[7].menuPrice << endl;
}
else(orderNumber == 9);
{
break;
}
}
}