View Single Post
Join Date: Nov 2008
Posts: 43
Reputation: davids2004 is an unknown quantity at this point 
Solved Threads: 0
davids2004 davids2004 is offline Offline
Light Poster

How can I add a loop for this code

 
0
  #1
Dec 1st, 2008
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

  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.
Reply With Quote