Help with a "restuarant menu"

0 Lé Pig · December 1, 2015
   Hey everyone. I've been hacking away at this assignment for the past 2 weeks and I just can't frickin get it. I have a restaurant menu (two arrays in a struct) and I am supposed to be able to list all items and prices (this part works fine) and then I'm supposed to be able to choose any individual item to add to an order. So for example if someone wants eggs, I input 0, if someone wants french toast I input "3" and so on. Then  when the person is done ordering, I need to add the total and print the bill.

   In my code I have the input as a for loop, but I can't figure out how to do it. I keep adding up numbers that are not referencing the array. Please help! Sorry I didn't use code tags, they never seem to work, but this is what I have so far.

#include <iostream>
#include <string>

using namespace std;



float sum;
int itemsOrdered;

struct menuItem
{
    string breakfastItem[8];
    float itemPrice[8];
}dish;

void setMenu()
{
    dish.breakfastItem[0]= "0. Plain Eggs -- $1.45\n";
    dish.itemPrice[0]= 1.45;
    dish.breakfastItem[1]= "1. Bacon and Eggs -- $2.45\n";
    dish.itemPrice[1]=2.45;
    dish.breakfastItem[2]="2. Muffin -- $0.99\n";
    dish.itemPrice[2]=0.99;
    dish.breakfastItem[3]="3. French Toast -- $1.99\n";
    dish.itemPrice[3]=1.99;
    dish.breakfastItem[4]="4. Fruit Basket -- $2.46\n";
    dish.itemPrice [4]=2.46;
    dish.breakfastItem[5]="5. Cereal -- $0.69\n";
    dish.itemPrice[5]=0.69;
    dish.breakfastItem[6]= "6. Coffee -- $0.50\n";
    dish.itemPrice[6]=.50;
    dish.breakfastItem[7]="7. Tea -- $0.75\n";
    dish.itemPrice[7]=0.75;
}

float showMenu()
{

            for(int i=0; i<=7;i++)
        cout << dish.breakfastItem;


       for(int j=0;j<99;j++){

        cin >> dish.itemPrice[j];
    sum = sum + dish.itemPrice[j];
    cout << sum;

  }
}






int main()
{
    setMenu();
    showMenu();


}



Post a Reply

Replies

- page 1
Oldest  Newest  Rating
+1 Dan P · December 1, 2015
Hi,

I'm confused as to why you are using C++ and using a struct rather than creating objects?
0 Lé Pig · December 1, 2015
Hey, Dan P. It's for an assignment.
+1 Dan P · December 1, 2015
Was the assignment to make a menu without using objects?
0 Lé Pig · December 1, 2015
Well, the specific instructions are as follows.

1. Show the customer the different menu items
2. Allow the customer to select more than one item from the menu.
3. Calculate and print the bill.

Then it gives a list of breakfast items. So actually I'm not sure. I think either way it would be alright.
0 c student · December 1, 2015
did you plan this before writing your code?  do you have some sort of psuedo algorithm to follow?  how well do you understand the procedure required to build the finished code?
+1 Dan P · December 1, 2015
Ok, for a start, lets clean up the struct...

struct menuItem
{
string breakfastItem;
float itemPrice;
};


We don't want to have an array of strings, or an array of itemPrices. This is just messy and confusing.

What you have done is said...

I want a structure that holds 8 breakfast items, and 8 prices.

Think about this for a minute. If you are creating all of the array items and prices in the 1 structure, then
why didn't you just put them directly in the main program? It wouldn't have made any difference.

The point in using the structure is that you define 1 item.

Then you can say, I want 10 of those structures.

For example...

menuItem menu = new menuItem[10];


This will create 10 menuItems for you.

If you did that, then you would have 80 menu items.

And you could only ever had multiples of 8.
0 Lé Pig · December 1, 2015
I did plan it despite the sloppiness of the code. What I don't understand is how to choose the value [0] for example and add it to another variable. My mind says make a variable called choice in a function called getData.

float getData()
{
    float choice;
    
       for(int j=0;j<5;j++){

        cin >> dish.itemPrice[j];
    sum = sum + dish.itemPrice[j];
    cout << sum;
}

That's all fine and dandy, but what the hell do I do with it? I have a for loop that apparently just adds every number I type in and gives me a sum from what I can tell in my finite wisdom it's referencing the array so I don't understand the realtionship there. Seriously, I have no idea what the hell is going on. I'm trying to get help and I do understand my explanations don't spark with a lot of people. I really wish I could explain to you how your brain works and you could explain to me how my brain works. I can take it. If you have to type in all caps and call me an idiot at the end, that's fine, whatever works for you. I want to understand this programming language. Starting with C++  was probably a bad idea for me but I can't turn back now. Please, explain it to me like the 5 year old I am.
+1 Dan P · December 1, 2015

float getData()
{
 float choice;

 for(int j=0;j> dish.itemPrice[j];   // this gets the itemPrice from the keyboard(well, stdin).
    sum = sum + dish.itemPrice[j];   // This adds the price you entered to the price you had before
    cout

What you need is a flag that is set to say that a menuItem has been chosen.


struct menuItem
{
bool selected;
string breakfastItem;
float itemPrice;
};


Then when you're asking the user to select what they want from the menu, if they press 1, then
item 1 gets dish[1].selected = true;

If they select item 5, then dish[5].selected = true.

sum = 0;
for(x = 0; x < dish.length; x++) {
  if(dish[x].selected == true) {
     sum = sum + dish[x].itemPrice;
  }
}

Then you do something like that to add them all up.
+1 Dan P · December 1, 2015
Sorry for so many posts, the code thing is broken.
0 Lé Pig · December 1, 2015
See, this is where I want to light my C++ book on fire. I've been working on my code for the past 2 weeks because someone in my class was helping me build it the way I showed you (until I got stuck). He knows what he's doing, he's a programmer, he'd make it work. Then suddenly everything I'm doing is wrong. What do I do about that?

and that's alright Dan. I appreciate the help. Now I'm gonna have to wipe my brain and go over everything new and everything I thought I knew and bleh...this is hard.
  • 1
  • 2

C++

107,198 followers
About

Used in many types of software including music players, video games, and many large scale applications.

Links
Moderators
Bucky Roberts Administrator