Sign in to follow this  

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

This topic is 1859 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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;
}
}

Share this post


Link to post
Share on other sites
[quote name='kunos' timestamp='1355471494' post='5010512']
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.
[/quote]
thanks!

Share this post


Link to post
Share on other sites

This topic is 1859 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this