I am trying to write a math tutoring program for students. A very basic outlay of ceretain components. This program should have the user select addition, subtraction, multiplication or division. user should also have the capeability to exit the program and quit it. I tried writing some code for this, but am having way to many issues. some criteria for this program should be

1 dispay what the user has inputted frm menu
2. generate 2 random #'s from 1-20
3. if user selects division, make sure divisor is not zero
4. display the #'s and accept the users answers from the keyboard on weather they perform addition subtraction mult, or division
6. if user gets it correct display congratulations!
8. after program operates display menu again, until users decides to end program by quitting.

Any form of help or snippets of code, or maybe what direction i should be heading in would be greatly apprecitated. Thanks in advance.

>>I tried writing some code for this, but am having way to many issues

please post code, and use code tags as described in the links in my signature below.

I see in your code that you are not using `srand( )` to seed or to initialize your random function and hence you are getting repetitive values.

Try putting `srand( time(NULL) )` at the start of your code and then see. For more description see

Okay the thing is that while generating random numbers care has to be taken that the numbers so generated dont repeat in a pattern or frequently (though this would happen if the range is small).

To safeguard from this, normally all random generators make use of a base values …

## All 10 Replies

>>I tried writing some code for this, but am having way to many issues

please post code, and use code tags as described in the links in my signature below.

>>I tried writing some code for this, but am having way to many issues

please post code, and use code tags as described in the links in my signature below.

Here is the code: My problem is the random numbers...they always come up the same as 4 and 19. Can someone help me with the random numbers. They have to be between 1-20. Thanks

``````#include<iostream>
usingnamespace std;
int Subtract(int x, int y);
int Times (int x, int y);
int Divide (int x, int y);
int Remainder (int x, int y);
int main()
{
int a,b,c,d,x=0;
while (x!=77){
cin>>x;
a=rand()%19 + 1;
b=rand()%19 + 1;
switch(x)
{
case 1:
cout<<a<<"+"<<b<<"=??";
break;
case 2:
cout<<a<<"-"<<b<<"=??";
d=Subtract(a,b);
break;
case 3:
cout<<a<<"*"<<b<<"=??";
d=Times(a,b);
break;
case 4:
cout<<a<<"/"<<b<<"=??";
d=Divide(a,b);
break;
case 5:
return 0;
break;
}//end switch(x)
cin>>c;
if (c==d)
cout<<"Congratulations...You are Correct";
else
if (x==4)
cout<<"\nwith a remainder of "<<Remainder(a,b);
cout<<endl<<endl;
}//end while(x)
}
{
int z;
z=x+y;
return z;
}
int Subtract (int x, int y)
{
int z;
z=x-y;
return z;
}
int Times (int x, int y)
{
int z;
z=x*y;
return z;
}
int Divide (int x, int y)
{
int z;
z=x/y;
return z;
}
int Remainder (int x, int y)
{
int z;
z=x%y;
return z;
}``````

I see in your code that you are not using `srand( )` to seed or to initialize your random function and hence you are getting repetitive values.

Try putting `srand( time(NULL) )` at the start of your code and then see. For more description see here.

HOpe it helped, bye.

I see in your code that you are not using `srand( )` to seed or to initialize your random function and hence you are getting repetitive values.

Try putting `srand( time(NULL) )` at the start of your code and then see. For more description see here.

HOpe it helped, bye.

I checked out the help from your page on where to put this snippet of code, But I never learned that function yet and still am having difficulty with trying to understand this srad. Can you maybe walk me through it a little better. What do I substitute for time? its a number generator program.

Okay the thing is that while generating random numbers care has to be taken that the numbers so generated dont repeat in a pattern or frequently (though this would happen if the range is small).

To safeguard from this, normally all random generators make use of a base values (normally a very large number) which serves as a base or as a reference so that the numbers so generated using them dont repeat.

This said, care should be taken that the reference number should be as unique as possible and should not be ideally repeated.

The time( ) function returns the number of seconds that have passed since 00:00:00 GMT January 1, 1970. So when this value is passsed to the time function, it serves as a satisfactory reandom number generator reference or seed value as they call it.

