I wrote this code that takes in menu items from a txt file and shows it to the user. Each item has a specific number, the user then inputs the number of their desired item. How can I hide the number that the user inputs and only show the name of the item. I've tried using getch() but it still shows the number. Maybe I'm using it wrong. Any ideas of how to do this?
#include <iostream>
#include <iomanip>
#include <string>
#include <fstream>
#include <cstdlib>
#include <conio.h>
usingnamespace std;
constdouble tax_rate = 0.05;
struct menuItemType
{
string ItemName;
double ItemPrice;
};
void getData(ifstream &in, menuItemType menuList[]);
void showMenu(menuItemType menuList[]);
void printCheck(menuItemType menuList[]);
int main()
{
menuItemType menuList[10];
menuList[10].ItemName;
menuList[10].ItemPrice;
ifstream in;
in.open("Menu.txt");
cout << "Welcome to Johnny's Breakfast Diner!" << endl;
getData(in,menuList);
showMenu(menuList);
return 0;
}
void getData(ifstream &in, menuItemType menuList[])
{
int i = 0;
while (!in.eof())
{
in >> menuList[i].ItemName >> menuList[i].ItemPrice;
i++;
}
}
void showMenu(menuItemType menuList[])
{
int j = 0;
char answ;
cout << fixed << setprecision(2);
cout << "Would you like to take a look at our menu? (Y/N)";
cin >> answ;
if (answ == 'Y' || answ == 'y')
{
cout << left << setw(10) << "Item#" << left << setw(15) << "Item" << left << setw(18) << "Price" << endl;
do {
{
cout << left << setw(8) << j << " " << left << setw(15) << menuList[j].ItemName << " " << "$" << menuList[j].ItemPrice << endl;
j++;
}
} while (j < 8);
printCheck(menuList);
}
elseif (answ == 'N' || answ == 'n')
{
system("cls");
cout << "Have a good day!" << endl;
}
}
void printCheck(menuItemType menuList[])
{
ofstream out;
out.open ("Receipt.txt");
char answ;
int choice;
bool menu = true;
double subtotal = 0.0;
char ch[1];
out << fixed << setprecision(2);
cout << "Would like to place your order (Y/N)";
cin >> answ;
if (answ == 'Y' || answ == 'y')
{
cout << "Please enter the number of the item, 8 to finish order:";
out << "Welcome to Johnny's Breakfast Diner!" << endl;
do {
cin >> choice;
for (int i=0; i<1; i++)
{
ch[i] = getch();
cout << ' ';
}
if (choice == 0)
{
cout << menuList[0].ItemName << left << setw(4) << " " << "$" << menuList[0].ItemPrice << endl;
out << menuList[0].ItemName << left << setw(4) << " " << "$" << menuList[0].ItemPrice << endl;
subtotal = subtotal + menuList[0].ItemPrice;
}
elseif (choice == 1)
{
cout << menuList[1].ItemName << left << setw(3) << " " << "$" << menuList[1].ItemPrice << endl;
out << menuList[1].ItemName << left << setw(3) << " " << "$" << menuList[1].ItemPrice << endl;
subtotal = subtotal + menuList[1].ItemPrice;
}
elseif (choice == 2)
{
cout << menuList[2].ItemName << left << setw(6) << " " << "$" << menuList[2].ItemPrice << endl;
out << menuList[2].ItemName << left << setw(6) << " " << "$" << menuList[2].ItemPrice << endl;
subtotal = subtotal + menuList[2].ItemPrice;
}
elseif (choice == 3)
{
cout << menuList[3].ItemName << left << setw(1) << " " << "$" << menuList[3].ItemPrice << endl;
out << menuList[3].ItemName << left << setw(1) << " " << "$" << menuList[3].ItemPrice << endl;
subtotal = subtotal + menuList[3].ItemPrice;
}
elseif (choice == 4)
{
cout << menuList[4].ItemName << left << setw(1) << " " << "$" << menuList[4].ItemPrice << endl;
out << menuList[4].ItemName << left << setw(1) << " " << "$" << menuList[4].ItemPrice << endl;
subtotal = subtotal + menuList[4].ItemPrice;
}
elseif (choice == 5)
{
cout << menuList[5].ItemName << left << setw(6) << " " << "$" << menuList[5].ItemPrice << endl;
out << menuList[5].ItemName << left << setw(6) << " " << "$" << menuList[5].ItemPrice << endl;
subtotal = subtotal + menuList[5].ItemPrice;
}
elseif (choice == 6)
{
cout << menuList[6].ItemName << left << setw(6) << " " << "$" << menuList[6].ItemPrice << endl;
out << menuList[6].ItemName << left << setw(6) << " " << "$" << menuList[6].ItemPrice << endl;
subtotal = subtotal + menuList[6].ItemPrice;
}
elseif (choice == 7)
{
cout << menuList[7].ItemName << left << setw(9) << " " << "$" << menuList[7].ItemPrice << endl;
out << menuList[7].ItemName << left << setw(9) << " " << "$" << menuList[7].ItemPrice << endl;
subtotal = subtotal + menuList[7].ItemPrice;
}
elseif (choice == 8)
{
break;
}
}while(menu = true);
double tax = (subtotal * tax_rate);
double total = (tax + subtotal);
cout << "Taxes" << left << setw(7) << " " << "$" << tax << endl;
out << "Taxes" << left << setw(7) << " " << "$" << tax << endl;
cout << "Amount Due" << left << setw(2) << " " << "$" << total << endl;
out << "Amount Due" << left << setw(2) << " " << "$" << total << endl;
}
elseif (answ == 'N' || answ == 'n')
{
system("cls");
cout << "Ok, maybe I can help you at a later time." << endl;
}
}
I put the whole code if you wanted to test it, but the problem is only located in the printCheck function.
This is what is inside the txt file just incase it is needed.
PlainEgg 1.45
Bacon&Egg 2.45
Muffin 0.99
FrenchToast 1.99
FruitBasket 2.49
Cereal 0.69
Coffee 0.50
Tea 0.75
Also I'm using break in the else if statement ( answ == 8) because I can't seem to break out of the do while by setting menu equal to false. But I'm still working to fix it.