rand() help

Hi,

just need a little guidance in the right direction.

What I want to do is generate random statements to congratulate for correct answer. for example.

"Fantastic";
"Excellent";
"Superb";


instead of only the "Congratulations". I have indicated where I need these statements displayed. [See Below]

Any help is appreciated. Thanks

Here is my code:

#include <iostream>
#include <cmath>
#include <cstdlib>
#include <ctime>
#include <string>

using namespace std;

// function prototypes

// generateQuestion
// generateOutput


int main()
{
srand(time(0));
int num1 = 1+ rand() %10;
int num2 = 1+ rand() %10;
int product = num1 * num2;
int answer = 0;


cout << " *****Welcome to Multiplication Teacher***** " << " Enter -1 to exit " << endl << endl;



cout <<"How much does " << num1 <<" times " << num2 << " = ";
cin >> answer;

if (answer == -1)

return 0;

else if(answer == product)
{
cout <<"\nCongratulations!!!"<< endl << endl; <--- output random statements!!
return 0;

}

while(answer != product)
{
cout <<"Invaid Entry!!! Please try again: ";
cin >> answer;
if (answer == -1)

return 0;

else if(answer == product)
{
cout <<"\nExcellent!!!"<< endl << endl; <--- output random statements!!
return 0;

}
}
@Taino

I would make an array. string Congrats[3] = {"Fantastic","Excellent","Superb"}; Then cout << Congrats[rand() %3]; Of course, make the array larger and the rand to match, if you use more words than three.
or if you don't know how to do an array you could do rand with if statements
To use rand you need to initialize a random seed, correct?
1
2
3
4
5
#include <time.h>

// Initialize random seed
srand(time(NULL));
Not obligatory, but it's often useful. If you don't, you'll get the same result at each runtime (but different results for different rands within the same runtime).
except you should use ctime
Well, I see Taino is using ctime, and srand..
Topic archived. No new replies allowed.