Hi,

I am running an application and wish to add random outputs when the user answers correctly.

example 1 + 1 = 2

cout << " great";

I wish to randomly cout << excellent or very good or fantastic as well. Thanks!
This is my code:

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


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;;
return 0;


}


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


return 0;


else if(answer == product)
{
cout <<"\nCongratulations"<< endl << endl;  <----- this is where i wish to add it as a random output and not just congratulations.
return 0;


}
}



cout << endl << endl;


return 0;


}

Edited 3 Years Ago by Nick Evan: Fixed formatting

make a string array of your responses then randomly pick one element. ie string ans[5]="blah" etc... ans[rand()%5].

Edited 5 Years Ago by frogboy77: n/a

string compliments[4] = {"String 1", "String 2", "String 3", "String 4"}; This will create an array of strings that you can output. If you're going to use the string class, you're going to need to include the proper header, which I believe is either called <string> or <strings>

You could also use a do {...} while() loop to have the user enter new answers each time instead of repeating the same code over and over. :)

This article has been dead for over six months. Start a new discussion instead.