srand() takes a seed as an argument and in this case...the system's time is the seed which naturally keeps varying ....so you get a true random number simulation....there are so many other algos to generate random numbers....you can refer a good algorithm text....place srand(time(NULL)) anywhere before the rand() function

Here;s something you can work on. It may not be exactly what you need but its a basis at least. Also, why do you need to check if the second number of the division is zero, from the second you say it will accept values from 1-20?? Anyway, I put the check in the program as well if u need it for later on.. Hope it helps, byee

``````#include <iostream>
#include <ctime>

using namespace std;

void subtraction(int random1, int random2);
void multiplication(int random1, int random2);
void division(int random1, int random2);

int main()
{
int random1, random2;
srand(time(0));

int choice;

do
{
random1 = 1+(rand()%20); // from 1-20
random2 = 1+(rand()%20); // from 1-20

switch (choice)
{
case 0: cout << endl;                     break;
case 2: subtraction(random1,random2);     break;
case 3: multiplication(random1,random2); break;
case 4:
{
if (random2 == 0)
cout << "Error: Division by zero, cannot proceed\n";
else
division(random1,random2);
} break;
default : cout << "Error in input\n\n"; break;
}

} while (choice != 0);

return 0;
}

{
cout << "(2)Subtraction\n";
cout << "(3)Multiplication\n";
cout << "(4)Division\n";
cout << "(0)Quit\n";
cout << "-----------------\n";
cout << "Choice: ";
}

{

cout << "\n" << random1 << " + " << random2 << " = ";

cout << "Congratulations\n\n";
else
cout << "Incorrect\n\n";
}

void subtraction(int random1, int random2)
{

cout << "\n" << random1 << " - " << random2 << " = ";

cout << "Congratulations\n\n";
else
cout << "Incorrect\n\n";
}

void multiplication(int random1, int random2)
{

cout << "\n" << random1 << " * " << random2 << " = ";

cout << "Congratulations\n\n";
else
cout << "Incorrect\n\n";
}

void division(int random1, int random2)
{

cout << "\n" << random1 << " / " << random2 << " = ";

cout << "Congratulations\n\n";
else
cout << "Incorrect\n\n";
}``````

hey, so i also had a question about this as well but it's an extension to his basic math tutor problem. i used the code layout that may4life gave out which was fine and dandy but how do i get the program to where the user can practice math problems continuously until he/she decides to quit the program? i am teaching myself how to code but it's a slow process and the book and instructions aren't very clear to me and it's asking for this and have it in a do-while loop.it says the program should output the number of problems attempted, number of correct answers, and the score as a percentage as well. can anyone help me out? it's an online course as well so i'm sortof stuck on time a bit so any help here by sometime tonight would be very much appreciated. do i just make the do-while loop after typing in the if/else statements or after each if/else statement? sorry if this is the wrong place to be asking this and if it is then please let me know and let me know how to take care of my question so i can either move it or whatever i need to do. i'm just tryin to understand all this but i'm so much of a code noob that it's hard for me i guess. idk =/

Holy Necro Bump Batman, make a new thread and I'll see what I can do for you.

hey, so i also had a question about this as well but it's an extension to his basic math tutor problem. i used the code layout that may4life gave out which was fine and dandy but how do i get the program to where the user can practice math problems continuously until he/she decides to quit the program? i am teaching myself how to code but it's a slow process and the book and instructions aren't very clear to me and it's asking for this and have it in a do-while loop.it says the program should output the number of problems attempted, number of correct answers, and the score as a percentage as well. can anyone help me out? it's an online course as well so i'm sortof stuck on time a bit so any help here by sometime tonight would be very much appreciated. do i just make the do-while loop after typing in the if/else statements or after each if/else statement? sorry if this is the wrong place to be asking this and if it is then please let me know and let me know how to take care of my question so i can either move it or whatever i need to do. i'm just tryin to understand all this but i'm so much of a code noob that it's hard for me i guess. idk =/

Add extra while loop , ask user whether he wants to quit then break from while loop.

Be a part of the DaniWeb community

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