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.
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";
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.
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.