[C++] variable defined inside for-loop can't be used outside

Questions about programming languages and debugging
Post Reply
User avatar
ayu
Staff
Staff
Posts: 8109
Joined: 27 Aug 2005, 16:00
18
Contact:

[C++] variable defined inside for-loop can't be used outside

Post by ayu »

I don't recall this happening before, maybe I had the luck of always missing it? =P

If I define my variables outside the loop in the following program, then I can use them both outside and inside the loop. But if I define them in the loop head, then they only seem to work inside of the loop =/

Is this supposed to happen? Can anyone explain?

My code:

Code: Select all

#include <iostream>

using namespace std;

void main()
{
	int i=0, num=-1, sum=0;
	for(;num != 0; i++) 
	{
		cout<<"Ange nummer (avsluta med 0):";
		cin>>num;
		sum += num;
	}

	cout<<"Medelvärdet är:"<<sum/(i-1)<<endl;
}
"The best place to hide a tree, is in a forest"

G-Brain
Fame ! Where are the chicks?!
Fame ! Where are the chicks?!
Posts: 467
Joined: 08 Nov 2007, 17:00
16
Location: NL

Post by G-Brain »

Yup, that's the standard. For loops have their own level of scope.

Code: Select all

// Complicated Scope Program, variation 2
#include <iostream>
 
using namespace std; /* outermost level of scope starts here */
 
int i;
 
main(){              /* next level of scope starts here */
  int i;
  i = 5;
 
  for(               /* next level of scope starts here */
      int i = 1;
      i<10 and cout << i << ' ';
      ++i )  
  {                  /* next level of scope starts here */
    int i = -1;
    cout << i << ' ';
  }                  /* two levels of scope end here*/
 
  cout << i << endl;
}                    /* next and outermost levels of scope end here */

Code: Select all

1 -1 2 -1 3 -1 4 -1 5 -1 6 -1 7 -1 8 -1 9 -1 5
http://en.wikibooks.org/wiki/C%2B%2B_Pr ... Code/Scope

BTW: main() returns an int, it is not void.
I <3 MariaLara more than all of you

User avatar
ayu
Staff
Staff
Posts: 8109
Joined: 27 Aug 2005, 16:00
18
Contact:

Post by ayu »

No, main can be void as well. If I make it a procedure, I don't have to return a value. Think it will have problems with returning errors like exit(1) and such, but for simple applications, it's good : )
"The best place to hide a tree, is in a forest"

G-Brain
Fame ! Where are the chicks?!
Fame ! Where are the chicks?!
Posts: 467
Joined: 08 Nov 2007, 17:00
16
Location: NL

Post by G-Brain »

cats wrote:No, main can be void as well. If I make it a procedure, I don't have to return a value. Think it will have problems with returning errors like exit(1) and such, but for simple applications, it's good : )
void main() is just wrong. main() is supposed to return a status code. If you're just writing a procedure, return 0 for success. What compiler are you using that doesn't warn you about it anyway?

More info here: http://users.aber.ac.uk/auj/voidmain.shtml
I <3 MariaLara more than all of you

User avatar
ayu
Staff
Staff
Posts: 8109
Joined: 27 Aug 2005, 16:00
18
Contact:

Post by ayu »

G-Brain wrote: void main() is just wrong. main() is supposed to return a status code. If you're just writing a procedure, return 0 for success. What compiler are you using that doesn't warn you about it anyway?

More info here: http://users.aber.ac.uk/auj/voidmain.shtml
As I said, small program. No need for status codes. Also since it doesn't break the syntax and give any kind of logic error, then why change it? I was taught to use this when I first started taking C++ classes. Now, my teacher is very conservative, but I don't see a reason yet to break the habit. I'm using Visual Studio Express 2005 at the moment (yeah ....I know). But it's what the class uses, and they demand that I give them whole project files that can be compiled in that IDE.
"The best place to hide a tree, is in a forest"

G-Brain
Fame ! Where are the chicks?!
Fame ! Where are the chicks?!
Posts: 467
Joined: 08 Nov 2007, 17:00
16
Location: NL

Post by G-Brain »

Read the linked page. Startup routines expect main() to return. It's in the standard. Tell your teacher.
Be warned that if you post your "void main" code on the forums, you're going to get told to correct it. Responding with "my teacher said it's OK" is no defense; teachers have a bad habit of being wrong. Be safe, and post only standard code, and you'll find people concentrate on answering your other problems, rather than waste time telling you about this type of thing.
http://faq.cprogramming.com/cgi-bin/sma ... 1044841143
I <3 MariaLara more than all of you

User avatar
ayu
Staff
Staff
Posts: 8109
Joined: 27 Aug 2005, 16:00
18
Contact:

Post by ayu »

Ok, you win =)

I read the link now xF

Actually I got a little curious and googled it, then I saw that you linked to the same site that I found ^^

Actually this might explain some of the errors I have had with my programs before.....

Then tell me, why does some books teach void main() ?
"The best place to hide a tree, is in a forest"

G-Brain
Fame ! Where are the chicks?!
Fame ! Where are the chicks?!
Posts: 467
Joined: 08 Nov 2007, 17:00
16
Location: NL

Post by G-Brain »

cats wrote:Ok, you win =)
Everybody's a winner :P
cats wrote:Then tell me, why does some books teach void main() ?
They're old. void main() was acceptable in the C89 standard, I think.
I <3 MariaLara more than all of you

User avatar
ayu
Staff
Staff
Posts: 8109
Joined: 27 Aug 2005, 16:00
18
Contact:

Post by ayu »

Oh well, thanks for the pointer =)
"The best place to hide a tree, is in a forest"

Post Reply