Author Topic: Sugesstion about small changes in standard cpp code.  (Read 24216 times)

Offline .angelo

  • Multiple posting newcomer
  • *
  • Posts: 71
  • I'm just a newbie here.
    • .angelo home-page
Sugesstion about small changes in standard cpp code.
« 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.
I'm not my father son.
http://angelo.h-q.pl (English available!)

Offline polygon7

  • Multiple posting newcomer
  • *
  • Posts: 104
    • Home site
Re: Sugesstion about small changes in standard cpp code.
« Reply #1 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.
best regards,
p7
 Free open source UML modeling tool: ArgoUML

Offline mandrav

  • Project Leader
  • Administrator
  • Lives here!
  • *****
  • Posts: 4291
    • Code::Blocks IDE
Re: Sugesstion about small changes in standard cpp code.
« Reply #2 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.
Be patient!
This bug will be fixed soon...

Offline thomas

  • Administrator
  • Lives here!
  • *****
  • Posts: 3979
Re: Sugesstion about small changes in standard cpp code.
« Reply #3 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: [Select]
[[email protected] ~]$ 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).
"We should forget about small efficiencies, say about 97% of the time: Premature quotation is the root of public humiliation."

Offline grv575

  • Official tester
  • Regular
  • ***
  • Posts: 381
Re: Sugesstion about small changes in standard cpp code.
« Reply #4 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.

Offline mandrav

  • Project Leader
  • Administrator
  • Lives here!
  • *****
  • Posts: 4291
    • Code::Blocks IDE
Re: Sugesstion about small changes in standard cpp code.
« Reply #5 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:
Be patient!
This bug will be fixed soon...

Offline me22

  • Official tester
  • Multiple posting newcomer
  • ***
  • Posts: 53
    • TA Universe
Re: Sugesstion about small changes in standard cpp code.
« Reply #6 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.

Offline David Perfors

  • Developer
  • Lives here!
  • *****
  • Posts: 560
Re: Sugesstion about small changes in standard cpp code.
« Reply #7 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
OS: winXP
Compiler: mingw
IDE: Code::Blocks SVN WX: 2.8.4 Wish list: faster code completion, easier debugging, refactoring

Offline thomas

  • Administrator
  • Lives here!
  • *****
  • Posts: 3979
Re: Sugesstion about small changes in standard cpp code.
« Reply #8 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.
"We should forget about small efficiencies, say about 97% of the time: Premature quotation is the root of public humiliation."

Offline grv575

  • Official tester
  • Regular
  • ***
  • Posts: 381
Re: Sugesstion about small changes in standard cpp code.
« Reply #9 on: September 12, 2005, 01:42:59 am »
Oh I misread.  Looks like everyone is against keeping the program blocking before it exits.

sethjackson

  • Guest
Re: Sugesstion about small changes in standard cpp code.
« Reply #10 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: [Select]
#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: [Select]
int main()

instead of

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

Offline grv575

  • Official tester
  • Regular
  • ***
  • Posts: 381
Re: Sugesstion about small changes in standard cpp code.
« Reply #11 on: September 12, 2005, 03:37:26 am »
Code: [Select]
    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.

Offline thomas

  • Administrator
  • Lives here!
  • *****
  • Posts: 3979
Re: Sugesstion about small changes in standard cpp code.
« Reply #12 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.
"We should forget about small efficiencies, say about 97% of the time: Premature quotation is the root of public humiliation."

Offline takeshi miya

  • Lives here!
  • ****
  • Posts: 1487
Re: Sugesstion about small changes in standard cpp code.
« Reply #13 on: September 12, 2005, 01:27:45 pm »
If it'll have to be changed, I would change it

from:
Code: [Select]
#include <iostream>

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

to something like:
Code: [Select]
#include <iostream>

using namespace std;

int main(int argc, char *argv[])
{
   cout << "Hello world!" << endl;
   return 0;
}

