Um... yes, it is Windows-specific. system() executes a command in the shell.
system("pause") will execute pause, which does not exist on Linux:[thomas@singularity ~]$ pause
-bash: pause: command not found
Apart from that little pedantism of mine :oops:, I'd like to remark that blocking a program when it is not necessary is generally not a good idea. When a program is finished with whatever it is doing, then it should exit.
If, for whatever reason, a user wants execution to block, he can still do that from the shell, but it should not be mandatory from the program's side (unless there is a good reason, like waiting for input).
The only problem with using console_runner is that if the user runs the executeble outside of Code::Blocks it will immediatly close and the user won't be able to see the output.
Here is my suggestion.
#include <iostream>
int main(int argc, char *argv[])
{
std::cout << "Hello world!";
std::cout << std::endl;
std::cout << "Press ENTER to continue.";
std::cout << std::endl;
std::cin.get();
return 0;
}
There is a drawback with this however if you run this code from Code::Blocks you have to press enter twice to "exit" the program.
I'm not saying the template needs to be changed but this is just my take on things. One question how come main is
instead of
int main(int argc, char *argv[])
If it'll have to be changed, I would change it
from:
#include <iostream>
int main()
{
std::cout << "Hello world!" << std::endl;
return 0;
}
to something like:
#include <iostream>
using namespace std;
int main(int argc, char *argv[])
{
cout << "Hello world!" << endl;
return 0;
}
How come a discussion of Hello World two pages long?
This thread is absolutely priceless :lol: Realy... I like it a lot.
I think the main should not contain the blocking statement. Anyway a console application should be run in a console, am I right? So actually Code::Blocks does his job well, when you run the console app from the IDE, Code::Blocks waits for e keystroke, when you run outside of the IDE you should run from a console. As simple as possible.
Concerning the main(), the parameters of the function should be definitely present:
int main(int argc, char *argv[])
It's much more comprehensive.
Concerning the main(), the parameters of the function should be definitely present:
int main(int argc, char *argv[])
It's much more comprehensive.
Personally, I prefer when I do not need specific options, otherwise int main(int argc, char *argv[])
What is important is that the main must return type int.
BS says in his technical FAQ (http://www.research.att.com/~bs/bs_faq2.html#void-main):
A conforming implementation accepts
int main() { /* ... */ }
and
int main(int argc, char* argv[]) { /* ... */ }
Michael
Yes, but remember hello world examples are made for NEWBIES :). Teaching them a little won't hurt anyone.
When I was a C++ beginner, this main
int main(int argc, char *argv[])
always scared me :D.
Now a little less, but still... :D.
Michael
Personally, I prefer when I do not need specific options
What is important is that the main must return type int.
Yes, you're right that only the int return type is important. However it's easier personally for me (and for the newbies) to delete the parameter list, than to re-type it (in case I need it). And for such console applications the command line parameters are frequently used...
What do you say about putting an "annoying" comment before the main... ? This way the newbies will not be scared, and this way they learn too... :)
// You may delete the parameter list, if you don't use it
int main(int argc, char *argv[])
For advanced users - remember: you always have the option to make your own template... I think those predefined hello world templates should target the newcomers. But of course all this subject is a matter of personal taste.
What do you say about putting an "annoying" comment before the main... ? This way the newbies will not be scared, and this way they learn too... :)
// You may delete the parameter list, if you don't use it
int main(int argc, char *argv[])
Yes, that could be a useful addition. May be:
// If you do not use the parameter list, you can use instead:
// int main()
int main(int argc, char *argv[])
Important it is to not forget the returned type. I have read in an article that a teacher used:
Because it was easier to understand for the students. There was not the problem of returned type... :roll:. Na ja, could be. But this is not C++ or not ISO standard C++.
Michael