I'd like to randomly generate the letters a - d (a,b,c,d), which I have done below. What I'd like to do is have each of the letters assigned to a comment, so that when the letter is called, and I use 'cout', it will print the comment assigned to the letter (and not the letter itself).
I think ultimately I want to use a global variable or some function for this, but really just trying to understand how to do the first part, or whatever is easiest.
The line of interest is in the 'if' statement (ignore the 'else').
#include <iostream>
#include <stdlib.h>
#include <time.h> /* time */
usingnamespace std;
// random multiplying function
int multiplying(int v1, int v2)
{
int computer_answer;
computer_answer = v1 * v2;
return computer_answer; // function can only return 1 thing
}
int main()
{
int v1, v2 , human_answer = 0;
int computer_answer = 1;
char comment;
// loop runs until user gets question correct
while (human_answer != computer_answer)
{
/* initialize random seed: */
srand (time(NULL));
v1 = rand() % 10 + 1; // v1 in the range 1 to 10
v2 = rand() % 10 + 1; // v2 in the range 1 to 10
computer_answer = multiplying(v1,v2); //the function 'multiplying(v1,v2)' takes 2 random number inputs v1,v2 and sets it equal to the return variable
cout << "What is " << v1 << " * " << v2 << " ? ";
cin >> human_answer;
// correct answer
if (human_answer == computer_answer)
{
//comment = rand char a1-a4; // randomly choose a Correct comment statement associated by the number 1-4
//cout << comment << endl;
char ch = 'a' + rand() % (('a'-'d') + 1); // generates a random leter between the two given
char a [] = "Very good!";
char b [] = "Excellent!";
char c [] = "Nice Work!";
char d [] = "Keep up the good work!";
cout << "Random comment is: " << ch << endl;
}
// incorrect answer
else
{
//comment = rand char b1-b4; // randomly choose a Correct comment statement associated by the number 1-4
//cout << comment << endl;
}
}
cin.ignore();
cin.get();
return 0;
}
BTW, you don't want to call srand() inside your loop at line 29. That will reset the random number sequence back to the beginning each time through the loop resulting in the same response each time. You want to call srand() ONCE at the begiining of your program (line 25).
Thanks for your responses! I've changed my program so that I use if/else statements for comment outputs (I guess it's preference for me to use if/else vs. switch).
I've also relocated the srand() outside of my while loop.
My remaining issue is that the random calls don't appear random at all, but instead also call a '2' if answer is correct, and a '4' if answer is incorrect.
I've also relocated the srand() outside of my while loop.
My remaining issue is that the random calls don't appear random at all, but instead also call a '2' if answer is correct, and a '4' if answer is incorrect.
You relocated your rand calls with your srand call, so you are only calling rand twice per run of the program. Big surprise, then, that you only get the two values you called it for.
Ok, so how would I fix this? I do not know where they should be.
If you want something to be repeated every iteration of a loop (such as the generation of a random number,) the prevailing strategy is to put it inside the loop. See the code I posted upthread for a reasonable place to put your rand() call (specifically what block of code it's in.)