Uninitialized local variable???

I'm trying to make this code which counts from 1000 to 0, then finds the remainder of each number that is 'less than 1000 but still greater than 0' by 5. I think the problem is that in the for loop that counted all the numbers from 1000 cant be used outside of the loop body??? Also, the 'num' variable that I used in the for loop isnt being used outside of it. (All the numbers that it stored in 'num' I cant use.) I think Im sort of right but not really sure.

Here is the code:


#include <iostream>

using namespace std;

int doMath()
{
int x = 0;
return x % 5;
}
int main()
{
/*Counts all numbers from 1000 and stops at 0.
Allnumbers are now stored in variable 'num'*/
for (unsigned int num = 1000; num > 0; --num)
cout << num;

//This function finds the remainder of all numbers in 'num' divided by 5
doMath();
int num;
int x = num;

//Testing all numbers to see whether they are a mutiple of 5
if (num > 0)
{
return 0;
}
else (num = 0);
{
return num;
}

cin.get();
return 0;
}


Anybody know how to fix it?
Last edited on
1
2
3
/*Counts all numbers from 1000 and stops at 0.
Allnumbers are now stored in variable 'num'*/
for (unsigned int num = 1000; num > 0; --num)


The variable 'num' is of unsigned int type, and not an array.
It cannot store more than 1 integer at a time.
So, all numbers are not stored in variable 'num'.
It will count from 1000 and stops at 0, each time it iterates the loop,
That number is stored in num.
So at the end of the loop, num has the value 0.

Easier way to solve this in your case is to put parenthesis for the 'for' loop {}.
And call the doMath() function from within the for loop.
http://www.cplusplus.com/doc/tutorial/control/


Next, doMath() function is not taking any arguments.
And also, you are not catching the return value in any variable.
This function needs to take input (num in your case), and the return value needs to be captured in another variable.
Please read more about function arguments and how to use return types.

http://www.cplusplus.com/doc/tutorial/functions/


Hope this helps.
Last edited on
Thank you very much.
Topic archived. No new replies allowed.