| | |
I Need Help with Cases in C++.
Please support our C++ advertiser: Intel Parallel Studio Home
Thread Solved |
•
•
Join Date: Oct 2008
Posts: 2
Reputation:
Solved Threads: 0
I am gonna start off saying that I am new to C++, and I have little programming knowledge (just Q Basic, and it has been a while since I have used that), and I need help on an assignment in my C++ class.
We have to use a case function to collect information for an order and display the total, but I have no clue how to get the case to work at all. I am sure the solution is simple, but as I have said I really do not know much about what I am doing.
And... here is the code:
I already have the int menu() function working fine, it is the case stuff I am struggling with.
We have to use a case function to collect information for an order and display the total, but I have no clue how to get the case to work at all. I am sure the solution is simple, but as I have said I really do not know much about what I am doing.
And... here is the code:
C++ Syntax (Toggle Plain Text)
double S=2.50; double C=1.00; double B=2.00; double R=1.00; double L=1.50; double &T; int menu(); int choices(); int main() { menu(); choices(); system ("pause"); return 0; } int choices() { for (n=1, n++) { do { case 'S' case 's': T=T+S; break; case 'C' case 'c': T=T+C; case 'B' case 'b': T=T+B; break; case 'R' case 'r': T=T+R; break; case 'L' case 'l': T=T+L; break; case 'X' case 'x' T=0; cout << "The Order Has Been Canceled. \n" break; } } }
I already have the int menu() function working fine, it is the case stuff I am struggling with.
•
•
Join Date: Nov 2007
Posts: 390
Reputation:
Solved Threads: 39
•
•
•
•
I am gonna start off saying that I am new to C++, and I have little programming knowledge (just Q Basic, and it has been a while since I have used that), and I need help on an assignment in my C++ class.
We have to use a case function to collect information for an order and display the total, but I have no clue how to get the case to work at all. I am sure the solution is simple, but as I have said I really do not know much about what I am doing.
And... here is the code:
C++ Syntax (Toggle Plain Text)
double S=2.50; double C=1.00; double B=2.00; double R=1.00; double L=1.50; double &T; int menu(); int choices(); int main() { menu(); choices(); system ("pause"); return 0; } int choices() { for (n=1, n++) { do { case 'S' case 's': T=T+S; break; case 'C' case 'c': T=T+C; case 'B' case 'b': T=T+B; break; case 'R' case 'r': T=T+R; break; case 'L' case 'l': T=T+L; break; case 'X' case 'x' T=0; cout << "The Order Has Been Canceled. \n" break; } } }
I already have the int menu() function working fine, it is the case stuff I am struggling with.
Hmm you are using the switch syntax wrong...try this:
C++ Syntax (Toggle Plain Text)
switch(tolower(choice)) { case 's': T=T+S; break; case 'c': T=T+C; case 'b': T=T+B; break; case 'r': T=T+R; break; case 'l': T=T+L; break; case 'x' T=0; cout << "The Order Has Been Canceled. \n" break; default: cout << "Invalid choice"; break; }
Also, you should either be passing choice an argument, or have a cin input a char to use in the switch statement. Perhaps have menu() return a char, and pass it to choices for testing. Here's what I mean :
C++ Syntax (Toggle Plain Text)
#include <iostream> using std::cin; using std::cout; double S=2.50; double C=1.00; double B=2.00; double R=1.00; double L = 1.50; double T(0); char Menu(); void Choices(char); int main() { Choices(Menu()); return 0; } char Menu() { char cChoice(0); cout << "Menu:\n\nA)Do Something\nB)Do Something\nC)Quit\n\nChoice:"; do { cin.clear(); cin.ignore(cin.rdbuf()->in_avail()); cin >> cChoice; } while (!cin.fail() && cin.rdbuf()->in_avail() > 1); //just makes sure you didn't screw up the input buffer with some crappy input return cChoice; } void Choices(char choice) { switch(tolower(choice)) { case 's': T=T+S; break; case 'c': T=T+C; case 'b': T=T+B; break; case 'r': T=T+R; break; case 'l': T=T+L; break; case 'x': T=0; cout << "The Order Has Been Canceled. \n"; break; default: cout << "Invalid choice"; break; } }
Don't expect this to compile; I didn't test it. But try and learn from it, as you had alot of syntax errors!
Last edited by skatamatic; Oct 27th, 2008 at 9:15 pm.
![]() |
Similar Threads
- Are there any cases out there as nice as the antec p180 for a lesser price? (Cases, Fans and Power Supplies)
- dell cpu cases (Cases, Fans and Power Supplies)
- cases (Cases, Fans and Power Supplies)
- Anyone heard of these cases? (Cases, Fans and Power Supplies)
- Any cases you guys know of that have a dual-ported powersupply slots? (Cases, Fans and Power Supplies)
- Hardware - Cases - Want to separate components (Troubleshooting Dead Machines)
Other Threads in the C++ Forum
- Previous Thread: std::copy a std::vector of std::pair to std::cout
- Next Thread: Clash RPG Help
| Thread Tools | Search this Thread |
api array based binary c++ c/c++ calculator char char* class classes code coding compile console conversion count database delete deploy desktop developer directshow dll download dynamic dynamiccharacterarray email encryption error file forms fstream function functions game givemetehcodez google graph gui homeworkhelp iamthwee ifstream input int integer java lib linkedlist linker linux list loop looping loops map math matrix memory multiple news number numbertoword output pointer problem program programming project python random read recursion recursive reference return rpg sorting string strings struct temperature template templates test text text-file tree unix url variable vector video visual visualstudio win32 windows winsock wordfrequency wxwidgets





