Skip to main content
GameDev.net gamedev.net
🔒 Locked

c++ error C4700: uninitialized local variable 'num1' used

Started by indelvarn Dec 14, 2012 at 7:45 AM 1 replies 3.7k views
Original Post
indelvarn
indelvarn
can anyone tell me what's the problem here?

c++ error C4700: uninitialized local variable 'num1' used

#include "stdafx.h"
#include
int main()
{
int num1 = 1;
int num2 = 2;
while (num1 < 101)
{
printf("%d\n",num1);
printf("%d\n",num2);
int num1 = num1 + num2;
int num2 = num1 + 1 + num2;
}
}
kunos
kunos
you declare and initialise num1 at the beginning. Then on this line:

int num1 = num1 + num2;

You are redeclaring another "num1" that is "shadowing" (hiding) the first one you've declared. At this point the compiler sees the "num1" in the expression as the one you've just declared.

You don't need to declared a new num1, you just need to assign it, so, to fix:

num1=num1+num2;

Or, more to the point:

num1+=num2;

Make sure you read and understand (google for it) variable declaration, scoping and assignment.
indelvarn
indelvarn

you declare and initialise num1 at the beginning. Then on this line:

int num1 = num1 + num2;

You are redeclaring another "num1" that is "shadowing" (hiding) the first one you've declared. At this point the compiler sees the "num1" in the expression as the one you've just declared.

You don't need to declared a new num1, you just need to assign it, so, to fix:

num1=num1+num2;

Or, more to the point:

num1+=num2;

Make sure you read and understand (google for it) variable declaration, scoping and assignment.

thanks!

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.