C++ errors

Heyo,

I'm new to C++ and I'm having trouble having the function grab two random integers between 1-12 to spit out an equation.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#include <iostream>
#include <rand>

using namespace std;

int first_value;
int second_value;
int user_value;

int main()

do
{
	first_value = % 12 + 1
	second_value = % 12 = 1
}
While (first_value != num) // (second_value != num)

cout << "What is " << first_value << " x " << second_value << "?"


I feel I'm missing a return, but I'm not actually sure. Any help?
You're missing more than just a return.

Line 2: There is no <rand> header file. The declaration for rand() is in the <cstdlib> header.

Line 11: missing {

You're also missing a call to srand() to seed the random number generator. Without it, you're going to get the same sequence of numbers every time you run your program.

Lines 14,15,17,19: You missing a ; to terminate the statement.

Line 17: while should not be capitalized.
num is not defined.

line 20: Missing the following:
1
2
3
    //  rest of program
    return 0;
}



your main function has no body(Curley Braces), your code is global. If you have more code, please post it. It's always best to post all of your code, or at least as much as possible.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#include <iostream>
#include <rand>

using namespace std;

int first_value;
int second_value;
int user_value;

int main()
{
      do
      {
	      first_value = % 12 + 1
	      second_value = % 12 = 1
      }
      While (first_value != num) // (second_value != num)

      cout << "What is " << first_value << " x " << second_value << "?"
}
Last edited on
closed account (E0p9LyTq)
One way to generate two uniformly distributed numbers between 1 - 12:

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 <chrono>
#include <random>

int main()
{
   // obtain a seed from the system clock:
   unsigned seed = static_cast<int> (std::chrono::system_clock::now().time_since_epoch().count());

   // seeds the random number engine, the mersenne_twister_engine
   std::mt19937 generator(seed);

   // set a distribution range (1 - 12)
   std::uniform_int_distribution<int> distribution(1, 12);

   // pick two random numbers
   int firstValue = distribution(generator);
   int secondValue = distribution(generator);

   std::cout << firstValue << "\t" << secondValue << std::endl;

   return 0;
}

Possible output:
7       8
Thanks for all the help, I really appreciate it.

I've updated the main body, I'll repost the code now just so I don't have to explain all the edits.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#include <iostream>
#include <cstdlib>

using namespace std;

int first_value;
int second_value;
int user_value;

int main()

{
	do;
	{
		first_value = rand() % 12 + 1;
		second_value = rand() % 12 = 1;
    
	while (first_value != num // second_value != num);

	cout << "What is " << first_value << " x " << second_value << "?";
	return 0;
}


In terms of the random generator, what will I need to do? Do I add a call before the body? If so, or if not, how might I do that? As a final question, how would I define "num" in line 17? With any integer, or?
Last edited on
closed account (E0p9LyTq)
@Rhenzo51,

you need to call ONCE the srand() function before you use the rand() function, seeding the pseudo-random number generator. Otherwise you will get the same series of numbers each time you run the program.

The best way to do the seeding, if you insist on using the C library random generator, is to include the <ctime> header and use the output of the time() C library function as input to the srand() function: srand(time(NULL));.

The C library random generator is not the best method for obtaining random numbers. You might want to look at the C++ <random> library.

Look at the code I posted earlier for an example of using the C++ <random> library.
How, in the generation, can I exclude the previous outputted numbers? Not all, just the previous one before each generation. Per se, it outputs 1 5, the next generation will not have 1 and 5, but the one after can.
Last edited on
Topic archived. No new replies allowed.