Menu Driven Program

Hi,
I have managed to get my program to work, and it looks great. But the output is not exactly what I want. In the print function, the loop seems to be repeating some items. This shouldn't be the case. This is my first time here, but I will appreciate any help. Thanks in advanced.

[This is my code]
//Assignment IV- Restaurant Menu: Nathaniel Cooper; Nov. 8, 2015
//This program automate the breakfast billing system of Cooper's restaurant.


#include<iostream>
#include<string>
#include<iomanip>
using namespace std;

struct ItemType //Struct Declaration
{
string item_name;
double UnitPrice;
};

//Functions Prototypes
void showMenu(ItemType MenuList[],int s);
int getData(ItemType MenuList[], ItemType OrderedList[], int Max);
void printCheck(ItemType OrderedList[], int counter);

int main()
{

int counter = 0;
const int SIZE = 8;
const int Max = 30;
ItemType OrderedList[Max];
ItemType MenuList[SIZE] = {{"Plain Egg", 1.45}, {"Bacon & Egg", 2.45 }, {"Muffin", 2.45},
{"French Toast", 1.99}, {"Fruit Basket", 2.49}, {"Cereal", 0.69},
{"Coffee", 0.50}, {"Tea ", 0.75}};
//Functions Call
showMenu(MenuList,SIZE);
counter = getData(MenuList, OrderedList, Max);
printCheck(OrderedList, counter);

return 0;
}


void showMenu(ItemType MenuList[], int s)
{
cout << "Welcome to Cooper's Restaurant!! \n";
cout << "The Citadel of Delicious Food. \n\n";
cout << " Items " << setw(25) << "Unit Price \n";
for(int count = 0; count < s; count++)
{
cout << "#" << count+1 <<" "
<< MenuList[count].item_name << "\t\t$"
<< MenuList[count].UnitPrice << "\n";
}

cout << "\nEnter the number of your selection until you have completed your order. \n";
cout << "Enter 9 when you are done. \n";
}

int getData(ItemType MenuList[], ItemType OrderedList[], int Max)
{
int choice;
int counter = 0;

do{
cout << "What would you like? If you done ordering (enter 9): \n";
cin >> choice;

if(choice < 1 || choice > 9)
{
cout << "\a\a Invalid Input \n";
cout << "Enter the number of your selection. \n";
cin >> choice;
}

if(choice !=9)
OrderedList[counter].item_name = MenuList[choice-1].item_name; //enters item name in order
OrderedList[counter].UnitPrice = MenuList[choice-1].UnitPrice; //enter price in order
counter++;
}while(choice !=9 && counter < Max); //repeats the whole thing until 9 is entered

return counter;

}


void printCheck(ItemType OrderedList[],int counter)
{
double tax = 0.05;
double checkTotal;
double FinalTotal;

cout << "\nWelcome to Cooper's Restaurant!"<< "\n";
cout << "This is your check. \n\n";
cout << "***************************************************** \n";
for(int count = 0; count < counter; count++)
{
cout << " "
<< OrderedList[count].item_name <<"\t\t$"
<< OrderedList[count].UnitPrice << "\n";
checkTotal += OrderedList[count].UnitPrice;
}
tax = checkTotal * tax;
FinalTotal = checkTotal + tax;
cout << setw(25) << left << "Tax" <<fixed << showpoint <<setprecision(2)<<"$"<<tax << "\n";
cout << "***************************************************** \n";
cout << setw(25) << left << "Total" <<fixed << showpoint <<setprecision(2)<<"$"<<FinalTotal << "\n";
}

*You will fully understand my point if you run the code. I just can't find why it's not doing what I want it to do.
Last edited on
Look at this snippet:for(int count = 1; count < counter; count++)
Do you realize that arrays start at zero, not one?
Last edited on
Yes, that was a stupid mistake. I just fixed that, and it still doesn't work.
I just can't find why it's not doing what I want it to do.

What exactly do you want it to do?

What exactly is it doing now?

Please edit your first post to add the code tags (The <> icon to the right).
I edited it. I also did the 'add the code tag.' I don't know if this makes sense, in the print function, the loop repeats the first item twice. This shouldn't be the case.
I want to print out the items, but I don't want any items to be repeated.
Those this make sense?
*Does this make sense?
Please, use code tags (the <> formatting button) when posting code.
It makes it easier to read your code and also easier to respond to your
post.
http://www.cplusplus.com/articles/jEywvCM9/
Hint: You can edit your post, highlight your code and press the <>
formatting button.
Please format your code as you are told.

One thing:
1
2
3
if(choice !=9)
OrderedList[counter].item_name = MenuList[choice-1].item_name; //enters item name in order
OrderedList[counter].UnitPrice = MenuList[choice-1].UnitPrice; //enter price in order 
I guess should be
1
2
3
4
5
if(choice !=9)
{
OrderedList[counter].item_name = MenuList[choice-1].item_name; //enters item name in order
OrderedList[counter].UnitPrice = MenuList[choice-1].UnitPrice; //enter price in order
}
The problem is in the function getData().
counter gets incremented once to often and then of course in printCheck it will print one item to much.
There is also a small problem with the alignment of the output.


Topic archived. No new replies allowed.