Uninitialized local variable that HAS been initialized!

Basically I have a integer "numExs" that is initialized by user input. Then, I use a for loop to output the appropriate number of X's, as dictated by the "numExs" integer.

However, I get a C4700 warning that "numExs" is not an initialized local variable. If I am reading it right then 'local' is the keyword there, but I don't know how to re-initialize it. The program does not run with the warning.

Any help would be appreciated. Many thanks.

/*Write a program that asks the user for a number between 1 and 10, then prints a line of that many “X”s
as shown below. Reject any entries outside of the range of 1 to 10 by displaying “Entry is out of range.”
You must use a FOR LOOP for this problem. The variable used to store the number of Xs must be called
“numExs”.*/
#include <iostream>
using namespace std;
int main()
{
int numExs;
int counter;
cout<<"Please enter the number of Xs (1-10): ";
cin>>numExs;
counter = numExs;
for (int numExs; counter>0; counter--) // C4700 ERROR HERE
{
if (numExs>0 && numExs<11)
{
cout<<"X";
}
else
{
cout<<"Entry is out of range.";
}
}
cout<<endl;
}

line 21 is declaring a new integer and hiding the variable you declared on line 12, and that is the one the compiler is complaining about. Rewrite that line like this: for (counter = numExs; counter>0; counter--) then delete line 19.

i dont think you will get a error here , its a warning saying that the corresponding variable(numExs) is not used.
more over the new declaration of numExs is local to the for loop it self, so all local variable have garbage value bydefault.

Ancient Dragon: The purpose of the if/ else statement is to output a number of X's equal to the integer stored as numExs. Or, if numExs is not in the 1-10 integer range, to output a try again message. Sorry that wasn't clear. I see what you mean though about why it's incorrect; I had been thinking that numExs was somehow still changing. I was thinking wrong. Thanks for the attention you've given my post!

Gaiety: Thank you too for helping! And I think the code you posted is right on. Taking the if/else statement and placing it before the for statement is a great idea. I hadn't thought of it because I had been thinking chronologically, and to me that meant the notice that a number was "Out of range" would be the last thing in the program. Screwy logic I have. Thanks again, I think that'll work!