The switch statement

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: Feb 2007
Posts: 10
Reputation: tikal777 is an unknown quantity at this point 
Solved Threads: 0
tikal777's Avatar
tikal777 tikal777 is offline Offline
Newbie Poster

The switch statement

 
0
  #1
Mar 4th, 2007
Hi to DaniWeb. I've been checking out the forums from time to time to get help with C++. I'm a total beginner in C++, the only other language I have any knowledge of would be HTML, which is getting old. Anyways, I'm in a class that is requiring an assigment using the switch. When using the switch, can the statements in the case statements be whatever code statements you want executed if that respective choice is selected, or just simple cout statments?, (this is what I have in mind)
  1. switch(process_choice)
  2. {
  3. case 1: // menu choice 1 - process transaction selected
  4. customer_no++; // customer_no accum
  5. cout << "Customer #: "<< customer_no << endl; // display Customer number/transaction
  6. cout << endl;
  7.  
  8. cout << "Please enter customer type (I-Internet, L-Local) : " << endl; // Select Customer Type I or L
  9. cin >> customer_type;
  10. cout << endl;
  11.  
  12. switch(customer_type) // decision making process for customer type (discout or shipping charges)
  13. {
  14. case 'i':
  15. case 'I':
  16. if(total_sales_int < 50.00)
  17. {
  18. shipping = 3.00; // declaration of shipping charge for this sale amount
  19. final_charge_int = total_sales_int + shipping; // final charge for internet customer
  20. }
  21. else
  22. shipping = 0.00;
  23. break;
  24. case 'L':
  25. case 'l':
  26.  
  27. /* Loop for local customer discount calculation BEGIN */
  28. if(tot_chg_pre_disc > 60.00)
  29. {final_charge_loc = (tot_chg_pre_disc + (tot_chg_pre_disc * DISC_10));}
  30. if((tot_chg_pre_disc > 30.00) && (tot_chg_pre_disc < 59.00))
  31. {final_charg_loc = (tot_chg_pre_disc + (tot_chg_pre_disc * DISC_5));}
  32. break;
  33. /* Loop for local customer discount calculation END */
  34. default:
  35. cout << endl << endl;
  36. cout << "You Entered an Incorrect Response. Try Again!" << endl;
  37. break;
  38. }
  39. case 2:
  40. cout << "you really don't know what you're doing" << endl;
  41. break;
  42. default:
  43. cout << "You Entered an Incorrect Response. Try Again!" << endl;
  44. break;
  45. }

If I should add any more information to my post please feel free to tell me. I'm a total novice at this. I just need a few pointers. The assignment deals with decision making, focusing on using the if, if-else, and switch statements. We are to write a program that displays a menu that asks us to either process Local or Internet customers (with a local discount program & and a shipping charge for Internet customers) or to display an overall report. The products sold are 4 books. I don't know why I just wrote all that, maybe to give whoever reads that part of the code an idea. I'm not looking for step by step help or anything like that, just if whats in my code is correct, almost correct, way off, or whatever you know. Enough rambling.

Any help would be appreciated. Thanks

TG
Reply With Quote Quick reply to this message  
Join Date: Jan 2007
Posts: 171
Reputation: Lazaro Claiborn is an unknown quantity at this point 
Solved Threads: 13
Lazaro Claiborn's Avatar
Lazaro Claiborn Lazaro Claiborn is offline Offline
Junior Poster

Re: The switch statement

 
0
  #2
