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

#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;
	int subAnswer = sub1- sub2;
	int multiplyAnswer = multiply1 * multiply2;
	int 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";
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";
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";
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";
return 0;}
			
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";}
     
     
return 0;

}

Dani AI

Generated

Quick diagnosis and a compact plan based on the replies from , , , and :

The original program failed to repeat correctly for three reasons: a) several branches ended with an early program exit (those return statements stop the loop — called this out), b) the random operands and their answers were computed once before the loop so every iteration showed the same numbers, and c) RNG seeding or placement can give repeated sequences if done incorrectly. The fixes below combine the advice already posted and add a few practical safeguards.

Practical changes to make

  • Seed the RNG exactly once at program start (use a time-based seed). Do not re-seed inside the loop.
  • Use a single repeating loop that shows the menu, reads/validates the choice, and breaks only on the quit choice (this is the idea behind ' suggestion). Remove any return statements that prematurely exit from inside the menu handling.
  • Generate operands and compute the correct answer just before asking that specific question (either inside the loop or inside a small function per operation, as recommended). That guarantees fresh numbers each iteration.
  • Validate numeric input (clear the stream on failure) so invalid keystrokes don’t lock the program.

Minimal control-structure sketch

/* seed RNG once (use current time) */

for (;;) {
  // create fresh operands for the next question
  // display menu and read/validate choice
  if (choice == QUIT) break;
  switch (choice) {
    case ADD:   /* generate add operands, ask, check */ break;
    case SUB:   /* same for subtraction */ break;
    ...
  }
}

Extra tips

  • For division problems that must produce integer answers, choose a divisor and an integer quotient, then set dividend = divisor * quotient so the result is exact.
  • When validating input, on stream failure call cin.clear() and cin.ignore(...) before continuing the loop.
  • Factor each operation into a small function (generation + display + check) to keep logic tidy and avoid duplicated code (this also makes testing easier).

Recommended Answers

All 16 Replies

Simply..

//...
char rep='n';
//display the menu and get user's choice
while(tolower(rep)=='n')
{
    //...Menu
    cout<<"repeat? [y/n] : ";
    cin>>rep;
}
return 0; //bye..
while (choice!=5) 
{
    Your Programme Body
}

Remember to Intialize Choice With any Number without 5

Simply..

//...
char rep='n';
//display the menu and get user's choice
while(tolower(rep)=='n')
{
    //...Menu
    cout<<"repeat? [y/n] : ";
    cin>>rep;
}
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.

while (choice!=5)
{
Your Programme Body

}

Remember to Intialize Choice With any Number without 5

while (choice!=5)
{
Your Programme Body

}

Remember to Intialize Choice With any Number without 5

What?

#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=0, 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;
  
      int subAnswer = sub1- sub2;
  
      int multiplyAnswer = multiply1 * multiply2;
  
      int divAnswer = div1 / div2;
  
       
  
       
  
          
  
     //Display the menu and get the user's choice
  
      while(choice!=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";
  
      if
  
      (input == addAnswer)
  
      cout << "Congratulations\n";
  
      else
  
      cout << "That is incorrect,\nThe correct answer is: " << addAnswer;
  
      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";
  
      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";
 
      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";
 
      return 0;}
 
       
 
   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";}
 
       }
 
       
 
      return 0;
 
       
 
      }

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

#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=0, 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;
  
      int subAnswer = sub1- sub2;
  
      int multiplyAnswer = multiply1 * multiply2;
  
      int divAnswer = div1 / div2;
  
       
  
       
  
          
  
     //Display the menu and get the user's choice
  
      while(choice!=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";
  
      if
  
      (input == addAnswer)
  
      cout << "Congratulations\n";
  
      else
  
      cout << "That is incorrect,\nThe correct answer is: " << addAnswer;
  
      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";
  
      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";
 
      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";
 
      return 0;}
 
       
 
   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";}
 
       }
 
       
 
      return 0;
 
       
 
      }

I tried to see what you did and tried it but it did not work.

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.

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

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

end quote.

Ok I tried to do this and add the choice=6 and deleted the return 0 and nothing.

#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";


while (choice!=5)   

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";
choice=6; 
}

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";
choice=6; }

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";
choice=6; }

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";
choice=6; }

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;


}

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>
#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;


}

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()
{
   int add1 = (rand()%500)+1;
   int add2 = (rand()%500)+1;
   int addAnswer = add1 + add2;

   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";
}

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()
{
   int add1 = (rand()%500)+1;
   int add2 = (rand()%500)+1;
   int addAnswer = add1 + add2;

   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";
}

So would I need to create a function for each problem?

Murtan,

I used your short answer method as we are just starting to learn about functions.

Thanks and problem solved

#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;


}

Ok fixed it so it would display the menu again if you do not select a valid option

#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;


}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.