Code::Blocks Forums

User forums => General (but related to Code::Blocks) => Topic started by: Angelo on September 11, 2005, 12:02:33 pm

Title: Sugesstion about small changes in standard cpp code.
Post by: Angelo on September 11, 2005, 12:02:33 pm
It's litttle, but useful idea.
Change standard cpp code, from:

#include <iostream>

int main()
{
   std::cout << "Hello world!" << std::endl;
   return 0;
}

to:

#include <iostream>

using namespace std;

int main(int argc, char *argv[])
{
    system("PAUSE");
    return EXIT_SUCCESS;
}

imo will be great.
Title: Re: Sugesstion about small changes in standard cpp code.
Post by: polygon7 on September 11, 2005, 02:52:03 pm
#include <iostream>

using namespace std;

int main(int argc, char *argv[])
{
    system("PAUSE");
    return EXIT_SUCCESS;
}

If i remember correctly system("PAUSE") and return EXIT_SUCCESS are Windows specific. So this code will not be portable to other systems.
Title: Re: Sugesstion about small changes in standard cpp code.
Post by: mandrav on September 11, 2005, 03:37:04 pm
The system("pause") command is not windows only, but I see no reason to alter the template. Besides, C::B keeps the console program open and waits for a keypress to close it. So why use system("pause")???

If you like your template better, just save it as a user template and use it for starting new projects.
Title: Re: Sugesstion about small changes in standard cpp code.
Post by: thomas on September 11, 2005, 05:44:20 pm
Um... yes, it is Windows-specific. system() executes a command in the shell.
system("pause") will execute pause, which does not exist on Linux:
Code
[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).
Title: Re: Sugesstion about small changes in standard cpp code.
Post by: grv575 on September 11, 2005, 09:03:25 pm
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.

Well for the newbie, I think the window staying open is good feedback.  It's confusing behavior when you hit the IDE run button and there is a quick flash of a console opening and closing (and then they have to figure out how to code it so it doesn't happen).  Most console cases you do want to see any printf() results anyway.

And if you don't want this behavior then it's just as easy to run the app from explorer (IMO).  I realize it's just a matter of personal preference so either way is suitable, but just think it's easier for newbies the way it is.
Title: Re: Sugesstion about small changes in standard cpp code.
Post by: mandrav on September 11, 2005, 09:16:02 pm
Besides, C::B keeps the console program open and waits for a keypress to close it.

Nobody knows it?? :shock:
Title: Re: Sugesstion about small changes in standard cpp code.
Post by: me22 on September 11, 2005, 09:52:46 pm
I'd also like to point out that using directives are discouraged and that EXIT_SUCCESS is in <cstdlib>, so you should be including that header to use it.
Title: Re: Sugesstion about small changes in standard cpp code.
Post by: David Perfors on September 11, 2005, 10:31:45 pm
Besides, C::B keeps the console program open and waits for a keypress to close it.

Nobody knows it?? :shock:
I know it :D it is a great feature :D
Title: Re: Sugesstion about small changes in standard cpp code.
Post by: thomas on September 12, 2005, 12:40:18 am
Besides, C::B keeps the console program open and waits for a keypress to close it.

Nobody knows it?? :shock:
I know it :D it is a great feature :D
Yep, and that is cool, because it is how things should be.
Title: Re: Sugesstion about small changes in standard cpp code.
Post by: grv575 on September 12, 2005, 01:42:59 am
Oh I misread.  Looks like everyone is against keeping the program blocking before it exits.
Title: Re: Sugesstion about small changes in standard cpp code.
Post by: sethjackson on September 12, 2005, 03:22:03 am
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.

Code
#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

Code
int main()

instead of

Code
int main(int argc, char *argv[])
Title: Re: Sugesstion about small changes in standard cpp code.
Post by: grv575 on September 12, 2005, 03:37:26 am
Code
    std::cin.get();

But I think the point was that if people are writing in a unix style, with multiple processes/utilities and piped communication, then programs should just exit and provide an exit status code.  Which is what's likeable about the console_runner approach (it permits both convenience within CB and process integration w/ pipes or whatever).

It's not too bad to open cmd.exe and drag and drop the .exe from Explorer.
Title: Re: Sugesstion about small changes in standard cpp code.
Post by: thomas on September 12, 2005, 10:01:31 am
But I think the point was that if people are writing in a unix style, with multiple processes/utilities and piped communication, then programs should just exit and provide an exit status code.  Which is what's likeable about the console_runner approach (it permits both convenience within CB and process integration w/ pipes or whatever).
It's not too bad to open cmd.exe and drag and drop the .exe from Explorer.
That is exactly it. :)
A program that blocks at the end of execution takes away the user's freedom of choice and, too, limits itself unnecessarily by being unable to be run non-interactively.

Apart from that, I think that is also an important point in ideology. Software is to serve the human, not to dictate him. Unluckily, most of the current "maintream" software does not respect that because it is too self-important. Think of even such simple a thing as an installer, or think of your antivirus software, or download a file with IE, and you know what I mean.
Title: Re: Sugesstion about small changes in standard cpp code.
Post by: takeshimiya on September 12, 2005, 01:27:45 pm
If it'll have to be changed, I would change it

from:
Code
#include <iostream>

int main()
{
   std::cout << "Hello world!" << std::endl;
   return 0;
}

to something like:
Code
#include <iostream>

using namespace std;

