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



1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
 #include <iostream>
#include <stdlib.h>
#include <time.h>       /* 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:
23
24
 
 const string responses [4] = { "resp 1", "resp 2", "resp 3", "resp 4" };


45
46
  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).
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#include <iostream>
#include <iterator>
#include <cstdlib>
#include <ctime>

// ...

        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?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91

#include <iostream>
#include <cstdlib>
#include <ctime>       /* 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.