-
December 1st, 2008, 03:09 PM
#1
[RESOLVED] How can I add a loop for this code
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
Code:
#include <iostream>
#include <cstdlib>
#include <iomanip>
#include <ctime>
using namespace std;
int main ()
{
srand((unsigned)time(0));
int add1, add2, sub1, sub2;
int multiply1, multiply2, div1, div2;
int choice, input;
add1 = (rand()%500)+1, add2 = (rand()%500)+1;
sub1 = (rand()%500)+1, sub2 = (rand()%98)+1;
multiply1 = (rand()%98)+1, multiply2 = (rand()%8)+1;
div1 = (rand()%500)+1, div2 = (rand()%8)+1;
int addAnswer = add1 + add2, subAnswer = sub1- sub2;
int multiplyAnswer = multiply1 * multiply2, divAnswer = div1 / div2;
//Display the menu and get the user's choice
cout << " Geometry Calculator \n\n";
cout << "1. Addition\n";
cout << "2. Subtraction\n";
cout << "3. Multiplication\n";
cout << "4. Division\n";
cout << "5. Quit\n";
cout << "\n";
cout <<"Enter your choice (1-5): ";
cin >> choice;
cout << "\n";
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";
cout << "\n";
return 0;
}
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";
cout << "\n";
return 0;}
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";
cout << "\n";
return 0;}
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";
cout << "\n";
return 0;}
else if (choice==5)
{
cout << "Thanks for playing\n";
cout << "\n";
cout << "\n";
}
else
{cout << "You can only select options 1-5, run the program again and select a option 1-5\n";}
return 0;
}
-
December 1st, 2008, 05:49 PM
#2
Re: How can I add a loop for this code
Can anyone help me. I am new and having trouble with loops.
-
December 1st, 2008, 06:00 PM
#3
Re: How can I add a loop for this code
If your indenting made more sense, it might be easier to get the loop right. What trouble are you having specifically?
-
December 1st, 2008, 06:43 PM
#4
Re: How can I add a loop for this code
Originally Posted by Lindley
If your indenting made more sense, it might be easier to get the loop right. What trouble are you having specifically?
Basically I want the user to be able to use the program as much as they want until they enter the number 5. So they could basically do any of the functions adding, subtractin, multiplication, and division as many times until they enter 5.
All the words such as if, else if, else are all not indented and everything else is indented with one tab. Seems to make sense to me when I did it.
-
December 1st, 2008, 08:38 PM
#5
Re: How can I add a loop for this code
I think you want to enclose the entire switch statement in a 'while' loop, like this:
Code:
while (switch!=5) // while switch is not '5'...
{
cout << " Geometry Calculator \n\n";
cout << "1. Addition\n";
cout << "2. Subtraction\n";
cout << "3. Multiplication\n";
cout << "4. Division\n";
cout << "5. Quit\n";
cout << "\n";
cout <<"Enter your choice (1-5): ";
cin >> choice;
cout << "\n";
if (choice==1)
{
cout << setw (6) << add1 << endl;
cout << "+ ";
cout << setw (4) << add2 << endl;
cout << "------";
cout << "\n";
cin >> input;
cout << "\n";
// and so on...
} // then obviously end the while loop
Hope that's of help!
-
December 1st, 2008, 08:59 PM
#6
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.
Code:
#include <iostream>
#include <cstdlib>
#include <iomanip>
#include <ctime>
using namespace std;
int main ()
{
srand((unsigned)time(0));
int add1, add2, sub1, sub2;
int multiply1, multiply2, div1, div2;
int choice, input;
add1 = (rand()%500)+1, add2 = (rand()%500)+1;
sub1 = (rand()%500)+1, sub2 = (rand()%98)+1;
multiply1 = (rand()%98)+1, multiply2 = (rand()%8)+1;
div1 = (rand()%500)+1, div2 = (rand()%8)+1;
int addAnswer = add1 + add2, subAnswer = sub1- sub2;
int multiplyAnswer = multiply1 * multiply2, divAnswer = div1 / div2;
do
{ //Display the menu and get the user's choice
cout << " Math Tutor \n\n";
cout << "1. Addition\n";
cout << "2. Subtraction\n";
cout << "3. Multiplication\n";
cout << "4. Division\n";
cout << "5. Quit\n";
cout << "\n";
cout <<"Enter your choice (1-5): ";
cin >> choice;
cout << "\n";
//Validate the menu selection
while (choice < 1 || choice > 5)
{
cout << "That is not a valid option\n";
cout <<"Enter your choice (1-5): ";
cin >> choice;
cout << "\n";
}
if (choice==1)
{
cout << setw (6) << add1 << "\n";
cout << "+ ";
cout << setw (4) << add2 << "\n";
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";
cout << "\n";
}
else if (choice==2)
{
cout << setw (6) << sub1 << "\n";
cout << "- ";
cout << setw (4) << sub2 << "\n";
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";
cout << "\n";
}
else if (choice==3)
{
cout << multiply1;
cout << " * ";
cout << multiply2 << "=";
cin >> input;
cout << "\n";
if
(input == multiplyAnswer)
cout << "Congratulations\n";
else
cout << "That is incorrect,\nThe correct answer is: " << multiplyAnswer;
cout << "\n";
cout << "\n";
}
else if (choice==4)
{
cout << div1;
cout << "/";
cout << div2 << "=";
cin >> input;
cout << "\n";
if
(input == divAnswer)
cout << "Congratulations\n";
else
cout << "That is incorrect,\nThe correct answer is: " << divAnswer;
cout << "\n";
cout << "\n";
}
else if (choice==5)
{
cout << "Thanks for playing\n";
cout << "\n";
cout << "\n";
}
} while (choice !=5);
return 0;
}
-
December 1st, 2008, 09:33 PM
#7
Re: How can I add a loop for this code
Originally Posted by davids2004
All the words such as if, else if, else are all not indented and everything else is indented with one tab. Seems to make sense to me when I did it.
Tell your IDE to auto-indent, there should be an option for that somewhere:
-Everything inside a function (including main()) should be indented one level from the function header.
-Everything inside a loop should be indented one level from the loop header.
-Everything inside an if should be indented one level from the if statement.
etc. You shouldn't be thinking in terms of every line being indented only zero or one stops, because that hides a great deal of the program's structure.
Case in point: You're wondering why you aren't generating new random numbers each iteration of the loop, when it should be obvious based on indenting alone that the calls to rand() are occuring before the loop.
-
December 1st, 2008, 09:57 PM
#8
Re: How can I add a loop for this code
Originally Posted by Lindley
Tell your IDE to auto-indent, there should be an option for that somewhere:
-Everything inside a function (including main()) should be indented one level from the function header.
-Everything inside a loop should be indented one level from the loop header.
-Everything inside an if should be indented one level from the if statement.
etc. You shouldn't be thinking in terms of every line being indented only zero or one stops, because that hides a great deal of the program's structure.
Case in point: You're wondering why you aren't generating new random numbers each iteration of the loop, when it should be obvious based on indenting alone that the calls to rand() are occuring before the loop.
It seems as though you are an expert on this. I am a beginner on this. Would you please help me to understand what you are talking about.
I am using Visual studios 2008 express and I do not see an auto indent. Why does indenting make such a big deal?
-
December 1st, 2008, 10:01 PM
#9
Re: How can I add a loop for this code
Is this better?
Code:
#include <iostream>
#include <cstdlib>
#include <iomanip>
#include <ctime>
using namespace std;
int main ()
{
srand((unsigned)time(0));
int add1, add2, sub1, sub2;
int multiply1, multiply2, div1, div2;
int choice, input;
add1 = (rand()%500)+1, add2 = (rand()%500)+1;
sub1 = (rand()%500)+1, sub2 = (rand()%98)+1;
multiply1 = (rand()%98)+1, multiply2 = (rand()%8)+1;
div1 = (rand()%500)+1, div2 = (rand()%8)+1;
int addAnswer = add1 + add2, subAnswer = sub1- sub2;
int multiplyAnswer = multiply1 * multiply2, divAnswer = div1 / div2;
do
{ //Display the menu and get the user's choice
cout << " Math Tutor \n\n";
cout << "1. Addition\n";
cout << "2. Subtraction\n";
cout << "3. Multiplication\n";
cout << "4. Division\n";
cout << "5. Quit\n";
cout << "\n";
cout <<"Enter your choice (1-5): ";
cin >> choice;
cout << "\n";
//Validate the menu selection
while (choice < 1 || choice > 5)
{
cout << "That is not a valid option\n";
cout <<"Enter your choice (1-5): ";
cin >> choice;
cout << "\n";
}
if (choice==1)
{
cout << setw (6) << add1 << "\n";
cout << "+ ";
cout << setw (4) << add2 << "\n";
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";
cout << "\n";
}
else if (choice==2)
{
cout << setw (6) << sub1 << "\n";
cout << "- ";
cout << setw (4) << sub2 << "\n";
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";
cout << "\n";
}
else if (choice==3)
{
cout << multiply1;
cout << " * ";
cout << multiply2 << "=";
cin >> input;
cout << "\n";
if
(input == multiplyAnswer)
cout << "Congratulations\n";
else
cout << "That is incorrect,\nThe correct answer is: " << multiplyAnswer;
cout << "\n";
cout << "\n";
}
else if (choice==4)
{
cout << div1;
cout << "/";
cout << div2 << "=";
cin >> input;
cout << "\n";
if
(input == divAnswer)
cout << "Congratulations\n";
else
cout << "That is incorrect,\nThe correct answer is: " << divAnswer;
cout << "\n";
cout << "\n";
}
else if (choice==5)
{
cout << "Thanks for playing\n";
cout << "\n";
cout << "\n";
}
} while (choice !=5);
return 0;
}
-
December 2nd, 2008, 07:46 AM
#10
Re: How can I add a loop for this code
Yeah, that's much better. Hopefully you can also see now that the values for add1, add2, sub1 and sub2 are assigned outside the loop. You need to assign new values each time the loop runs, so go
Code:
add1 = ((rand()%500)+1)
//...and so on
at the top of the while loop.
-
December 2nd, 2008, 10:34 AM
#11
Re: How can I add a loop for this code
Originally Posted by dan56965
Yeah, that's much better. Hopefully you can also see now that the values for add1, add2, sub1 and sub2 are assigned outside the loop. You need to assign new values each time the loop runs, so go
Code:
add1 = ((rand()%500)+1)
//...and so on
at the top of the while loop.
I see the code that you are talking about but what do you mean by at the top of the while loop?
-
December 2nd, 2008, 10:45 AM
#12
Re: How can I add a loop for this code
Ok got it all working now
Code:
#include <iostream>
#include <cstdlib>
#include <iomanip>
#include <ctime>
using namespace std;
int main ()
{
srand((unsigned)time(0));
int add1, add2, sub1, sub2;
int multiply1, multiply2, div1, div2;
int choice, input;
do {add1 = (rand()%500)+1, add2 = (rand()%500)+1;
sub1 = (rand()%500)+1, sub2 = (rand()%98)+1;
multiply1 = (rand()%98)+1, multiply2 = (rand()%8)+1;
div1 = (rand()%500)+1, div2 = (rand()%8)+1;
int addAnswer = add1 + add2, subAnswer = sub1- sub2;
int multiplyAnswer = multiply1 * multiply2, divAnswer = div1 / div2;
//Display the menu and get the user's choice
cout << " Math Tutor \n\n";
cout << "1. Addition\n";
cout << "2. Subtraction\n";
cout << "3. Multiplication\n";
cout << "4. Division\n";
cout << "5. Quit\n";
cout << "\n";
cout <<"Enter your choice (1-5): ";
cin >> choice;
cout << "\n";
//Validate the menu selection
while (choice < 1 || choice > 5)
{
cout << "That is not a valid option\n";
cout <<"Enter your choice (1-5): ";
cin >> choice;
cout << "\n";
}
if (choice==1)
{
cout << setw (6) << add1 << "\n";
cout << "+ ";
cout << setw (4) << add2 << "\n";
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";
cout << "\n";
}
else if (choice==2)
{
cout << setw (6) << sub1 << "\n";
cout << "- ";
cout << setw (4) << sub2 << "\n";
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";
cout << "\n";
}
else if (choice==3)
{
cout << multiply1;
cout << " * ";
cout << multiply2 << "=";
cin >> input;
cout << "\n";
if
(input == multiplyAnswer)
cout << "Congratulations\n";
else
cout << "That is incorrect,\nThe correct answer is: " << multiplyAnswer;
cout << "\n";
cout << "\n";
}
else if (choice==4)
{
cout << div1;
cout << "/";
cout << div2 << "=";
cin >> input;
cout << "\n";
if
(input == divAnswer)
cout << "Congratulations\n";
else
cout << "That is incorrect,\nThe correct answer is: " << divAnswer;
cout << "\n";
cout << "\n";
}
else if (choice==5)
{
cout << "Thanks for playing\n";
cout << "\n";
cout << "\n";
}
} while (choice !=5);
return 0;
}
-
December 2nd, 2008, 10:52 AM
#13
Re: How can I add a loop for this code
OK tweaked it a little bit. Now it displays the menu each time you do not select a valid option.
Code:
#include <iostream>
#include <cstdlib>
#include <iomanip>
#include <ctime>
using namespace std;
int main ()
{
srand((unsigned)time(0));
int add1, add2, sub1, sub2;
int multiply1, multiply2, div1, div2;
int choice, input;
do {add1 = (rand()%500)+1, add2 = (rand()%500)+1;
sub1 = (rand()%500)+1, sub2 = (rand()%98)+1;
multiply1 = (rand()%98)+1, multiply2 = (rand()%8)+1;
div1 = (rand()%500)+1, div2 = (rand()%8)+1;
int addAnswer = add1 + add2, subAnswer = sub1- sub2;
int multiplyAnswer = multiply1 * multiply2, divAnswer = div1 / div2;
//Display the menu and get the user's choice
cout << " Math Tutor \n\n";
cout << "1. Addition\n";
cout << "2. Subtraction\n";
cout << "3. Multiplication\n";
cout << "4. Division\n";
cout << "5. Quit\n";
cout << "\n";
cout <<"Enter your choice (1-5): ";
cin >> choice;
cout << "\n";
//Validate the menu selection
while (choice < 1 || choice > 5)
{
cout << "That is not a valid option\n";
cout <<"Please select again\n\n ";
cout << " Math Tutor \n\n";
cout << "1. Addition\n";
cout << "2. Subtraction\n";
cout << "3. Multiplication\n";
cout << "4. Division\n";
cout << "5. Quit\n";
cout << "\n";
cout <<"Enter your choice (1-5): ";
cin >> choice;
cout << "\n";
}
if (choice==1)
{
cout << setw (6) << add1 << "\n";
cout << "+ ";
cout << setw (4) << add2 << "\n";
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";
cout << "\n";
}
else if (choice==2)
{
cout << setw (6) << sub1 << "\n";
cout << "- ";
cout << setw (4) << sub2 << "\n";
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";
cout << "\n";
}
else if (choice==3)
{
cout << multiply1;
cout << " * ";
cout << multiply2 << "=";
cin >> input;
cout << "\n";
if
(input == multiplyAnswer)
cout << "Congratulations\n";
else
cout << "That is incorrect,\nThe correct answer is: " << multiplyAnswer;
cout << "\n";
cout << "\n";
}
else if (choice==4)
{
cout << div1;
cout << "/";
cout << div2 << "=";
cin >> input;
cout << "\n";
if
(input == divAnswer)
cout << "Congratulations\n";
else
cout << "That is incorrect,\nThe correct answer is: " << divAnswer;
cout << "\n";
cout << "\n";
}
else if (choice==5)
{
cout << "Thanks for playing\n";
cout << "\n";
cout << "\n";
}
} while (choice !=5);
return 0;
}
-
December 2nd, 2008, 03:19 PM
#14
Re: How can I add a loop for this code
Originally Posted by davids2004
Ok got it all working now
Glad to hear it!
On the subject of formatting though, I suggest you clean up that block of declarations at the top - it took me a few mins just to find the start of the 'do while' loop. The idea with the indentation is to show the hierarchy of the design, so for example, the 'do while' loop should be higher up that hierarchy than all your various "if (choice==...)" conditions.
e.g.
Code:
int main ()
{
srand((unsigned)time(0));
int add1, add2, sub1, sub2;
int multiply1, multiply2, div1, div2;
int choice, input;
do
{
add1 = (rand()%500)+1;
add2 = (rand()%500)+1;
sub1 = (rand()%500)+1;
sub2 = (rand()%98)+1;
multiply1 = (rand()%98)+1;
multiply2 = (rand()%8)+1;
div1 = (rand()%500)+1;
div2 = (rand()%8)+1;
int addAnswer = add1 + add2;
subAnswer = sub1- sub2;
int multiplyAnswer = multiply1 * multiply2;
divAnswer = div1 / div2;
//...
while (choice < 1 || choice > 5)
{
cout << "That is not a valid option\n";
cout <<"Enter your choice (1-5): ";
cin >> choice;
cout << "\n";
}
if (choice == 1)
{
cout << setw (6) << add1 << "\n";
cout << "+ ";
cout << setw (4) << add2 << "\n";
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";
cout << "\n";
}
//...
} while (choice !=5);
}
On a side note: I gather it's kind of bad practice to format your loops/conditions like this, as it's easy to add a line, and forget that it is no longer included in the loop:
Code:
if (choice==1)
cout << "choice is one.";
cout << "this line is not in the loop...";
You should either do like I did above, and include braces, or put it all on one line:
Code:
if (choice==1) cout << "this is the only statement for this condition";
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|