Stuck with my Project!

Hey guys,

I have this project that I have to complete by next week for my Computer Science class. Now I have to write a program that will help students learn basic math operations.

I'm pretty far into my project but let me share a criteria with you guys that I need help with:

The student types the answer for each problem. Your program checks the student’s answer. If it is correct, print one of the following randomly:
Very good!
Excellent!
Nice work!
Keep up the good work!

I am really confused on how I can make the computer print one of these following messages randomly.

int integer_one, integer_two, answer;

for (int i = 1; 1 <= 10; i++)
{
single_digit(integer_one, integer_two);
cout << integer_one << " + " << integer_two << " = ";
cin >> answer;

if (integer_one + integer_two == answer)
{
cout << "Good Job" << endl;
}
else
{
cout << "Sorry, try again." << endl;
}

I can make it print one message but 5 and random? Need Help!!


I'd use rand(), it will give you a random number each time you use it if you use a good seed in srand() (I suggest using the time(NULL) in ctime). Then it's just a matter of modulusing the amount of possible answers and using if statements.

The use in this case would look like this:
#include <cmath>
#include <ctime>
#include <iostream>

using namespace std;

...

int random = 0;
srand(time(NULL)
random = rand() % 5;
if (random == 0)
cout << "Good Job";
else if (random ==1)
cout << "Other message";


and so on.
I haven't really grasped the concept of srand() yet. The teacher actually didn't even teach it, for one lab she just said to use that but I understood what rand() did. But what does NULL do? I have this even though I have NO idea what it means:

srand(time(0)), what does time mean? why is there a 0 there? The teacher never explained those, we just use them.

Last edited on
You could also replace the if statements by storing the output strings in an array and using the random number as the array index.
It's virtually impossible to generate completely random numbers, rand() does it by taking a seed number and working off of that. srand() is the operation that gives rand() it's seed number. With the same seed number rand() will give off the same sequence of numbers so, we seed time(0) (or time(NULL)) which returns the current time and as the time is always unique we get random numbers.

The 0 is there because you don't want to store the time value into a time_t object.
Awesome, thanks for helping me understand.

I haven't learned arrays yet, don't even know what they are yet.
Some people were saying it was hard though :o
Topic archived. No new replies allowed.