Mar 4th, 2007
Originally Posted by tikal777 View Post
Hi to DaniWeb. I've been checking out the forums from time to time to get help with C++. I'm a total beginner in C++, the only other language I have any knowledge of would be HTML, which is getting old. Anyways, I'm in a class that is requiring an assigment using the switch. When using the switch, can the statements in the case statements be whatever code statements you want executed if that respective choice is selected, or just simple cout statments?, (this is what I have in mind)
  1. switch(process_choice)
  2. {
  3. case 1: // menu choice 1 - process transaction selected
  4. customer_no++; // customer_no accum
  5. cout << "Customer #: "<< customer_no << endl; // display Customer number/transaction
  6. cout << endl;
  7.  
  8. cout << "Please enter customer type (I-Internet, L-Local) : " << endl; // Select Customer Type I or L
  9. cin >> customer_type;
  10. cout << endl;
  11.  
  12. switch(customer_type) // decision making process for customer type (discout or shipping charges)
  13. {
  14. case 'i':
  15. case 'I':
  16. if(total_sales_int < 50.00)
  17. {
  18. shipping = 3.00; // declaration of shipping charge for this sale amount
  19. final_charge_int = total_sales_int + shipping; // final charge for internet customer
  20. }
  21. else
  22. shipping = 0.00;
  23. break;
  24. case 'L':
  25. case 'l':
  26.  
  27. /* Loop for local customer discount calculation BEGIN */
  28. if(tot_chg_pre_disc > 60.00)
  29. {final_charge_loc = (tot_chg_pre_disc + (tot_chg_pre_disc * DISC_10));}
  30. if((tot_chg_pre_disc > 30.00) && (tot_chg_pre_disc < 59.00))
  31. {final_charg_loc = (tot_chg_pre_disc + (tot_chg_pre_disc * DISC_5));}
  32. break;
  33. /* Loop for local customer discount calculation END */
  34. default:
  35. cout << endl << endl;
  36. cout << "You Entered an Incorrect Response. Try Again!" << endl;
  37. break;
  38. }
  39. case 2:
  40. cout << "you really don't know what you're doing" << endl;
  41. break;
  42. default:
  43. cout << "You Entered an Incorrect Response. Try Again!" << endl;
  44. break;
  45. }

If I should add any more information to my post please feel free to tell me. I'm a total novice at this. I just need a few pointers. The assignment deals with decision making, focusing on using the if, if-else, and switch statements. We are to write a program that displays a menu that asks us to either process Local or Internet customers (with a local discount program & and a shipping charge for Internet customers) or to display an overall report. The products sold are 4 books. I don't know why I just wrote all that, maybe to give whoever reads that part of the code an idea. I'm not looking for step by step help or anything like that, just if whats in my code is correct, almost correct, way off, or whatever you know. Enough rambling.

Any help would be appreciated. Thanks

TG
Hello and welcome,

Yes any code before the break of a given case in a switch condition can contain any code you like. The nature of switch is to continue executing either until a break is encountered or the end of the switch condition which is ended with the "}" character. For example you can do something like this:

  1. char id = 'a';
  2. switch (a) {
  3. case 'a' :
  4. // This serves no purpose but it is valid
  5. switch (id) {
  6. case 'a' :
  7. cout << "It is a!!!!" << endl;
  8. break;
  9. }
  10. break;
  11. case 'b' :
  12. cout << "It is b!!!!" << endl;
  13. break;
  14. case 'c' :
  15. cout << "It is c!!!!" << endl;
  16. break;
  17. default : // this is optional
  18. cout << "It is nothing!!!! :(" << endl;
  19. break;
  20. }

Good luck, LamaBot
Last edited by Lazaro Claiborn; Mar 4th, 2007 at 11:54 pm.
Reply With Quote Quick reply to this message  
Join Date: Feb 2007
Posts: 10
Reputation: tikal777 is an unknown quantity at this point 
Solved Threads: 0
tikal777's Avatar
tikal777 tikal777 is offline Offline
Newbie Poster

Re: The switch statement

 
0
  #3
Mar 5th, 2007
Wow, that extremely quick. Thanks a lot for that. Anyways, I'll definitely keep that in mind as I'm working on it. If I run into any problems, hope not, but I know I probably will I'll check back in. Thanks again.
Reply With Quote Quick reply to this message  
Join Date: May 2006
Posts: 1,580
Reputation: Infarction has a spectacular aura about Infarction has a spectacular aura about Infarction has a spectacular aura about 
Solved Threads: 52
Infarction's Avatar
Infarction Infarction is offline Offline
Battle Programmer

