Need help with output - Menu item number will not increase and doesn't display correctly after first iteration
Apr 28, 2016 at 8:53pm UTC
My code: (Line 58-62 is the problem)
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
#include<iostream>
#include<fstream>
#include<iomanip>
#include<string>
using namespace std;
const int FOOD_ITEMS = 8;
const double TAX = 0.05;
struct menuItemType {
string menuItem;
double menuPrice;
int count = 0;
}menuList[FOOD_ITEMS];
int items[FOOD_ITEMS] = { 0 };
void getData(ifstream &inData);
void showMenu(menuItemType menuList[]);
void printCheck(menuItemType menuList[], int items[]);
int main()
{
ifstream inData;
getData(inData);
showMenu(menuList);
}
void getData(ifstream &inData)
{
inData.open("menu.txt" );
if (!inData)
{
cout << "error opening file!" ;
}
for (int i = 0; i < FOOD_ITEMS; i++)
{
getline(inData, menuList[i].menuItem, '$' );
inData >> menuList[i].menuPrice;
}
}
void showMenu(menuItemType menuList[])
{
char greeting;
int selection;
int orders;
cout << fixed << showpoint << setprecision(2);
cout << "Welcome to Johnny's Resturant\n" << endl;
cout << left << setw(12) << "Item #" << left << setw(12) << "Food" << "Price\n"
<< "-------------------------------------------\n" ;
for (int i = 0; i < FOOD_ITEMS; i++)
{
cout << left << setw(12) << menuList[i].count + 1 << menuList[i].menuItem << '$' << menuList[i].menuPrice
<< endl;
}
cout << endl << endl;
cout << "Would you like to place an order? (y or n): " ;
cin >> greeting;
while (true ){
if (greeting == 'y' || greeting == 'Y' )
{
cout << "Please enter the Item # you would like: " ;
cin >> selection;
cout << "How many orders: " ;
cin >> orders;
items[selection - 1] = orders;
cout << "Would you like to place an order? (y or n): " ;
cin >> greeting;
}
else
{
printCheck(menuList, items);
break ;
}
}
}
void printCheck(menuItemType menuList[], int items[])
{
double total = 0;
cout << "\nWelcome to Johnny's Resturant" ;
for (int i = 0; i < FOOD_ITEMS; i++)
{
if (items[i] != 0)
{
cout << menuList[i].menuItem << setw(10) << items[i] << setw(5) << "$" << items[i] * menuList[i].menuPrice;
total = total + (items[i] * menuList[i].menuPrice);
}
}
double taxAmount = TAX * total;
cout << "\nAmount Total: $" << total << endl;
cout << "Tax: $" << taxAmount << endl;
cout << "Amount Due: $" << total + taxAmount << endl;
}
Displays:
Welcome to Johnny's Resturant
Item # Food Price
-------------------------------------------
1 Plain Egg $1.45
1
Bacon and Egg $2.45
1
Muffin $0.99
1
French Toast $1.99
1
Fruit Basket $2.49
1
Cereal $0.69
1
Coffee $0.50
1
Tea $0.75
Would you like to place an order? (y or n):
Last edited on Apr 28, 2016 at 8:54pm UTC
Apr 29, 2016 at 10:49am UTC
You don't ever set the value of menuItemType::count . For every one of your menu items, count is 0, so menuList[i].count + 1
is always 1, regardless of what i is.
Topic archived. No new replies allowed.