### Randomly generating letters that have assignemnts

Hi,

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').

 ``12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667`` `````` #include #include #include /* time */ using namespace 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; }``````
There is not much use of the multiplying function. You could have straightaway evaluated v1*v2, which is what the function ultimately does.

To answer your question, I would just generate a random number between 1 and 4 and use a switch statement on that number.

Also, you should use the new <cstdlib> and <ctime> instead of stdlib.h and time.h.
Simply create an array of strings:
 ``2324`` `````` const string responses [4] = { "resp 1", "resp 2", "resp 3", "resp 4" };``````

 ``4546`` `````` int ch = rand() % 4; cout << responses[ch] << endl;``````

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).
 ``1234567891011121314151617181920212223`` ``````#include #include #include #include // ... if (human_answer == computer_answer) { const char* comments[] = { "Very good!", "Excellent!", "Nice work!", "Keep up the good work!" }; unsigned numComments = std::end(comments) - std::begin(comments) ; std::cout << "Random comment: " << comments[(std::rand() % numComments)] << '\n' ; } // ... ``````

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.

Any ideas?

 ``12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091`` `````` #include #include #include /* time */ using namespace 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; int v3 = rand() % 4 + 1; // response number for correct answers int v4 = rand() % 4 + 1; // response number for incorrect answers /* initialize random seed: */ srand (time(NULL)); // loop runs until user gets question correct while (human_answer != computer_answer) { 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, will terminate while loop if (human_answer == computer_answer) { // char ch = 'a' + rand() % (('a'-'d') + 1); // generates a random leter between the two given cout << "Random number for comment = " << v3 << endl; if (v3 == 1) {cout << "Very good!";} else if (v3 == 2) {cout << "Excellent!";} else if (v3 == 3) {cout << "Nice Work!";} else // (v3 == 4) {cout << "Keep up the good work!";} } // incorrect answer if (human_answer != computer_answer) { cout << "Random number for comment = " << v4 << endl; if (v4 == 1) {cout << "No. Please try again";} else if (v4 == 2) {cout << "Wrong. Try once more.";} else if (v4 == 3) {cout << "Don't give up";} else // (v4 == 4) {cout << "No. Keep trying.";} cout << "\n" << endl; } } cin.ignore(); cin.get(); return 0; }``````
* I meant to say that the code always calls 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.
 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.)
Last edited on
Thanks for everyone's help. I changed a few things around and I got my code working now!
Topic archived. No new replies allowed.