943,872 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Marked Solved
  • Views: 451
  • C++ RSS
Oct 27th, 2008
0

I Need Help with Cases in C++.

Expand 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:

C++ Syntax (Toggle Plain Text)
  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.
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
NinjaDude007 is offline Offline
2 posts
since Oct 2008
Oct 27th, 2008
0

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

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)
  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:

C++ Syntax (Toggle Plain Text)
  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 :

C++ Syntax (Toggle Plain Text)
  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.
Reputation Points: 352
Solved Threads: 109
Master Poster
skatamatic is offline Offline
777 posts
since Nov 2007
Oct 28th, 2008
0

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

Thanks. I finally got it figured out and working right... somehow.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
NinjaDude007 is offline Offline
2 posts
since Oct 2008

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: std::copy a std::vector of std::pair to std::cout
Next Thread in C++ Forum Timeline: Clash RPG Help





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC