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: NinjaDude007 is an unknown quantity at this point 
Solved Threads: 0
NinjaDude007 NinjaDude007 is offline Offline
Newbie Poster

I Need Help with Cases in C++.

 
0
  #1
Oct 27th, 2008
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:

  1. double S=2.50;
  2. double C=1.00;
  3. double B=2.00;
  4. double R=1.00;
  5. double L=1.50;
  6. double &T;
  7.  
  8. int menu();
  9. int choices();
  10. int main()
  11. {
  12. menu();
  13. choices();
  14. system ("pause");
  15. return 0;
  16.  
  17. }
  18. int choices()
  19. {
  20. for (n=1, n++)
  21. {
  22. do
  23. {
  24. case 'S'
  25. case 's':
  26. T=T+S;
  27. break;
  28. case 'C'
  29. case 'c':
  30. T=T+C;
  31. case 'B'
  32. case 'b':
  33. T=T+B;
  34. break;
  35. case 'R'
  36. case 'r':
  37. T=T+R;
  38. break;
  39. case 'L'
  40. case 'l':
  41. T=T+L;
  42. break;
  43. case 'X'
  44. case 'x'
  45. T=0;
  46. cout << "The Order Has Been Canceled. \n"
  47. break;
  48. }
  49. }
  50. }

I already have the int menu() function working fine, it is the case stuff I am struggling with.
Reply With Quote Quick reply to this message  
Join Date: Nov 2007
Posts: 390
Reputation: skatamatic will become famous soon enough skatamatic will become famous soon enough 
Solved Threads: 39
skatamatic skatamatic is offline Offline
Posting Whiz

Re: I Need Help with Cases in C++.

 
0
  #2
Oct 27th, 2008
Originally Posted by NinjaDude007 View Post
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:

  1. double S=2.50;
  2. double C=1.00;
  3. double B=2.00;
  4. double R=1.00;
  5. double L=1.50;
  6. double &T;
  7.  
  8. int menu();
  9. int choices();
  10. int main()
  11. {
  12. menu();
  13. choices();
  14. system ("pause");
  15. return 0;
  16.  
  17. }
  18. int choices()
  19. {
  20. for (n=1, n++)
  21. {
  22. do
  23. {
  24. case 'S'
  25. case 's':
  26. T=T+S;
  27. break;
  28. case 'C'
  29. case 'c':
  30. T=T+C;
  31. case 'B'
  32. case 'b':
  33. T=T+B;
  34. break;
  35. case 'R'
  36. case 'r':
  37. T=T+R;
  38. break;
  39. case 'L'
  40. case 'l':
  41. T=T+L;
  42. break;
  43. case 'X'
  44. case 'x'
  45. T=0;
  46. cout << "The Order Has Been Canceled. \n"
  47. break;
  48. }
  49. }
  50. }

I already have the int menu() function working fine, it is the case stuff I am struggling with.
I don't understand your for loop (which is also syntactically erronous) , or the do loop. Just get rid of them for now, and implement them later if need be.

Hmm you are using the switch syntax wrong...try this:

  1. switch(tolower(choice))
  2. { case 's':
  3. T=T+S;
  4. break;
  5. case 'c':
  6. T=T+C;
  7. case 'b':
  8. T=T+B;
  9. break;
  10. case 'r':
  11. T=T+R;
  12. break;
  13. case 'l':
  14. T=T+L;
  15. break;
  16. case 'x'
  17. T=0;
  18. cout << "The Order Has Been Canceled. \n"
  19. break;
  20. default:
  21. cout << "Invalid choice";
  22. break;
  23. }

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 :

  1. #include <iostream>
  2. using std::cin;
  3. using std::cout;
  4. double S=2.50;
  5. double C=1.00;
  6. double B=2.00;
  7. double R=1.00;
  8. double L = 1.50;
  9. double T(0);
  10.  
  11. char Menu();
  12. void Choices(char);
  13. int main()
  14. {
  15. Choices(Menu());
  16. return 0;
  17. }
  18.  
  19. char Menu()
  20. {
  21. char cChoice(0);
  22. cout << "Menu:\n\nA)Do Something\nB)Do Something\nC)Quit\n\nChoice:";
  23. do {
  24. cin.clear();
  25. cin.ignore(cin.rdbuf()->in_avail());
  26. cin >> cChoice;
  27. } while (!cin.fail() && cin.rdbuf()->in_avail() > 1);
  28. //just makes sure you didn't screw up the input buffer with some crappy input
  29. return cChoice;
  30. }
  31.  
  32. void Choices(char choice)
  33. {
  34. switch(tolower(choice))
  35. { case 's':
  36. T=T+S;
  37. break;
  38. case 'c':
  39. T=T+C;
  40. case 'b':
  41. T=T+B;
  42. break;
  43. case 'r':
  44. T=T+R;
  45. break;
  46. case 'l':
  47. T=T+L;
  48. break;
  49. case 'x':
  50. T=0;
  51. cout << "The Order Has Been Canceled. \n";
  52. break;
  53. default:
  54. cout << "Invalid choice";
  55. break;
  56. }
  57. }

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.
Reply With Quote Quick reply to this message  
Join Date: Oct 2008
Posts: 2
Reputation: NinjaDude007 is an unknown quantity at this point 
Solved Threads: 0
NinjaDude007 NinjaDude007 is offline Offline
Newbie Poster

Re: I Need Help with Cases in C++.

 
0
  #3
Oct 28th, 2008
Thanks. I finally got it figured out and working right... somehow.
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:



Similar Threads
Other Threads in the C++ Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC