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);
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
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:
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.
#include<iostream>
#include<cstdlib>
#include<ctime>
usingnamespace 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;
}
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.