Question: C + + code I did the code myself, I ' m just stuck. The code reads in from a file called MenuData.txt ( from

C++ code
I did the code myself, I'm just stuck.
The code reads in from a file called MenuData.txt (from notepad) with this info, in this format:
Plain_Egg 1.45
Bacon_and_Egg 2.45
Muffin 0.99
French_Toast 1.99
Fruit_Basket 2.49
Cereal 0.69
Coffee 0.50
Tea 0.75
Then I use the data from the imported file for an array. I output the menu to the console (from the array) and prompt the user to select an item from the menu. They can choose up to 8 times (1 item per choice). Then I calculate the sales tax. The very last step is to output the receipt with the items selected, the tax, total cost, and amount due.
The file reads fine, but the issue I'm having is I somehow created an infinite loop and the tax and total amount aren't printing to the receipt. Please help!!
I'll include an image to give an idea of what the output should look like. Thank you!
Please don't include advanced stuff like stringstream, we haven't learned that!
This is the code:
#include fstream >
#include iostream >
#include iomanip >
#include cstring >
using namespace std;
const int MAX_ITEMS =8; // Maximum number of menu items
// Struct for menu items
struct menuItemType {
char itemName[30]; // Name of the item
double itemPrice; // Price of the item
};
// Function to load data from file into menuList array
void getData(menuItemType menuList[], int& itemCount){
ifstream infile;
infile.open("MenuData.txt");
int i =0;
// using if else statement for if file opens successfully or not
if(!infile.is_open())
{
cout "File can't be opened" endl;
}
else
{
while(!infile.eof())
{
// storing player information in the array players
infile >> menuList[itemCount].itemName;
infile >> menuList[itemCount].itemPrice;
itemCount++;
}
}
infile.close();
}
// Function to show the menu
void showMenu(menuItemType menuList[], int itemCount){
cout "Welcome to My Restaurant" endl;
cout "---Today's Menu---" endl;
cout "---" endl;
for (int i =0; i itemCount; i++){
cout i +1": " menuList[i].itemName "";
cout fixed showpoint setprecision(2)"$" menuList[i].itemPrice endl;
}
cout "
You can make up to " itemCount " single order selections" endl;
}
// Function to calculate and print the check
void printCheck(const menuItemType menuList[], const bool selectedItem[]){
double total =0.0;
cout "
Your order
";
for (int i =0; i MAX_ITEMS; i++){
if (selectedItem[i]){
cout left setw(15) menuList[i].itemName "$" fixed setprecision(2)
menuList[i].itemPrice endl;
total += menuList[i].itemPrice;
}
}
// while (choice =='Y'|| choice =='y');
// Print the check
printCheck(menuList, selectedItem);
// calculating tax (5%)
double tax = total *0.05;
total += tax;
// outputting the tax and amount due
cout "Tax ""$" fixed showpoint setprecision(2) tax endl;
cout "Amount Due ""$" fixed showpoint setprecision(2) total endl;
}
int main(){
menuItemType menuList[MAX_ITEMS];
int itemCount;
getData(menuList, itemCount);
showMenu(menuList, itemCount);
// User selection
bool selectedItem[MAX_ITEMS]={false};
char choice;
int selection;
do {
cout "Do you want to make selection Y/y (Yes), N/n (No): ";
cin >> choice;
if (choice =='Y'|| choice =='y'){
cout "Enter item number: ";
cin >> selection;
if (selection >=1 && selection = itemCount){
if (selection >= MAX_ITEMS){
cout "You reached the menu ordering selection limit" endl;
break;
}
else {
selectedItem[selection-1]= true;
selection++;
cout "selected:" menuList[selection -1].itemName endl;
}
}
else {
cout "Invalid item number. Please try again." endl;
}
}
} while (choice =='Y'|| choice =='y');
// Print the check
printCheck(menuList, selectedItem);
return 0;
}
 C++ code I did the code myself, I'm just stuck. The

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!