943,706 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Marked Solved
  • Views: 1087
  • C++ RSS
You are currently viewing page 1 of this multi-page discussion thread
Dec 1st, 2008
0

How can I add a loop for this code

Expand Post »
The code is pretty easy to understand what I am doing. You basically pick a number 1-4 and it will do either addition, subtraction, multiplication, or division. What I need it to do is after they do a problem it loops back and lets them do it again, until they enter 5 to end the program.

Thanks.

Here is my code

C++ Syntax (Toggle Plain Text)
  1. #include <iostream>
  2. #include <cstdlib>
  3. #include <iomanip>
  4. #include <ctime>
  5.  
  6. using namespace std;
  7.  
  8.  
  9. int main ()
  10.  
  11. {
  12. srand((unsigned)time(0));
  13. int add1, add2, sub1, sub2;
  14. int multiply1, multiply2, div1, div2;
  15. int choice, input;
  16. add1 = (rand()%500)+1;
  17. add2 = (rand()%500)+1;
  18. sub1 = (rand()%500)+1;
  19. sub2 = (rand()%98)+1;
  20. multiply1 = (rand()%98)+1;
  21. multiply2 = (rand()%8)+1;
  22. div1 = (rand()%500)+1;
  23. div2 = (rand()%8)+1;
  24. int addAnswer = add1 + add2;
  25. int subAnswer = sub1- sub2;
  26. int multiplyAnswer = multiply1 * multiply2;
  27. int divAnswer = div1 / div2;
  28.  
  29.  
  30.  
  31. //Display the menu and get the user's choice
  32. cout << " Geometry Calculator \n\n";
  33. cout << "1. Addition\n";
  34. cout << "2. Subtraction\n";
  35. cout << "3. Multiplication\n";
  36. cout << "4. Division\n";
  37. cout << "5. Quit\n";
  38. cout << "\n";
  39.  
  40. cout <<"Enter your choice (1-5): ";
  41. cin >> choice;
  42. cout << "\n";
  43.  
  44.  
  45. if (choice==1)
  46. {
  47. cout << setw (6) << add1 << endl;
  48. cout << "+ ";
  49. cout << setw (4) << add2 << endl;
  50. cout << "------";
  51. cout << "\n";
  52. cin >> input;
  53. cout << "\n";
  54. if
  55. (input == addAnswer)
  56. cout << "Congratulations\n";
  57. else
  58. cout << "That is incorrect,\nThe correct answer is: " << addAnswer;
  59. cout << "\n";
  60. return 0;
  61. }
  62.  
  63. else if (choice==2)
  64. {
  65. cout << setw (6) << sub1 << endl;
  66. cout << "- ";
  67. cout << setw (4) << sub2 << endl;
  68. cout << "------";
  69. cout << "\n";
  70. cin >> input;
  71. cout << "\n";
  72. if
  73. (input == subAnswer)
  74. cout << "Congratulations\n";
  75. else
  76. cout << "That is incorrect,\nThe correct answer is: " << subAnswer;
  77. cout << "\n";
  78. return 0;}
  79.  
  80. else if (choice==3)
  81. {
  82. cout << setw (6) << multiply1 << endl;
  83. cout << "* ";
  84. cout << setw (4) << multiply2 << endl;
  85. cout << "------";
  86. cout << "\n";
  87. cin >> input;
  88. cout << "\n";
  89. if
  90. (input == multiplyAnswer)
  91. cout << "Congratulations\n";
  92. else
  93. cout << "That is incorrect,\nThe correct answer is: " << multiplyAnswer;
  94. cout << "\n";
  95. return 0;}
  96.  
  97. else if (choice==4)
  98. {
  99. cout << setw (6) << div1 << endl;
  100. cout << "/ ";
  101. cout << setw (4) << div2 << endl;
  102. cout << "------";
  103. cout << "\n";
  104. cin >> input;
  105. cout << "\n";
  106. if
  107. (input == divAnswer)
  108. cout << "Congratulations\n";
  109. else
  110. cout << "That is incorrect,\nThe correct answer is: " << divAnswer;
  111. cout << "\n";
  112. return 0;}
  113.  
  114. else if (choice==5)
  115. {cout << "Thanks for playing\n";}
  116.  
  117. else
  118. {cout << "You can only select options 1-5, run the program again and select a option 1-5\n";}
  119.  
  120.  
  121. return 0;
  122.  
  123. }
