| | |
Using arrays and menus...
Please support our C++ advertiser: Intel Parallel Studio Home
![]() |
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.
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.
(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.
C++ Syntax (Toggle Plain Text)
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.
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.
>> 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.
Last edited by Ancient Dragon; Dec 16th, 2007 at 7:00 pm.
Don't PM me with questions -- you might get a nasty PM in response. If you have a question then post it in one of the forums.
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.
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.
Last edited by WaltP; Dec 17th, 2007 at 12:53 am.
The 3 Laws of the Procrastination Society:
1) Never do today that which can be put off until tomorrow
2) Tomorrow never comes
1) Never do today that which can be put off until tomorrow
2) Tomorrow never comes
This is the translation (in diagram) for WaltP explanations:
- Suppose you're declaring an array named food
- The declaration of an 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
- Suppose you're declaring an array named food
- The declaration of an array:
C++ Syntax (Toggle Plain Text)
double food[the_size_of_the_array];
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
![]() |
Similar Threads
- Help with holding data input to a file (C++)
- can you help me in this program (C)
- Easy drop down menu? (JavaScript / DHTML / AJAX)
Other Threads in the C++ Forum
- Previous Thread: I need help please
- Next Thread: stuck please help-quick question
| Thread Tools | Search this Thread |
api array based beginner binary bitmap c++ c/c++ calculator char char* class code coding compile compiler console conversion count database delete deploy desktop developer dll download dynamic dynamiccharacterarray email encryption error file forms fstream function functions game givemetehcodez google graph gui homeworkhelp homeworkhelper iamthwee ifstream input int integer java lib linkedlist linker list loop looping loops map math memory multiple news node number numbertoword output parameter pointer problem program programming project python random read recursion recursive reference rpg sorting string strings struct temperature template test text text-file tree unix url variable vector video visualstudio win32 windows winsock word wordfrequency wxwidgets