Re: The switch statement

 
0
  #4
Mar 5th, 2007
A switch is basically a nice clean version of an if-elseif-else chain. Consider the following:
  1. switch(youVar)
  2. {
  3. case 0:
  4. foo();
  5. // do some more stuff
  6. break;
  7. case 1:
  8. // do stuff
  9. break;
  10. case 2:
  11. // do more stuff
  12. break;
  13. default:
  14. // handle accordingly
  15. break; // not so important for the last one
  16. }
This code is logically the same as above:
  1. if(yourVar == 0) {
  2. foo();
  3. // do some more stuff
  4. } else if(yourVar == 1) {
  5. // do stuff
  6. } else if(yourVar == 2) {
  7. // do more stuff
  8. } else /* default case */ {
  9. // handle accordingly
  10. }
There's an intricacy to switches involving case fall-through, but we won't go there yet
Last edited by Infarction; Mar 5th, 2007 at 4:13 am.
Reply With Quote Quick reply to this message  
Join Date: Feb 2007
Posts: 10
Reputation: tikal777 is an unknown quantity at this point 
Solved Threads: 0
tikal777's Avatar
tikal777 tikal777 is offline Offline
Newbie Poster

Re: The switch statement

 
0
  #5
Mar 6th, 2007
Thanks for that information. Much appreciated.
TG
Reply With Quote Quick reply to this message  
Join Date: Feb 2007
Posts: 10
Reputation: tikal777 is an unknown quantity at this point 
Solved Threads: 0
tikal777's Avatar
tikal777 tikal777 is offline Offline
Newbie Poster

Re: The switch statement

 
0
  #6
Mar 6th, 2007
i think I need more help, but I don't know whether to post my question here or to start a new thread? I'm gonna do both, sorry in advance if i'm not supposed to do that. Thanks again.
Reply With Quote Quick reply to this message  
Join Date: Feb 2007
Posts: 10
Reputation: tikal777 is an unknown quantity at this point 
Solved Threads: 0
tikal777's Avatar
tikal777 tikal777 is offline Offline
Newbie Poster

Re: The switch statement

 
0
  #7
