Hey guys gotta quick question for ya. I'm trying to figure out the best way to write an array for the following:

(1) Hot dog 1.50
(2) Fries 1.00
(3) Lemonade .75
(4) End order
You should be allowed to keep ordering from the menu until you press 4 for End order, at which point you should see a total amount due for your entire order.

I am using menus for the code. This is what I have come up with so far.

start
   perform housekeeping()
   while response not equal to quitValue
       perform mainLoop()
   endwhile
   perform finishUp()
stop

housekeeping()
    declare variables
    open files
    perform displayMenu()
return

displayMenu()
    print “1. HotDog: $1.50”
    print “2. Fries: $1.00”
    print “3. Lemonade: $0.75”
    print “4. End of Order”
    print “Please press a number to make a selection”
    read response
return

mainLoop()
    case based on response
         case 1
             perform hotDog()
         case 2
             perform fries()
         case 3
             perform lemonade()
         case 4
             perform endOfOrder()
         default
             print “Sorry. Invalid Entry.”
    endcase
    perform displayMenu()
return

Now for the hotDog(), fries(), lemonade(), and endOfOrder() modules how would I go about initializing the totalCost variable and have it add up correctly for each? I presume it should be something like this:
if choice = 1 (totalCost = totalCost + 1.50)
if choice = 2 (totalCost = totalCost + 1.00)
if choice = 3 (totalCost = totalCost + 0.75)
if choice = 4 (EXIT LOOP)
output "The total cost of your order is: " totalCost

That's where I am stuck at though. :confused:

I don't know what language that was written in -- it looks a little like cobol, but in any event you will have to translate that into c++. Shouldn't be very difficult -- just add the code between start and stop in main() then create functions for all the others.

>> how would I go about initializing the totalCost variable and have it add up correctly for each
Declare totalCost in main() and pass it to the functions.

Thanks Ancient Dragon. I haven't learned any specific languages yet. That's in line next semester. This is just the pseudocode and format that we have been taught. I found a good example which helped me visually understand how to lay it out. Thanks for your help!

>> I haven't learned any specific languages yet
Then I'm confused about what it is that you wanted? And why post it in the C++ board if you aren't trying to learn c++ yet?

I just wanted to make sure I was on the right track as far as the logic behind my code. Needing to initialize it beforehand so I would have the right outcome for my total. I start my C++ class in a few weeks. I didn't see a section under the programming list that fit just right. Did I miss one maybe or can you recommend where I should have posted? Regardless of your confusion you still lead me in the right direction to figure out what I needed so thanks again lol.

The array being filled with the order could simply contain the item numbers being ordered, one element per ordered item. Example, if the array contained 1,1,2,1,3,3, then 3 dogs, 1 fry, and 2 lemonades were ordered. Then at end of order, use each element of the array as an index into a cost array, which contains 1.25, 1.00, .75.

Or set up the array to be the number of items, each item being an element of the array. IOW, array[0] is for "hot dog". Example: 3,1,2 would be the same order as above. Then to total, multiply each array value with the cost array from above.

This is the translation (in diagram) for WaltP explanations:

- Suppose you're declaring an array named food
- The declaration of an array:

double food[the_size_of_the_array];

- ALWAYS keep in mind that, the number of an array index starts with 0. For example, you want to declare the size the array to be 5. It starts with 0....and end with 4. Like this:

food
[0] <-- The array index number. E.g: array[0]
[1]
[2] <-- Whatever contains in the array, you decide it
[3]

- Thus, your menu should look like this (inside the PC memory):

[1] Hot dog -----------> food[0] = 1.5;
[2] Fries -----------> food[1] = 1.0;
[3] Lemonade
[4] End order ---------> This part is a lil' bit tricky. You a need while loop for it

- Play around with the menu codings, its kinda fun for me.....at first ;)

This article has been dead for over six months. Start a new discussion instead.