View Full Version : Using rand()
Dee_dee
10-31-2005, 03:23 AM
I have a lab to do in school and it's kind of confusing.
We have to write a program using the rand() function to produce two positive one-digit integers. The program should then output a question using the numbers, such as: How much is 6 times 7? First number must be less than or equal to the second. The student then types the answer. The program checks the student's answer. If it's correct print out "very good" Otherwise print out "Sorry, you are wrong" then let the student try the same question again. He always wants use to count the numer of right and wrong answers. After the student has entered -1 print "You did X correct and Y wrong." This is what I have so far.
#include <iostream>
using std::cout;
using std::endl;
using std::cin;
int main()
{
do
{
int a;
int b;
int answer = 0;
}
while ();
cout <<"What is "<<a<<" times "<<b<<"?";
cin >> answer;
if (answer == product)
product=a*b;
answer = true;
else
answer = false;
if ()
cout<<"Please try again.";
else
cout<<"Good job!";
}
return 0;
}
I'm really lost on what to do any help would be greatly appreciated.
oracleguy
10-31-2005, 07:14 PM
Well, you should start by writing a function that generates a random number inside the bounds you want so in this instance 0 to 9, and then returns that. Then you could use a do while loop to make sure the second one is larger than the first.
#include <ctime>
#include <cstdlib>
Those are the files you need to include (i think) to allow you to use the rand() function.
west_dweller79
11-06-2005, 10:08 AM
Hi Dee dee,
if u need to create 2 random integer, you can used the following code to create random number
srand(time(NULL));
a = rand()%10;
This is to generate a random number based on the time on the computer. The 2nd line is to generate a number between 0 - 9.
After creating 2 random number, just need to do a comparison and make sure the 2nd number is bigger than the 1st.
Regards
west_dweller79
Dee_dee
11-07-2005, 07:28 PM
thanks for the help. But it still doesn't tell me how many wrong answers I have and how many right answers I have. or it doesn't let me keep trying the question if i got it wrong. Do I use aother if statement for that????
#include<iostream>
using std::cout;
using std::cin;
using std::endl;
#include<cstdlib>
#include<ctime>
inline int multi (int number1, int number2) {return number1*number2;}
int main ()
{
cout<<"Name: \n";
cout<<"Student ID: \n";
int number1;
int number2;
int answer;
srand(time(0));
do
{
number1=rand()%10;
number2=rand()%10;
if
(number1 > number2)
cout<<number2<<" times "<<number1<<"?\n";
else
cout<<number1<<" times"<<number2<<"?\n";
cin>>answer;
if (answer!=-1)
{
if(answer==multi(number1,number2))
cout<<"very Good!\n";
else
cout<<"Sorry wrong answer\n";
cout<<endl;
}
if
(answer is wrong)
cout<<"No. Please try again.";
cout<<"you did<<number3<<"Correct"<<number4<<wrong;
cout<<"Goodbye!\n";
}while (answer!=-1);
return 0;
oracleguy
11-07-2005, 07:34 PM
Please in the future specify the errors you are getting and on what lines. It makes it easier for people to help you with your code. But since I'm a nice guy and I took a look at it and ran it through the compiler anyways.
Your problem is with your inline function statement as I first suspected. You never were returning a value and you had a comma between the decleration and the body of the function.
The correct syntax is:
inline int multi (int number1, int number2) {return number1*number2;}
west_dweller79
11-08-2005, 04:16 PM
Dee dee,
After reading your code, i guess you need to declare 2 more variable to count the number to correct answer and number of wrong answer.
eg, int correct=0, wrong=0;
if (answer is right)
{
cout<< blah blah blah;
correct++;
}
else
cout<< blah blah blah;
if(answer is not right)
{
cout<< blah blah blah;
wrong++;
}
cout<< "you got " << correct << "and " << wrong << "wrong.";
If you want to keep trying the same question over and over again till you got the correct answer, you can use while loop.
Not going to show you the working program of yours here, but at least some help. Hope you understand what i'm writing here.
PS: Human are born to learn when they grow and not spoon feeding :)
Dee_dee
11-11-2005, 01:22 PM
Thanks for the help. I get it now
vBulletin® v3.8.2, Copyright ©2000-2012, Jelsoft Enterprises Ltd.