954,500 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

declaring an int within a for loop

Hi,
can you have a quick look at the two for loops here. I've declared the int 'i' in the first for loop but in the second for loop it doesn't recognise it and i have to declare it again. Why is that?

for(int i=0; i<x; i++)
	  {
		  cout << "Enter a number ";
		  cin >> y;
		  *(aaa + i)= y;
	  }
  }

  cout << "The numbers entered are ";
  for(int i=0; i<x; i++)
	   cout << *(aaa + i) << ", ";
radiat
Newbie Poster
17 posts since Feb 2012
Reputation Points: 10
Solved Threads: 1
 

Always use { } to make your code readable and consistent.
This then no longer will be a problem.

WaltP
Posting Sage w/ dash of thyme
Moderator
10,506 posts since May 2006
Reputation Points: 3,348
Solved Threads: 944
 

ok , i did that, but it's still doesn't recognise 'i' in the second for loop. Here's the full program. (Yeah i know i shouldn't be using _getch(), i'll get around to sorting that out.)

#include <iostream>
#include <sstream>
#include <fstream>
#include "conio.h"
using namespace std;

int main()
{
  int x, y;
  int * aaa;
  
  cout << "How many numbers do you want to enter? \n";
  cin >> x;

  aaa = new(nothrow) int[x];

  if (aaa==0)
	  cout << "Error, memory not assigned";
  else
  {
	  for(int i=0; i<x; i++)
	  {
		  cout << "Enter a number ";
		  cin >> y;
		  *(aaa + i)= y;
	  }
  }

  cout << "The numbers entered are ";

  for(i=0; i<x; i++)  
  {
	    cout << *(aaa + i) << ", ";
  }


  _getch();
}
radiat
Newbie Poster
17 posts since Feb 2012
Reputation Points: 10
Solved Threads: 1
 
ok , i did that, but it's still doesn't recognise 'i' in the second for loop.


That's because you didn't define it in the second loop. You only definedi in the first loop.

(Yeah i know i shouldn't be using _getch(), i'll get around to sorting that out.)


Why "get around" to it? Just use cin.get() and be done with it...

WaltP
Posting Sage w/ dash of thyme
Moderator
10,506 posts since May 2006
Reputation Points: 3,348
Solved Threads: 944
 

because when i use cin.get() the command prompt closes before i press a key

radiat
Newbie Poster
17 posts since Feb 2012
Reputation Points: 10
Solved Threads: 1
 

That's because you read the number from your input but not the NewLine you also pressed. Loop up cin.ignore ...

I'm surprised the _getch didn't do the same thing...

WaltP
Posting Sage w/ dash of thyme
Moderator
10,506 posts since May 2006
Reputation Points: 3,348
Solved Threads: 944
 

Like Walt P says, when you define an integer within a for loop, it only is recognized within that for loop in which it is defined. If you would have defined i outside of the for loop, then your code will work.

pattmorter
Light Poster
26 posts since Feb 2012
Reputation Points: 10
Solved Threads: 1
 

Think of a function as a building,

You can walk down the hall all the way, and when you get to a room the door/walls etc belong to that room, (room = loop, if, case, etc)

You cant access the door from the next or the previous room in the room you currently are (now don't get technical and create doors from 1 room to the other :P, this example there is only a hallway and the rooms branch from that) ;)

Eagletalon
Junior Poster
113 posts since Mar 2011
Reputation Points: 47
Solved Threads: 13
 

Hi, can you have a quick look at the two for loops here. I've declared the int 'i' in the first for loop but in the second for loop it doesn't recognise it and i have to declare it again. Why is that?

for(int i=0; i<x; i++)
	  {
		  cout << "Enter a number ";
		  cin >> y;
		  *(aaa + i)= y;
	  }
  }

  cout << "The numbers entered are ";
  for(int i=0; i<x; i++)
	   cout << *(aaa + i) << ", ";

You can also look at it this way:
What happens in Vegas stays in Vegas. Yourfor loop is Vegas.

Actually, it has to do with variable scope. If you have code like this:

int main()
{
   .....
   .....
 
  {
      int x = 7;
      cout << x;    //no problem
  }

   cout << x;      // ERROR - x undeclared indentifier.

   return 0;
}

Any variable defined within brackets { } is not visible, and in most cases doesn't even exist anymore, outside of those brackets. Andfor loops and while loops both have that same kind of domain thing, that if a variable is defined anywhere in the loop statement or body, then it's existence is limited to that "scope" (even if you are not using brackets).

In the past, that wasn't so, you could use the variable outside of the loop. But eventually the standard made the variable's scope confined to the loop. But some compilers, like MSVS, allow you the choice of having the compiler recognize the variable outside of the loop or forcing compliance with the standard. Of course you would want it to force compliance to the standard, unless you were compiling old source code that expected the variable to still be in scope outside of the loop.

MandrewP
Junior Poster
111 posts since Nov 2009
Reputation Points: 33
Solved Threads: 21
 

This question has already been solved

Post: Markdown Syntax: Formatting Help
You