Hello, I am a new CS student and I am taking a C++ course. I am running into problems with a simple number generation program. Basically, I have to generate 2 single digit numbers and then request the user to multiply them. (I know there is nothing in there yet for the user input or multiplication) I am having a problem with the functions. When I call randomGen in the below example, I get an error saying that the variables are uninitialized. If I put int in front of num1, num2 in the call, I get more errors. Anyone know what I am doing wrong? I have been working on this one section for a while, reading tutorials and what not, to no avail.

int randomGen (int num1, int num2);
int multiply (int num1, int num2);

void main ()
{
	int num1, num2;
	cout << "Multiplication\n";
	randomGen (num1, num2);
	cout << "What is " << num1 << " times " << num2 << endl;
}
int randomGen (int num1, int num2)
{
	num1 = rand () % 10;
	num2 = rand () % 10;
	return num1, num2;

Recommended Answers

All 3 Replies

If you get errors, the technique to get help is to post the errors exactly as you see them (cut & paste, don't retype).

Based on he code snippet you posted, you are only missing the randomizer srand() .

You need to #include<ctime>

At the start of main you need: srand (time(0));

Both of these are so that you will actually generate two random numbers.
Your randomGen function is trying to return 2 values. You need to make this
a void function that passes the numbers by reference.

void randomGen(int& num1, int& num2); //prototype

void randomGen (int& num1, int& num2) //function def.
{
num1 = (rand () % 10);
num2 = (rand () % 10);
}


Now it will send the two numbers back to main, you can cout them there, or pass them by
value to void multiply.

thanks alot, I should be able to figure everything out from here.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.