Mar 6th, 2007
Anyways, I have the bulk of the code done, I just need a few tweaks on the coding, or at least I think. All the output is fine, except that I don’t know if I’m setting up the counters & accumulators correctly. When I process the first transaction, the totals don’t calculate correctly in the final reports and in the current sale report. I don’t know what to do. Here is my code.

  1. // tgiddens_assig2.cpp
  2. // Assignment 2 Decision Making
  3. // Timothy Giddens
  4. // IS 2043.002 Spring 07
  5.  
  6.  
  7. #include <iostream>
  8. #include <iomanip>
  9. #include <cstdlib>
  10.  
  11. using namespace std;
  12.  
  13. int main()
  14. {
  15. // individual book price constants
  16. const double PR_LASSI = 7.25,
  17. PR_ROBIN = 9.75,
  18. PR_OLIVE = 6.75,
  19. PR_LITTL = 5.50,
  20. DISC_5 = 0.05, // %5 discount for locals
  21. DISC_10 = 0.10; // %10 discount for locals
  22.  
  23. // integer counter variables
  24. int count_customer_no = 0,
  25. count_total_books = 0,
  26. count_lassie_bk = 0,
  27. count_robin_bk = 0,
  28. count_olive_bk = 0,
  29. count_littl_bk = 0;
  30.  
  31. // Display menu selector
  32. int selection; // user enters 1 or 2
  33.  
  34.  
  35. // various charges variables
  36. double sales_las = 0.00,
  37. sales_rob = 0.00,
  38. sales_oli = 0.00,
  39. sales_lit = 0.00,
  40. grand_total_sales = 0.00,
  41. total_sales = 0.00,
  42. tot_chg_pre_disc = 0.00,
  43. final_charge = 0.00,
  44. discount = final_charge - total_sales,
  45. shipping = 0.00; //shipping charge for internet users
  46.  
  47. // selector variables
  48. char response = ' ', // main menu selector input; user enters 'Y'
  49. book_selection = ' ', // book menu switch selector input; user enters 'Y'
  50. book_response = ' ',
  51. select_las = '1',
  52. select_rob = '2',
  53. select_oli = '3',
  54. select_lit = '4',
  55. customer_type;
  56.  
  57. // set output display format
  58. cout << setprecision(2)
  59. << setiosflags(ios::fixed)
  60. << setiosflags(ios::showpoint);
  61. do
  62. {
  63. // display welcome menu & recv input for menu choice (1 or 2)
  64. cout << "Welcome to Johnson's Bookstore" << endl;
  65. cout << "==============================" << endl;
  66. cout << "1 - Process Transaction" << endl;
  67. cout << "2 - Display Overall Report" << endl << endl;
  68. cout << "Please enter your selection: ";
  69. cin >> selection;
  70. cout << endl;
  71.  
  72. //decision making process for menu choice and sub menu transactions
  73. if(selection == 1)
  74. {
  75. // menu choice 1 - process transaction selected
  76. count_customer_no++; // customer_no accum
  77. cout << "Customer #: "<< count_customer_no << endl; // display Customer number/transaction
  78. cout << endl;
  79.  
  80. cout << "Please enter customer type (I-Internet, L-Local) : "; // Select Customer Type I or L
  81. cin >> customer_type;
  82. cout << endl;
  83.  
  84. switch(customer_type) // decision making process for customer type (discout or shipping charges)
  85. {
  86. case 'i':
  87. case 'I':
  88. /* internet customer shipping calculations BEGIN */
  89. if(total_sales < 50.00)
  90. {
  91. shipping = 3.00; // declaration of shipping charge for this sale amount
  92. final_charge = total_sales + shipping; // final charge for internet customer
  93. }
  94. else
  95. shipping = 0.00;
  96. break;
  97. /* internet customer shipping calculations END */
  98. case 'L':
  99. case 'l':
  100.  
  101. /* local customer discount calculation BEGIN */
  102. if(total_sales > 60.00)
  103. {
  104. final_charge = (total_sales - (total_sales * DISC_10));
  105. }
  106. else if((total_sales > 30.00) && (total_sales < 59.99))
  107. {
  108. final_charge = (total_sales - (total_sales * DISC_5));
  109. }
  110. break;
  111. /* local customer discount calculation END */
  112. default:
  113. cout << endl;
  114. cout << "Incorrect Customer Type! Try Again!" << endl;
  115. break;
  116. }
  117. do // loop for displaying books
  118. {
  119. // display list of books
  120. cout << "Available Titles" << endl;
  121. cout << "================" << endl;
  122. cout << "1 - Lassie ($7.25)" << endl;
  123. cout << "2 - Robinson Crusoe ($9.75)" << endl;
  124. cout << "3 - Oliver Twist ($6.75)" << endl;
  125. cout << "4 - Little Women ($5.50)" << endl;
  126. cout << endl;
  127. cout << "Please enter your selection: ";
  128. cin.get();
  129. book_selection = cin.get();
  130.  
  131. switch(book_selection)
  132. {
  133. case '1':
  134. cout << "Enter the number of Lassie books sold: ";
  135. cin >> count_lassie_bk;
  136. break;
  137. case '2':
  138. cout << "Enter the number of Robinson Crusoe books sold: ";
  139. cin >> count_robin_bk;
  140. break;
  141. case '3':
  142. cout << "Enter the number of Oliver Twist books sold: ";
  143. cin >> count_olive_bk;
  144. break;
  145. case '4':
  146. cout << "Enter the number of Little Women books sold: ";
  147. cin >> count_littl_bk;
  148. break;
  149. default:
  150. cout << "Incorrect selection! Try Again." << endl;
  151. break;
  152. }
  153.  
  154. // Prompt user to continue with another book
  155. cout << "Do you want to continue with another book? (Y to continue or S to stop): ";
  156. cin >> book_response;
  157. cout << endl;
  158.  
  159. }
  160. while(book_response == 'Y' || book_response == 'y');
  161.  
  162. // perform calculations
  163. sales_las = (count_lassie_bk * PR_LASSI);
  164. sales_rob = (count_robin_bk * PR_ROBIN);
  165. sales_oli = (count_olive_bk * PR_OLIVE);
  166. sales_lit = (count_littl_bk * PR_LITTL);
  167. count_total_books = (count_lassie_bk + count_robin_bk + count_olive_bk + count_littl_bk);
  168. total_sales = (sales_las + sales_oli + sales_rob + sales_lit);
  169. grand_total_sales += total_sales;
  170.  
  171. // display current sale report
  172. if(book_response == 's' || book_response == 'S')
  173. {
  174. cout << "The customer has purchased:" << endl;
  175. cout << "(" << count_lassie_bk << ")" << " Lassie" << endl;
  176. cout << "(" << count_robin_bk << ")" << " Robinson Crusoe" << endl;
  177. cout << "(" << count_olive_bk << ")" << " Oliver Twist" << endl;
  178. cout << "(" << count_littl_bk << ")" << " Little Women" << endl << endl;
  179. cout << "The sale is: $" << total_sales << endl;
  180. cout << "The shipping cost is: $" << shipping << endl;
  181. cout << "The discount given is: $" << discount << endl;
  182. cout << "The final charge is: $" << final_charge << endl << endl;
  183. }
  184.  
  185. // Prompt user to continue with another transaction
  186. cout << "Do you want to continue with the program? (Y to continue):";
  187. cin >> response;
  188. cout << endl;
  189. }
  190. else
  191. if(selection == 2)
  192. {
  193. cout << "Final Report:" << endl;
  194. cout << endl;
  195. cout << " Title Total Sales" << endl;
  196. cout << "===============================" << endl;
  197. cout << " Lassie" << setw(7) << count_lassie_bk << setw(7) << sales_las << endl;
  198. cout << " Robinson Crusoe" << setw(7) << count_robin_bk << setw(7) << sales_rob << endl;
  199. cout << " Oliver Twist" << setw(7) << count_olive_bk << setw(7) << sales_oli << endl;
  200. cout << " Little Women" << setw(7) << count_littl_bk << setw(7) << sales_lit << endl;
  201. cout << "===============================" << endl;
  202. cout << " Grand Total" << setw(7) << count_total_books << setw(7) << grand_total_sales << endl;
  203. }
  204. else
  205. cout << "You Entered an Incorrect Response. Try Again!" << endl;
  206. }
  207.  
  208. while(response == 'Y' || response == 'y');
  209. return 0;
  210. }


If this is too much to look over, I understand. I just can’t figure out how to get the totals correctly. Any suggestions would be great.

TG
Reply With Quote Quick reply to this message  
Join Date: May 2006
Posts: 1,580
Reputation: Infarction has a spectacular aura about Infarction has a spectacular aura about Infarction has a spectacular aura about 
Solved Threads: 52
Infarction's Avatar
Infarction Infarction is offline Offline
Battle Programmer

Re: The switch statement

 
0
  #8
Mar 6th, 2007
It's 'cause you're calculating the shipping and total cost before the loop. You need to loop and get all the books before calculating the costs. A little re-arranging will probably fix it up.

[edit:] and for the record, it's better to reuse a thread if it's on topic. Otherwise it'll seem like a double post, which is bad.
Last edited by Infarction; Mar 6th, 2007 at 4:27 am.
Reply With Quote Quick reply to this message  
Join Date: Feb 2007
Posts: 10
Reputation: tikal777 is an unknown quantity at this point 
Solved Threads: 0
tikal777's Avatar
tikal777 tikal777 is offline Offline
Newbie Poster

Re: The switch statement

 
0
  #9
Mar 6th, 2007
cool man, thanks again...
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



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

©2003 - 2009 DaniWeb® LLC