How to use Rand???

I am trying to create a program that produces random addition problem. When the user gets the answer correct how do I make it to where it displays 4 different types of responses when you get the answer right? Like if you get it right, it randomly generates great job, well done, and so on. How do I do that?


This is my program so far but it only generates one response for now:
#include "stdafx.h"
#include <iostream>
#include <cmath>
using namespace std;



int main()

{
int userGuess, answer, num1, num2;
cout << "Welcome to the addition game!" << endl;
cout << "Calculate the problems below, when finished type -1 to view results.\n\n";

num1 = 1 + rand() % 10;
num2 = 1 + rand() % 10;

cout << " What is " << num1 << "+" << num2 << "?";
cin >> userGuess;
answer = num1 + num2;

if (userGuess == num1 + num2)
cout << "Great Job ";

else
cout << "Wrong answer moron " << endl;


return 0;
}

You need to call srand() first, to seed the random number generator.
http://www.cplusplus.com/reference/cstdlib/srand/

You should call srand just the once, typically at the start of your program. And you usually pass the current time.

If you pass the same seed you will get the same sequence of numbers (which can be helpful when testing?)

Andy

Last edited on
Hello LyonPredator,


PLEASE ALWAYS USE CODE TAGS (the <> formatting button) when posting code.
It makes it easier to read your code and also easier to respond to your post.
http://www.cplusplus.com/articles/jEywvCM9/
http://www.cplusplus.com/articles/z13hAqkS/
Hint: You can edit your post, highlight your code and press the <> formatting button.
You can use the preview button at the bottom to see how it looks.

Give this a try. I have not tested this yet, but I believe the concept is right:

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
#include "stdafx.h"
#include <iostream>
#include <cmath>
#include <ctime>  // <--- Added.

using namespace std;

int main()

{
	int userGuess{}, answer{}, num1{}, num2{};  // Always a good idea to initializze your variables.
	srand(size_t()time(NULL));

	cout << "Welcome to the addition game!" << endl;
	cout << "Calculate the problems below, when finished type -1 to view results.\n\n";

	num1 = 1 + rand() % 10;
	num2 = 1 + rand() % 10;

	cout << " What is " << num1 << "+" << num2 << "?";
	cin >> userGuess;
	answer = num1 + num2;

	if (userGuess == num1 + num2)
	{
		switch (rand() % 4 + 1)
		{
			case 1:
				cout << "Great Job ";
				break;
			case 2:
				break;
			case 3:
				break;
			case 4:
				break;
			default:
				cout << "Wrong answer moron " << endl;
				break;

		}
	}
	

	return 0;
}


Hope that helps,

Andy

EDIT:switch
Last edited on
Now that you know "how to do it", take a deep breath and read:
http://www.cplusplus.com/reference/random/

That leads to:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#include <iostream>
#include <random>
#include <chrono>
#include <string>
#include <vector>

int main()
{
  const std::vector<std::string> answers {"One", "Two", "Three", "Four"};

  unsigned seed1 = std::chrono::system_clock::now().time_since_epoch().count();
  std::default_random_engine generator( seed1 );
  std::uniform_int_distribution<int> distribution( 0, answers.size() - 1 );

  for ( int roll=0; roll<10; ++roll ) {
    std::cout << answers[ distribution(generator) ] << '\n';
  }
  return 0;
}

There is time, seed, random numbers, and text lookup.
Is there any easier way to do this? I have yet to learn about switch and case in school yet.
1
2
3
4
5
6
7
8
#include <cstdlib>

int main()
{
  srand(0);
  int i = rand() % 100 + 1;
  ...
}
Is there any easier way to do this? I have yet to learn about switch and case in school yet.

Your question is:
I have four strings. I have a (random) number K from range [0..3]. How do I access Kth string?

Andy did post a switch-statement based solution. You cannot know it yet.

I did post "use K as index to array that stores the strings" solution. Let me guess, not shown yet?

Perhaps you have had if .. else if .. else if .. else?
IF yes, THEN use it.
Hello LyonPredator,

Working off what keskiverto suggested with if/else if I changed to this:

1
2
3
4
5
6
7
8
9
10
11
answer = rand() % 4 + 1;

if (userGuess == num1 + num2)
{
	if (answer == 1)
	{
		std::cout << "Great Job 1" << std::endl;
		numCorrect++;
	}
	else if(answer == 2)
	{


In the first if statement it should have been (userGuess == answer), but what you have works fine. Since "answer" was unused I gave it a different use. I will leave it to you to finish out.

Keep the switch/case code somewhere for future use.

Hope that helps,

Andy
Registered users can post here. Sign in or register to post.