Problem with functions

EDIT: I just found out that I get this error in Visual C++ 2010 but not Dev-C++... I don't really know what the deal is with that.



I have an assignment for school where I need to code a random number game. I have to break it up into 3 functions. The function that I'm running into here is my generate function, which creates the random number. My problem is this: After I enter the option, I get a runtime error stating that 'random' is being used without being initialized. Can anybody help me out? Here is the code:

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
int generate()
{
	int max;
	int min;
	int random;
	int option;

	cout<<"1) Play 1 - 100"<<endl;
	cout<<"2) Define your own numbers"<<endl;
	cin>>option;

	if (option == 1){

		int random = rand() % 100 + 1;		
	}

	if (option == 2){
		
		cout<<"Pick the lowest possible number: ";
		cin>>min;
		cout<<endl;

		cout<<"Pick the highest possible number: ";
		cin>>max;
		cout<<endl;

		int random = rand() % max + (min + 1);
	}

	return random;
}


If you need to look at more of my code to help me out, I would be glad to provide it.

Thanks in advance to anybody who can help me!
Last edited on
Line 14 and 27 you are creating the variable "random" again when you already created it on line five. Delete the "int" on those lines and try it out.

Remember, you only need to say the variable type when you are creating it, after that you just need to say the name then = then something to set the variable to.
Topic archived. No new replies allowed.