Last edited by davids2004; Dec 1st, 2008 at 1:33 pm.
Similar Threads
Reputation Points: 10
Solved Threads: 0
Light Poster
davids2004 is offline Offline
43 posts
since Nov 2008
Dec 1st, 2008
0

Re: How can I add a loop for this code

Simply..
C++ Syntax (Toggle Plain Text)
  1. //...
  2. char rep='n';
  3. //display the menu and get user's choice
  4. while(tolower(rep)=='n')
  5. {
  6. //...Menu
  7. cout<<"repeat? [y/n] : ";
  8. cin>>rep;
  9. }
  10. return 0; //bye..
Reputation Points: 47
Solved Threads: 69
Posting Whiz
cikara21 is offline Offline
340 posts
since Jul 2008
Dec 1st, 2008
0

Re: How can I add a loop for this code

[code] while (choice!=5)
{
Your Programme Body

}[\code]

Remember to Intialize Choice With any Number without 5
Reputation Points: 188
Solved Threads: 44
Posting Pro
Majestics is offline Offline
554 posts
since Jul 2007
Dec 1st, 2008
0

Re: How can I add a loop for this code

Click to Expand / Collapse  Quote originally posted by cikara21 ...
Simply..
C++ Syntax (Toggle Plain Text)
  1. //...
  2. char rep='n';
  3. //display the menu and get user's choice
  4. while(tolower(rep)=='n')
  5. {
  6. //...Menu
  7. cout<<"repeat? [y/n] : ";
  8. cin>>rep;
  9. }
  10. return 0; //bye..
Thanks for the help. Would I put that before the else if part of the code? Also the char rep='n'; needs to go at the top where I clarify everything. Also what would I do if I wanted it to either be a capital or lower case y or n?

Thanks.
Reputation Points: 10
Solved Threads: 0
Light Poster
davids2004 is offline Offline
43 posts
since Nov 2008
Dec 1st, 2008
0

Re: How can I add a loop for this code

C++ Syntax (Toggle Plain Text)
  1. while (choice!=5)
  2. {
  3. Your Programme Body
  4.  
  5. }

Remember to Intialize Choice With any Number without 5
Reputation Points: 188
Solved Threads: 44
Posting Pro
Majestics is offline Offline
554 posts
since Jul 2007
Dec 1st, 2008
0

Re: How can I add a loop for this code

Click to Expand / Collapse  Quote originally posted by Majestics ...
[code] while (choice!=5)
{
Your Programme Body

}[\code]

Remember to Intialize Choice With any Number without 5
What?
Reputation Points: 10
Solved Threads: 0
Light Poster
davids2004 is offline Offline
43 posts
since Nov 2008
Dec 1st, 2008
0

Re: How can I add a loop for this code

