| | |
Need help with FOR repitition with Switch
Please support our C++ advertiser: Programming Forums - DaniWeb Sister Site
Thread Solved |
•
•
Join Date: Nov 2009
Posts: 3
Reputation:
Solved Threads: 0
I'm trying to write a c++ program that first asks how may people are in a party, then it takes that number to calculate how many times to ask what the next person wants to order. This is what I have so far. I can get the program to recognize how many people I have entered in for the amount of people in a party. What I'm having a problem with is totaling the amount and then displaying the total amount due. The available menu items are a, b, c, d, and x. Each one has a value amount except for x. So if x is entered, then there is no amount to add to the total.
With what I have right now, the program will ask and hold the amount of people in the party. So the program will ask for an order the correct amount of time, but it isn't totaling my cost. So somewhere I'm missing a holder for the total and I'm just not sure on that part. Any help is greatly appreciated.
With what I have right now, the program will ask and hold the amount of people in the party. So the program will ask for an order the correct amount of time, but it isn't totaling my cost. So somewhere I'm missing a holder for the total and I'm just not sure on that part. Any help is greatly appreciated.
C++ Syntax (Toggle Plain Text)
#include <iostream> using std::cout; using std::cin; using std::endl; int main( ) { char itemOrdered = ' '; double customers = 0.0; double total = 0.0; int numCustomers = 0; cout << "Enter number of customers in party: "; cin >> numCustomers; cout << "This program calculates total for " << numCustomers << " customers." << endl; for (int counter = 0; counter < numCustomers; counter = counter + 1) { cout << "Our lunch menu today: " << endl; cout << " Combo A: Fried chicken with slaw $4.25" << endl; cout << " Combo B: Roast beef and mashed potato $5.75" << endl; cout << " Combo C: Fish and chips $5.25" << endl; cout << " Combo D: Soup and salad $3.75" << endl; cout << "Enter combo code[A/B/C/D] or X for no order: "; cin >> itemOrdered; itemOrdered = toupper(itemOrdered); for (char itemOrdered = 0; itemOrdered < numCustomers;) { switch(itemOrdered) { case 'A': total = total + 4.25; break; case 'B': total = total + 5.75; break; case 'C': total = total + 5.25; break; case 'D': total = total + 3.25; break; case 'x': total = total + 0.00; } cout << "Enter next item ordered [A/B/C/D/X] : "; cin >> itemOrdered; itemOrdered = toupper(itemOrdered); } } cout << "Please pay this amount: " << total << endl; system("pause"); return 0; }
•
•
Join Date: Oct 2009
Posts: 7
Reputation:
Solved Threads: 3
0
#2 Nov 3rd, 2009
You can use while
C++ Syntax (Toggle Plain Text)
while (itemOrdered != 'x') { switch(itemOrdered) { . . . } }
•
•
Join Date: Sep 2009
Posts: 615
Reputation:
Solved Threads: 81
0
#3 Nov 3rd, 2009
Scratch... sorry. I think he's right with the while, except use your count variable. You were getting stuck since your for loop wasn't incrementing
(when writing this I had missed that itemsOrdered was a char, but you got it to work so good luck and hope we at least nudged you a bit in the right direction
)
itemsOrdered but even doing that your count would be off. Go for the while or do{} while().(when writing this I had missed that itemsOrdered was a char, but you got it to work so good luck and hope we at least nudged you a bit in the right direction
) Last edited by jonsca; Nov 3rd, 2009 at 2:49 pm. Reason: incorrect assumption
•
•
Join Date: Nov 2009
Posts: 3
Reputation:
Solved Threads: 0
0
#5 Nov 3rd, 2009
For this program I wasn't able to use a while statement. Here is the code that I was able to get to work. Thanks again for the suggestions.
#include <iostream>
#include <iostream>
C++ Syntax (Toggle Plain Text)
using std::cout; using std::cin; using std::endl; int main( ) { char itemOrdered = ' '; double customers = 0.0; double total = 0.0; int numCustomers = 0; cout << "Enter number of customers in party: "; cin >> numCustomers; cout << "This program calculates the total due for " << numCustomers << " customers." << endl; for (int counter = 0; counter < numCustomers; counter = counter + 1) { cout << "Our lunch menu today: " << endl; cout << " Combo A: Fried chicken with slaw $4.25" << endl; cout << " Combo B: Roast beef and mashed potato $5.75" << endl; cout << " Combo C: Fish and chips $5.25" << endl; cout << " Combo D: Soup and salad $3.75" << endl; cout << "Enter combo code[A/B/C/D] or X for no order: "; cin >> itemOrdered; itemOrdered = toupper(itemOrdered); { switch(itemOrdered) { case 'A': total = total + 4.25; break; case 'B': total = total + 5.75; break; case 'C': total = total + 5.25; break; case 'D': total = total + 3.25; break; case 'x': total = total + 0.00; } itemOrdered = toupper(itemOrdered); } } cout << "Please pay this amount: " << total << endl; system("pause"); return 0; }
![]() |
Similar Threads
- Printing a song by using repetition and switch statements (C++)
- KVM SWITCH HELP! (Monitors, Displays and Video Cards)
- LAN is not Working Properly After Changing Switch From 10 Mbps 100 Mbps (Networking Hardware Configuration)
- Cannot get switch to execute (C++)
- iis 5 through linksys wireless 4prt switch firewal (Windows NT / 2000 / XP)
Other Threads in the C++ Forum
- Previous Thread: Error message "Linker error"????
- Next Thread: Read Last ten lines from text
Views: 243 | Replies: 4
| Thread Tools | Search this Thread |
Tag cloud for C++
6 api application array arrays based beginner binary bmp c++ c/c++ calculator char char* class classes code compile compiler console conversion convert count data delete deploy dll download dynamiccharacterarray encryption error file format forms fstream function functions game givemetehcodez graph homeworkhelp iamthwee ifstream input int java lib lines list loop looping loops map math matrix memory newbie news number numbertoword output pointer problem program programming project python random read recursion recursive reference return rpg search simple sort sorting spoonfeeding string strings struct temperature template templates text text-file tree url variable vector video visual visualstudio void win32 windows winsock wordfrequency wxwidgets