int main(int argc, char *argv[])
{
   cout << "Hello world!" << endl;
   return 0;
}
Title: Re: Sugesstion about small changes in standard cpp code.
Post by: phlox81 on September 12, 2005, 05:45:23 pm
Makes the "using namespace std;" really sense there ?
I feel with std:: much better. And if, it should belong _in_ the main-function.
Title: Re: Sugesstion about small changes in standard cpp code.
Post by: takeshimiya on September 12, 2005, 06:22:23 pm
It's the same, but in most hello world examples, it's the using namespace statement, to show that feature of C++.

Also it's ok, because the scope is in the main.cpp, and probably other functions would be there, and will be using the std namespace also.

I'd suggest to put what Stroustrup or the books put as a 'hello world' example.
Title: Re: Sugesstion about small changes in standard cpp code.
Post by: me22 on December 06, 2005, 05:45:10 pm
It's the same, but in most hello world examples, it's the using namespace statement, to show that feature of C++.

No, it's because most books about C++ are actually teaching you C ( just with cin/cout and some classes ) :x

using directives were introduced to aid in porting old code to the new standard, but none of the C++ experts ( Bjarne, Myers, Sutter, Andrescu, ... ) actually suggest using them.   using declarations are acceptable, providing their scope is limited.  ( Your main function could have using std::cout; using std::cin if all it does is deal with input and pass it off to other functions, for example. )

There's a whole lot of stuff in std:: ( which was a mistake, but that's how it is ) and you can really get bitten by it.  std headers are allowed to include whatever they want and iirc implementations are allowed to put whatever they want extra in std::, since the C++ user is technically not allowed to put anything in there.  This means that there can be all kinds of conflicting function and variable names.  Even the standard headers have functions with common names such as min, max, count, find, copy, ...
Title: Re: Sugesstion about small changes in standard cpp code.
Post by: Michael on December 06, 2005, 08:00:06 pm
Hello,

I have found this article "C++ Namespaces and the 'using' Directive" interesting:

http://www.bgsu.edu/departments/compsci/docs/namespaces.html

Best wishes,
Michael
Title: Re: Sugesstion about small changes in standard cpp code.
Post by: Ptomaine on December 09, 2005, 12:28:49 am
What a wonderful thread I can see :)

"You can't imagine how it matters" (quot. from the "Charlie And The Chocolate Factory" movie)  :lol:
Title: Re: Sugesstion about small changes in standard cpp code.
Post by: knue on December 09, 2005, 01:20:52 am
Why not build a bug in the "Hello World" template?
All beginners wil despair. :D
Title: Re: Sugesstion about small changes in standard cpp code.
Post by: rickg22 on December 09, 2005, 04:53:27 am
Actually, that's NOT a bad idea. If we put in the comments how to fix the lines, we might give beginners a crash course on bug fixing.

// My first bug (TM). This yields a "blablah" error because yadda yadda.
// To fix, replace the above line with:
// ...........

On a second thought, I can't imagine the dread WE will experience with "I fixed the hello world bug, but it still doesn't compile :( " complaints.  :eek:

No, thanks, nevermind.
Title: Re: Sugesstion about small changes in standard cpp code.
Post by: takeshimiya on December 09, 2005, 09:18:24 am
lol :D

How come a discussion of Hello World two pages long?
Title: Re: Sugesstion about small changes in standard cpp code.
Post by: Michael on December 09, 2005, 10:44:02 am
lol :D

How come a discussion of Hello World two pages long?

Yeah :D. But as big things born from smaller ones....
Title: Re: Sugesstion about small changes in standard cpp code.
Post by: bszente on December 14, 2005, 08:07:03 pm
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:
Code
int main(int argc, char *argv[])
It's much more comprehensive.
Title: Re: Sugesstion about small changes in standard cpp code.
Post by: Michael on December 14, 2005, 08:26:23 pm
Concerning the main(), the parameters of the function should be definitely present:
Code
int main(int argc, char *argv[])
It's much more comprehensive.

Personally, I prefer
Code
int main()
when I do not need specific options, otherwise
Code
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):

Quote
A conforming implementation accepts
   int main() { /* ... */ }
and
   int main(int argc, char* argv[]) { /* ... */ }

Michael
Title: Re: Sugesstion about small changes in standard cpp code.
Post by: rickg22 on December 14, 2005, 08:29:52 pm
Yes, but remember hello world examples are made for NEWBIES :). Teaching them a little won't hurt anyone.
Title: Re: Sugesstion about small changes in standard cpp code.
Post by: Michael on December 14, 2005, 08:38:43 pm
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

Code
int main(int argc, char *argv[])

always scared me :D.

Now a little less, but still... :D.

Michael
Title: Re: Sugesstion about small changes in standard cpp code.
Post by: yop on December 14, 2005, 09:53:27 pm
Yes, but remember hello world examples are made for NEWBIES :). Teaching them a little won't hurt anyone.
A typical new project at my work:
Fire up KDevelop.
New QMake project -> Hello world application
Nope Hello world is not for newbies, it's the beginning of everything ;)
Title: Re: Sugesstion about small changes in standard cpp code.
Post by: bszente on December 15, 2005, 08:57:50 am
Personally, I prefer
Code
int main()
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...  :)
Code
// 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.
Title: Re: Sugesstion about small changes in standard cpp code.
Post by: Michael on December 15, 2005, 11:38:32 am
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...  :)
Code
// 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:

Code
// 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:

Code
main()

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