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 =/
// 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 */
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 : )
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?
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?
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.
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.