random math teacher
| idono (25) | |||
| 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; } | |||
| mikeb570 (184) | |||||
| 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
Now a and b are ints between 1 and 100. Then for your if's do something like
| |||||
| idono (25) | |||
| 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; } | |||
| Mitsakos (343) | |||||
You forgot the parenthesis at your first rand (where you initialize a and b
a = rand() % 100 + 1,b = rand() % 100 + 1Also you have an logic error. At the beginning of the main you have
int a = rand() % 100 + 1,b = rand() % 100 + 1You 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:
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 | |||||
| Mayflower (25) | |||
Weeeee! | |||
| Mitsakos (343) | |||
| 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 | |||
| Aakanaar (151) | |||
| 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. | |||
| Mayflower (25) | |||
| 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 | |||
This topic is archived - New replies not allowed.