Offline phlox81

  • Multiple posting newcomer
  • *
  • Posts: 53
    • phlox81.de
Re: Sugesstion about small changes in standard cpp code.
« Reply #14 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.

Offline takeshi miya

  • Lives here!
  • ****
  • Posts: 1487
Re: Sugesstion about small changes in standard cpp code.
« Reply #15 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.

Offline me22

  • Official tester
  • Multiple posting newcomer
  • ***
  • Posts: 53
    • TA Universe
Re: Sugesstion about small changes in standard cpp code.
« Reply #16 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, ...
« Last Edit: December 06, 2005, 05:47:25 pm by me22 »

Offline Michael

  • Lives here!
  • ****
  • Posts: 1608
Re: Sugesstion about small changes in standard cpp code.
« Reply #17 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

Offline Ptomaine

  • Multiple posting newcomer
  • *
  • Posts: 37
Re: Sugesstion about small changes in standard cpp code.
« Reply #18 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:
The Real Beauty Is Not The One That You Look At With Admiration But The One You Close Your Eyes At.

Offline knue

  • Multiple posting newcomer
  • *
  • Posts: 32
Re: Sugesstion about small changes in standard cpp code.
« Reply #19 on: December 09, 2005, 01:20:52 am »
Why not build a bug in the "Hello World" template?
All beginners wil despair. :D

Offline rickg22

  • Lives here!
  • ****
  • Posts: 2283
Re: Sugesstion about small changes in standard cpp code.
« Reply #20 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.

Offline takeshi miya

  • Lives here!
  • ****
  • Posts: 1487
Re: Sugesstion about small changes in standard cpp code.
« Reply #21 on: December 09, 2005, 09:18:24 am »
lol :D

How come a discussion of Hello World two pages long?

Offline Michael

  • Lives here!
  • ****
  • Posts: 1608
Re: Sugesstion about small changes in standard cpp code.
« Reply #22 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....

Offline bszente

  • Multiple posting newcomer
  • *
  • Posts: 28
Re: Sugesstion about small changes in standard cpp code.
« Reply #23 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: [Select]
int main(int argc, char *argv[])It's much more comprehensive.

Offline Michael

  • Lives here!
  • ****
  • Posts: 1608
Re: Sugesstion about small changes in standard cpp code.
« Reply #24 on: December 14, 2005, 08:26:23 pm »
Concerning the main(), the parameters of the function should be definitely present:
Code: [Select]
int main(int argc, char *argv[])It's much more comprehensive.

Personally, I prefer
Code: [Select]
int main() when I do not need specific options, otherwise
Code: [Select]
int main(int argc, char *argv[])
What is important is that the main must return type int.

BS says in his technical FAQ:

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

Michael

Offline rickg22

  • Lives here!
  • ****
  • Posts: 2283
Re: Sugesstion about small changes in standard cpp code.
« Reply #25 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.

Offline Michael

  • Lives here!
  • ****
  • Posts: 1608
Re: Sugesstion about small changes in standard cpp code.
« Reply #26 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: [Select]
int main(int argc, char *argv[])

always scared me :D.

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

Michael

Offline yop

  • Regular
  • ***
  • Posts: 387
Re: Sugesstion about small changes in standard cpp code.
« Reply #27 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 ;)
Life would be so much easier if we could just look at the source code.

Offline bszente

  • Multiple posting newcomer
  • *
  • Posts: 28
Re: Sugesstion about small changes in standard cpp code.
« Reply #28 on: December 15, 2005, 08:57:50 am »
Personally, I prefer
Code: [Select]
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: [Select]
// 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.
« Last Edit: December 15, 2005, 09:06:28 am by bszente »

Offline Michael

  • Lives here!
  • ****
  • Posts: 1608
Re: Sugesstion about small changes in standard cpp code.
« Reply #29 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: [Select]
// 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: [Select]
// 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: [Select]
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