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

Angelo

  • Guest
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.

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: 4315
    • 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
[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).
"We should forget about small efficiencies, say about 97% of the time: Premature quotation is the root of public humiliation."

grv575

  • Guest
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: 4315
    • 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."

grv575

  • Guest
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
#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[])

grv575

  • Guest
Re: Sugesstion about small changes in standard cpp code.
« Reply #11 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.

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."

takeshimiya

  • Guest
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
#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;
}

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.