random math teacher

Im having trouble with connecting a random math problem to the correct answer its i don't know if its my logic or am i missing something to connect the random 2 numbers with the correct answer at the bottom.


#include<iostream>
#include<cstdlib>
#include<ctime>
using namespace std;
int main ()

{
int maxrange = 100 ,expression,right_number;
unsigned seed = time (0);

expression = 1 + rand() % maxrange;

cout <<"please add these two numbers"<<endl;
cout <<" "<<endl<<endl<<endl;
cout <<" "<< rand() % 100 <<endl;
cout <<" "<< rand() % 100 <<endl;
cout <<"+____"<<endl;

cout <<"what is the correct number:";
cin >>right_number;

if (right_number <= 0)
{
cout <<"the answer you gave is wrong"<<endl;
}

if (right_number == rand() % 100)
{
cout <<"the answer you gave is corrent whee.."<<endl;
}

if (rand() % 100 < right_number)
{
cout <<"the number you enter is wrong"<<endl;
}




return 0;

}
Please use [code]Your code here[/code]

Each time you call rand() % 100 it is going to be a different number. So where you are checking to see if it is right or not, is checking if you guessed correctly a random number from 0 to 99.

You'll should make variables to hold the random numbers that were generated. Lets say

1
2
int a = rand % 100 + 1
int b = rand % 100 + 1


Now a and b are ints between 1 and 100. Then for your if's do something like
1
2
3
4
if(right_number == (a + b))
     cout << "Correct" << endl;
else
     cout << "Wrong" << endl;
im geting an error with the % agian how do i fix this?


#include<iostream>
#include<cstdlib>
#include<ctime>
using namespace std;
int main ()

{ int maxrange = 100,expression,right_number,a = rand % 100 + 1,b = rand % 100 + 1;

unsigned seed = time (0);

//expression = 1 + rand() % maxrange;

cout <<"please add these two numbers"<<endl;
cout <<" "<<endl<<endl<<endl;
cout <<" "<< rand() % 100 <<endl;
cout <<" "<< rand() % 100 <<endl;
cout <<"+____"<<endl;

cout <<"what is the correct number:";
cin >>right_number;

if (right_number == (a + b))
{
cout <<" great you did it ! "<<endl;
}

else {
cout <<" sorry your wrong "<<endl;
}


return 0;

}
You forgot the parenthesis at your first rand (where you initialize a and b
a = rand() % 100 + 1,b = rand() % 100 + 1

Also you have an logic error.
At the beginning of the main you have
int a = rand() % 100 + 1,b = rand() % 100 + 1
You assign to a and b some random numbers between 0 and 100.
When you prompt the user with the numbers you have:
1
2
cout <<" "<< rand() % 100 <<endl;
cout <<" "<< rand() % 100 <<endl;

Which generates two new random numbers.
Then you are trying to find if he had the correct answer by checking if his answer is (a+b).
What you prompt the user to add is different than what you want him to add.
You should have:
1
2
cout <<" "<< a <<endl;
cout <<" "<< b <<endl;

That way you are giving him the numbers you generated at the beginning and then you check if he has the sum of those two.

Hope this helps
Last edited on
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
#include<iostream>
#include<cstdlib>
#include<ctime>
using namespace std; 

int RND(int max)
{
	return (int)(rand()*max);
}
int main ()
{
	int a, b, ans;
	int maxrange = 100;
	while (true)
	{
		a = RND(maxrange);
		b = RND(maxrange);
		cout << "Please add these two numberz: " << a << " + " << b << endl;
		cin >> ans;
		if (ans == a + b)
		{
			cout << "You are correct." << endl;
		}
		else
		{
			cout << "You are incorrect.  You get no cake." << endl;
		}
	}
	return 0;
}


Weeeee!
Why would you declare a new RND function that returns a call to rand when the rand() can do it by itshelf?

Also to have a number between 0 and maxrange you have to use a = rand() % maxrange
Last edited on
I am also guessing this isn't the whole code? You have a while (true) which is an infinate loop, and i see nothing to exit out of the loop. No return, no break, no anything.
It's called manual labor.... Breaking the process by yourself :)*

Yeah, I guess I had the rand() thing all wrong. Every random number generator I've seen returns a decimal. But not C++.... Odd.


*-requires certain compliers
Last edited on
Topic archived. No new replies allowed.