Thanks ahead of time for reading, and any help you give.

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.

commented: Very helpful and knowledgeable +0

Thanks so much Ancient Dragon. And that works because I'm no longer declaring numEx as a new integer, if I've read you right, which makes sense. I appreciate the help!

Any reason the else statement might be blanked out when it runs now?

Thanks!

Because that if statement is incorrect. It uses numExs, which never changes. Its unclear what the purpose of that loop is. What is it supposed to do?

for (int numExs; counter>0; counter--)  // C4700 ERROR HERE	{		
                   if (numExs>0 && numExs<11)	
	    {
                   	cout<<"X";	
                    }
                   else	
                   {
                      cout<<"Entry is out of range.";
                   }
	}

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.

the code can be written as

if(numExs > 10)
    {
       cout << "Out of Range";
    } 
else
{         
   for (counter=numExs; counter>0; counter--)  
     {		
                   cout<<"X";	
     }
                 
 }
commented: Solved my thread! Very helpful. +0

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!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.