C++ Syntax (Toggle Plain Text)
  1.  
  2. #include <iostream>
  3.  
  4. #include <cstdlib>
  5.  
  6. #include <iomanip>
  7.  
  8. #include <ctime>
  9.  
  10.  
  11.  
  12. using namespace std;
  13.  
  14.  
  15.  
  16.  
  17.  
  18. int main ()
  19.  
  20.  
  21. {
  22.  
  23. srand((unsigned)time(0));
  24.  
  25. int add1, add2, sub1, sub2;
  26.  
  27. int multiply1, multiply2, div1, div2;
  28.  
  29. int choice=0, input;
  30.  
  31. add1 = (rand()%500)+1;
  32.  
  33. add2 = (rand()%500)+1;
  34.  
  35. sub1 = (rand()%500)+1;
  36.  
  37. sub2 = (rand()%98)+1;
  38.  
  39. multiply1 = (rand()%98)+1;
  40.  
  41. multiply2 = (rand()%8)+1;
  42.  
  43. div1 = (rand()%500)+1;
  44.  
  45. div2 = (rand()%8)+1;
  46.  
  47. int addAnswer = add1 + add2;
  48.  
  49. int subAnswer = sub1- sub2;
  50.  
  51. int multiplyAnswer = multiply1 * multiply2;
  52.  
  53. int divAnswer = div1 / div2;
  54.  
  55.  
  56.  
  57.  
  58.  
  59.  
  60.  
  61. //Display the menu and get the user's choice
  62.  
  63. while(choice!=5)
  64. {
  65. cout << " Geometry Calculator \n\n";
  66.  
  67. cout << "1. Addition\n";
  68.  
  69. cout << "2. Subtraction\n";
  70.  
  71. cout << "3. Multiplication\n";
  72.  
  73. cout << "4. Division\n";
  74.  
  75. cout << "5. Quit\n";
  76.  
  77. cout << "\n";
  78.  
  79.  
  80.  
  81. cout <<"Enter your choice (1-5): ";
  82.  
  83. cin >> choice;
  84.  
  85. cout << "\n";
  86.  
  87.  
  88.  
  89.  
  90.  
  91. if (choice==1)
  92.  
  93. {
  94.  
  95. cout << setw (6) << add1 << endl;
  96.  
  97. cout << "+ ";
  98.  
  99. cout << setw (4) << add2 << endl;
  100.  
  101. cout << "------";
  102.  
  103. cout << "\n";
  104.  
  105. cin >> input;
  106.  
  107. cout << "\n";
  108.  
  109. if
  110.  
  111. (input == addAnswer)
  112.  
  113. cout << "Congratulations\n";
  114.  
  115. else
  116.  
  117. cout << "That is incorrect,\nThe correct answer is: " << addAnswer;
  118.  
  119. cout << "\n";
  120.  
  121. return 0;
  122.  
  123. }
  124.  
  125.  
  126.  
  127. else if (choice==2)
  128.  
  129. {
  130.  
  131. cout << setw (6) << sub1 << endl;
  132.  
  133. cout << "- ";
  134.  
  135. cout << setw (4) << sub2 << endl;
  136.  
  137. cout << "------";
  138.  
  139. cout << "\n";
  140.  
  141. cin >> input;
  142.  
  143. cout << "\n";
  144.  
  145. if
  146.  
  147. (input == subAnswer)
  148.  
  149. cout << "Congratulations\n";
  150.  
  151. else
  152.  
  153. cout << "That is incorrect,\nThe correct answer is: " << subAnswer;
  154.  
  155. cout << "\n";
  156.  
  157. return 0;}
  158.  
  159.  
  160.  
  161. else if (choice==3)
  162.  
  163. {
  164.  
  165. cout << setw (6) << multiply1 << endl;
  166.  
  167. cout << "* ";
  168.  
  169. cout << setw (4) << multiply2 << endl;
  170.  
  171. cout << "------";
  172.  
  173. cout << "\n";
  174.  
  175. cin >> input;
  176.  
  177. cout << "\n";
  178.  
  179. if
  180.  
  181. (input == multiplyAnswer)
  182.  
  183. cout << "Congratulations\n";
  184.  
  185. else
  186.  
  187. cout << "That is incorrect,\nThe correct answer is: " << multiplyAnswer;
  188.  
  189. cout << "\n";
  190.  
  191. return 0;}
  192.  
  193.  
  194.  
  195. else if (choice==4)
  196.  
  197. {
  198.  
  199. cout << setw (6) << div1 << endl;
  200.  
  201. cout << "/ ";
  202.  
  203. cout << setw (4) << div2 << endl;
  204.  
  205. cout << "------";
  206.  
  207. cout << "\n";
  208.  
  209. cin >> input;
  210.  
  211. cout << "\n";
  212.  
  213. if
  214.  
  215. (input == divAnswer)
  216.  
  217. cout << "Congratulations\n";
  218.  
  219. else
  220.  
  221. cout << "That is incorrect,\nThe correct answer is: " << divAnswer;
  222.  
  223. cout << "\n";
  224.  
  225. return 0;}
  226.  
  227.  
  228.  
  229. else if (choice==5)
  230.  
  231. {cout << "Thanks for playing\n";}
  232.  
  233.  
  234.  
  235. else
  236.  
  237. {cout << "You can only select options 1-5, run the program again and select a option 1-5\n";}
  238.  
  239. }
  240.  
  241.  
  242.  
  243. return 0;
  244.  
  245.  
  246.  
  247. }
