Jump to content

  • Log In with Google      Sign In   
  • Create Account


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


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
2 replies to this topic

#1 indelvarn   Members   -  Reputation: 103

Like
0Likes
Like

Posted 14 December 2012 - 01:45 AM

can anyone tell me what's the problem here?

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

#include "stdafx.h"
#include <stdio.h>
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;
}
}

Sponsor:

#2 kunos   Crossbones+   -  Reputation: 2205

Like
5Likes
Like

Posted 14 December 2012 - 01:51 AM

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.
Stefano Casillo
Lead Programmer
TWITTER: @KunosStefano
AssettoCorsa - netKar PRO - Kunos Simulazioni

#3 indelvarn   Members   -  Reputation: 103

Like
0Likes
Like

Posted 14 December 2012 - 06:54 AM

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!




Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS