Variable not initialised

Write your question here.

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
  
#include <stdlib.h>
#include <stdio.h>
#include <time.h>

int main()
{
	int Die1; /*Random Number 1-6*/
	int Die2; /*Random Number 1-6*/
	int Total=Die1 + Die2; /*Total value of the two dice*/


	/* Generate two random numbers 1-6 for the two dice*/

	srand((unsigned)time(NULL));
	Die1 = 1 + (rand() % 6); {
		printf("Die 1 value is: %d\n", Die1); }
	Die2 = 1 + (rand() % 6); {
		printf("Die 2 value is: %d\n\n", Die2); }

/* Show the total value of the two dice */

	Total= Die1 + Die2; {
		printf("Total value is: %d\n\n", Total); }

	/* Display win, loss or point message for first throw */

		if (Total == 7 || Total == 11) {
			printf("Congratulations, you win!\n"); }
		else if (Total == 2 || Total == 3 || Total == 12) {
			printf("Sorry, you lose.\n"); }
		else if (Total == 4 || Total == 5 || Total == 6 || Total == 8 || Total == 9 || Total == 10) {
			printf("Point to make is: %d\n"); }


	
		
		printf("Thanks for playing!\n\n"); 

	return 0;
}


This is the error:

Run-Time Check Failure #3 - The variable 'Die1' is being used without being initialized.
Run-Time Check Failure #3 - The variable 'Die2' is being used without being initialized.

What's the reason for this? I created this on Visual Studio 2005 at uni (yes it's a really old version) then I converted it to 2012 and it's given me problems.
The error message are clear enough.

1
2
3
	int Die1; /*Random Number 1-6*/
	int Die2; /*Random Number 1-6*/
	int Total=Die1 + Die2; /*Total value of the two dice*/


What do you think what will be the value of Total if neither Die1 nor Die2 were initialized?
1
2
3
	int Die1; 
	int Die2; 
	int Total=Die1 + Die2; 

What do you think Total will equals to here?
Last edited on
But it worked when I originally did it in 2005 so I don't know why I'm getting this error message
Because VS started to actually check code for validity?
Yor code yields you an undefined behavior. In theory it could do anything from exploding your PC to producing a vast amount of antymatter. In practice however it usually just writes some garbage to Total.You can solve it in several ways:
1) Preferred: change that line to int Total; //or better, int Total = 0 . You assigning it value in line 23 anyway.
2) Do
1
2
3
int Die1 = 0; 
int Die2 = 0; 
int Total=Die1 + Die2;
(Compiler probably wil optimize it away anyway.)
but if it set values for the die 1 and die 2 doesn't that make the random number generator pointless? or does it overwrite that 0?
This is essentially what you do now:
1
2
3
4
5
6
7
int Die1;
int Die2;
int Total = Die1 + Die2;
Die1 = rand();
Die2 = rand();
Total = Die1 + Die2;
/* use Total */

You are trying to use Die1 and Die2 on line 3. That is what the compiler thinks as an error, and rightly so, for Total does not need any value at line 3.

Lines 4 and 5 properly set the values for Die1 and Die2, and therefore the assignment to Total on line 6 is valid.
so you're saying replace line 1 and 2 with line 4 and 5? or leave lines 1,2,3 and then write the code from line 4,5,6 as well?
No. MiiNiPaa did already give several ways.

Replace the int Total = Die1 + Die2; with int Total;
That works, thanks a lot for your help everyone, sorry I took so long to understand! Have only been doing this for a short time.
I wonder how many times should be pointed out you that this statement

int Total=Die1 + Die2; /*Total value of the two dice*/

has no sense?

Repeate one more?

Change it at least to

int Total;
Note:
1
2
int Die1; /*Random Number 1-6*/
int Die2; /*Random Number 1-6*/

Its a random number 1-6. You can do it by doing this:
1
2
3
4
5
6
7
8
9
10
#include <ctime>
#include <cstdlib>
using namespace std;
int main{
     srand(time(NULL));
     int Die1 = rand() %6 +1;
     int Die2 = rand() %6 +1;
     int Total = Die1 + Die2
     return 0;
}

That should work, You were assingning to rand() after Total.
It looks like you are making a craps game simulator. Did you go any further with this program?
Topic archived. No new replies allowed.