Reputation Points: 188
Solved Threads: 44
Posting Pro
Majestics is offline Offline
554 posts
since Jul 2007
Dec 1st, 2008
0

Re: How can I add a loop for this code

I was doing a similair problem, and with some help from people on here found that using a do-while loop with a switch instead of four if statements worked best. Check out the last page for some pointers...
http://www.daniweb.com/forums/thread159343.html
Reputation Points: 10
Solved Threads: 1
Newbie Poster
cout<<"alias" is offline Offline
16 posts
since Nov 2008
Dec 1st, 2008
0

Re: How can I add a loop for this code

Click to Expand / Collapse  Quote originally posted by Majestics ...
C++ Syntax (Toggle Plain Text)
  1.  
  2. #include <iostream>
  3.  
  4. #include <cstdlib>
  5.  
  6. #include <iomanip>
  7.  
  8. #include <ctime>
  9.  
  10.  
  11.  
  12. using namespace std;
  13.  
  14.  
  15.  
  16.  
  17.  
  18. int main ()
  19.  
  20.  
  21. {
  22.  
  23. srand((unsigned)time(0));
  24.  
  25. int add1, add2, sub1, sub2;
  26.  
  27. int multiply1, multiply2, div1, div2;
  28.  
  29. int choice=0, input;
  30.  
  31. add1 = (rand()%500)+1;
  32.  
  33. add2 = (rand()%500)+1;
  34.  
  35. sub1 = (rand()%500)+1;
  36.  
  37. sub2 = (rand()%98)+1;
  38.  
  39. multiply1 = (rand()%98)+1;
  40.  
  41. multiply2 = (rand()%8)+1;
  42.  
  43. div1 = (rand()%500)+1;
  44.  
  45. div2 = (rand()%8)+1;
  46.  
  47. int addAnswer = add1 + add2;
  48.  
  49. int subAnswer = sub1- sub2;
  50.  
  51. int multiplyAnswer = multiply1 * multiply2;
  52.  
  53. int divAnswer = div1 / div2;
  54.  
  55.  
  56.  
  57.  
  58.  
  59.  
  60.  
  61. //Display the menu and get the user's choice
  62.  
  63. while(choice!=5)
  64. {
  65. cout << " Geometry Calculator \n\n";
  66.  
  67. cout << "1. Addition\n";
  68.  
  69. cout << "2. Subtraction\n";
  70.  
  71. cout << "3. Multiplication\n";
  72.  
  73. cout << "4. Division\n";
  74.  
  75. cout << "5. Quit\n";
  76.  
  77. cout << "\n";
  78.  
  79.  
  80.  
  81. cout <<"Enter your choice (1-5): ";
  82.  
  83. cin >> choice;
  84.  
  85. cout << "\n";
  86.  
  87.  
  88.  
  89.  
  90.  
  91. if (choice==1)
  92.  
  93. {
  94.  
  95. cout << setw (6) << add1 << endl;
  96.  
  97. cout << "+ ";
  98.  
  99. cout << setw (4) << add2 << endl;
  100.  
  101. cout << "------";
  102.  
  103. cout << "\n";
  104.  
  105. cin >> input;
  106.  
  107. cout << "\n";
  108.  
  109. if
  110.  
  111. (input == addAnswer)
  112.  
  113. cout << "Congratulations\n";
  114.  
  115. else
  116.  
  117. cout << "That is incorrect,\nThe correct answer is: " << addAnswer;
  118.  
  119. cout << "\n";
  120.  
  121. return 0;
  122.  
  123. }
  124.  
  125.  
  126.  
  127. else if (choice==2)
  128.  
  129. {
  130.  
  131. cout << setw (6) << sub1 << endl;
  132.  
  133. cout << "- ";
  134.  
  135. cout << setw (4) << sub2 << endl;
  136.  
  137. cout << "------";
  138.  
  139. cout << "\n";
  140.  
  141. cin >> input;
  142.  
  143. cout << "\n";
  144.  
  145. if
  146.  
  147. (input == subAnswer)
  148.  
  149. cout << "Congratulations\n";
  150.  
  151. else
  152.  
  153. cout << "That is incorrect,\nThe correct answer is: " << subAnswer;
  154.  
  155. cout << "\n";
  156.  
  157. return 0;}
  158.  
  159.  
  160.  
  161. else if (choice==3)
  162.  
  163. {
  164.  
  165. cout << setw (6) << multiply1 << endl;
  166.  
  167. cout << "* ";
  168.  
  169. cout << setw (4) << multiply2 << endl;
  170.  
  171. cout << "------";
  172.  
  173. cout << "\n";
  174.  
  175. cin >> input;
  176.  
  177. cout << "\n";
  178.  
  179. if
  180.  
  181. (input == multiplyAnswer)
  182.  
  183. cout << "Congratulations\n";
  184.  
  185. else
  186.  
  187. cout << "That is incorrect,\nThe correct answer is: " << multiplyAnswer;
  188.  
  189. cout << "\n";
  190.  
  191. return 0;}
  192.  
  193.  
  194.  
  195. else if (choice==4)
  196.  
  197. {
  198.  
  199. cout << setw (6) << div1 << endl;
  200.  
  201. cout << "/ ";
  202.  
  203. cout << setw (4) << div2 << endl;
  204.  
  205. cout << "------";
  206.  
  207. cout << "\n";
  208.  
  209. cin >> input;
  210.  
  211. cout << "\n";
  212.  
  213. if
  214.  
  215. (input == divAnswer)
  216.  
  217. cout << "Congratulations\n";
  218.  
  219. else
  220.  
  221. cout << "That is incorrect,\nThe correct answer is: " << divAnswer;
  222.  
  223. cout << "\n";
  224.  
  225. return 0;}
  226.  
  227.  
  228.  
  229. else if (choice==5)
  230.  
  231. {cout << "Thanks for playing\n";}
  232.  
  233.  
  234.  
  235. else
  236.  
  237. {cout << "You can only select options 1-5, run the program again and select a option 1-5\n";}
  238.  
  239. }
  240.  
  241.  
  242.  
  243. return 0;
  244.  
  245.  
  246.  
  247. }
I tried to see what you did and tried it but it did not work.
Reputation Points: 10
Solved Threads: 0
Light Poster
davids2004 is offline Offline
43 posts
since Nov 2008
Dec 1st, 2008
0

Re: How can I add a loop for this code

Ok I just can not get the loop to work. I tried what was in the second post but it does not work. Please help. I am stuck on the code I already posted.

Thanks.
Reputation Points: 10
Solved Threads: 0
Light Poster
davids2004 is offline Offline
43 posts
since Nov 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: Function 'initgraph' should have a prototype
Next Thread in C++ Forum Timeline: Program on strings - Can anyone solve this program?





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


Follow us on Twitter


© 2011 DaniWeb® LLC