![]() |
| ||
| Re: How can I add a loop for this code I dont know if you already figure it out, but here it is... .../ your code int addAnswer = add1 + add2; int subAnswer = sub1- sub2; int multiplyAnswer = multiply1 * multiply2; int divAnswer = div1 / div2; choice=6; // this is what I added while (choice!=5) { // the loop check the program, if you notice everytime the user makes a choice, at the end you terminate the program, what you need to do is to initialize "choice =6" and delete "return 0" for everyoption... check this out: ../ your code showing the menu if (choice==1) { cout << setw (6) << add1 << endl; cout << "+ "; cout << setw (4) << add2 << endl; cout << "------"; cout << "\n"; cin >> input; cout << "\n"; if (input == addAnswer) cout << "Congratulations\n"; else cout << "That is incorrect,\nThe correct answer is: " << addAnswer; cout << "\n"; choice=6; } else if (choice==2) { cout << setw (6) << sub1 << endl; cout << "- "; cout << setw (4) << sub2 << endl; cout << "------"; cout << "\n"; cin >> input; cout << "\n"; if (input == subAnswer) cout << "Congratulations\n"; else cout << "That is incorrect,\nThe correct answer is: " << subAnswer; cout << "\n"; choice=6; } else if (choice==3) { cout << setw (6) << multiply1 << endl; cout << "* "; cout << setw (4) << multiply2 << endl; cout << "------"; cout << "\n"; cin >> input; cout << "\n"; if (input == multiplyAnswer) cout << "Congratulations\n"; else cout << "That is incorrect,\nThe correct answer is: " << multiplyAnswer; cout << "\n"; choice=6; } else if (choice==4) { cout << setw (6) << div1 << endl; cout << "/ "; cout << setw (4) << div2 << endl; cout << "------"; cout << "\n"; cin >> input; cout << "\n"; if (input == divAnswer) cout << "Congratulations\n"; else cout << "That is incorrect,\nThe correct answer is: " << divAnswer; cout << "\n"; choice=6; } else if (choice==5) {cout << "Thanks for playing\n";} else {cout << "You can only select options 1-5, run the program again and select a option 1-5\n";} } // end of while |
| ||
| Re: How can I add a loop for this code Quote:
Ok I tried to do this and add the choice=6 and deleted the return 0 and nothing. #include <iostream> |
| ||
| Re: How can I add a loop for this code Ok I got the loop to work now why am I not generating random numbers each time. It just displays the same numbers each time a selection is picked. It will display random numbers when I re run the program but now if I run it again with the loop. #include <iostream> |
| ||
| Re: How can I add a loop for this code Short answer: Move the do { from line 23/24 in front of the random stuff starting on line 15.Longer answer: If you were to break each problem type out into a function, you could call it from the if (choice ==stuff. The function could then generate the random values for the problem. This second method would have the added advantage of having everything about the problem all in one place. (The random parameters, the display, the input and the validation.) Also, your code wouldn't be bothering to generate random problems that will never be used. Sample add method: void add() |
| ||
| Re: How can I add a loop for this code Quote:
|
| ||
| Re: How can I add a loop for this code Murtan, I used your short answer method as we are just starting to learn about functions. Thanks and problem solved #include <iostream> |
| ||
| Re: How can I add a loop for this code Ok fixed it so it would display the menu again if you do not select a valid option
|
| All times are GMT -4. The time now is 5:41 pm. |
Forum system based on vBulletin Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
©2003 - 2009 DaniWeb® LLC