plz correct the code

Started by
17 comments, last by Zahlman 17 years, 5 months ago
Quote:
BTW, why should one not use system("pause") at the end of a console program like this?

Because it's Flat Out Stupid. It is equivalent to things like "We don't call delete on this particular pointer, because the OS will clean up the memory when our process dies" and "the user has selected the quit option, so let's quit real fast: int *p = 0; *p = 10;."

And so on. Yes, they achieve the desired end result, but they do it in obviously stupid and potentially dangerous, non-portable ways. Note that non-portability is generally the least of the problems here.

When you run a program built for a console environment from your IDE, the IDE spawns a console environment. The program runs to termination and quits. At this point, most console environments will terminate themselves because they are smart enough to know that they were launched with the express purpose of running a single command (your executable). This is the correct, desired behavior of both the console environment and your program.

A program built for a console environment is designed to run in a console environment. Artificially pausing the program prior to exit is counter-intuitive in that environment. Think of how annoying it would be use to the command program if every time your listed a directory, changed directories, et cetera, you were promted to "press a key to continue." Think of how impossible it would be to write a batch file uses a program that requires user input to fully terminate itself, even if it otherwise requires no user interaction. It's exactly the same as the obnoxious "You picked the quit option, are you sure you want to quit?" prompts (these are forgivable only if quitting would cause you to lose state or information, such as when you have unsaved work; otherwise they are redundant and counter-intuitive). Just don't do it.

Any decent IDE (including Visual Studio -- it's either Run or Run Without Debugging, I tend to forget which, my keymapping is nonstandard, and I don't have VS installed on the machine I'm currently at) has a means to invoke a console-environment program and persist the console window. This could be as simple as generating a batch file that calls your program, and then calls "pause" or some other platform-specific delay mechanism. The difference, however, is that that method does not hard-code incorrect delay logic into your program.
Advertisement
Quote:Original post by Julian90
I dont want to sound picky but you should use const even when passing by value IF it is SEMANTICALLY correct, also the other reason you would which doesnt really matter as much is that the compiler is able to perform additional optimizations if you do :-), or at least i think so it was in another thread awhile ago so please correct me if im wrong.


That's the point, the optimizations you talk about comes from the fact that the code have more knowledge about the function (it doesn't alter the value). When passing by value you get no additional information, so the function is exactly the some seen from the outside.

The only change is that you will not be able to reuse the parameter inside the function (leading to slower and less intuitive code).

Almost all other kind of consts should be used however because they DO change the function's contract.

Quote:That's in the Coding Standards document where I work :|

Well it could be in there for many reasons.

You might have migrated from C and kept some of the still legal standards to keep the code consistent. This is such a small issue that there is no reason to really stop doing it, however neither are there any reasons to start doing it.

Also many C++ programmers (still the minority, but a significant part) are still programming in C, but using a couple of C++ features.

So do you believe it's a good thing that you're forced to do it? Can you tell us why such a thing is required? If you present good points then some of us might see your point and start doing it ourselves.

EDIT: Also I second what jpetrie said about explicitely pausing, however I often feel beginners' have others just as big design problems.
I was merely casually observing the fact. No insinuation that it's either good or bad. The extra eight characters typed per parameter-less function doesn't really impact on my day too much :P

The surprised face was that I had never questioned the validity of the standard, and just blindly accepted it! I think you're probably right that at one time the company used C as its main language.
Quote:Original post by jpetrie
Quote:
BTW, why should one not use system("pause") at the end of a console program like this?

Because it's Flat Out Stupid. It is equivalent to things...


I think when you're just starting out, and can barely manage to construct a class, portability and efficient batch execution is not high on the list of concerns; nor is the desire to customize your build environment.

Of course jpetrie, you are absolutely technically correct, but quick fixes such as system("pause") could in theory accelerate the rate of learning new techniques, as long as you're careful later on.
No bombs, No guns, just an army of game creators...
I disagree. "Control-F5" verus "F5" (or whatever the appropriate key combination swappage is) to persist the console window after execution is trivial to learn (just as trivial as system("pause")) and get into the habit of. It requires no customization of the environment in any environment I've every worked in.

I see no reason that system("pause") should ever be used or taught to beginners in this respect; there's little point in learning and using things you know to be incorrect with the rationale that "you'll change it later."
Quote:Original post by jpetrie
I disagree. "Control-F5" verus "F5" (or whatever the appropriate key combination swappage is) to persist the console window after execution is trivial to learn (just as trivial as system("pause")) and get into the habit of. It requires no customization of the environment in any environment I've every worked in.
How about Dev-C++? I don't think it has such a feature. Even the example console app it provides has system("pause") in it IIRC. And I see no reason not to use it during development if there's no better alternative.
Quote:Original post by davidx9
I think when you're just starting out, and can barely manage to construct a class, portability and efficient batch execution is not high on the list of concerns; nor is the desire to customize your build environment.

Of course jpetrie, you are absolutely technically correct, but quick fixes such as system("pause") could in theory accelerate the rate of learning new techniques, as long as you're careful later on.

Unlearning bad habits can be harder than learning the correct habits in the first place. Plus, if the objective is to accelerate learning, perhaps a better solution is to choose a language other than C++?

Quote:Original post by Anonymous Poster
How about Dev-C++? I don't think it has such a feature. Even the example console app it provides has system("pause") in it IIRC. And I see no reason not to use it during development if there's no better alternative.

But there are. And the better alternatives are free, too.

Dev-C++ isn't being actively developed. There's no reason to attach yourself to a dead product.
Quote:Original post by Oluseyi
But there are. And the better alternatives are free, too.
Such as? Also consider how light-weight, easy to install and small Dev-C++ is. Even though I have VC++ installed, I sometimes use Dev-C++ for small tests because it starts up faster, requires less memory and uses GCC as a compiler (in case I need to check differences between VC and GCC). And I see no problem using it for such purposes. system("pause") doesn't bother me enough to even consider other options.
Just because I'm me...

#include <iostream>#include <string>using namespace std;struct Blood_Pressure {  float upper;  float lower;  Blood_Pressure(float upper, float lower) : upper(upper), lower(lower) {}};class Patient {  std::string name;  Blood_Pressure BP;  public:  Patient(const std::string& name, const Blood_Pressure& BP) :     name(name), BP(BP) {}  bool bloodPressureIsNormal() {    return BP.upper == 120 && BP.lower == 80;  }};int main() {  std::string name;  cout << "Enter your name: ";  std::getline(cin, name);  float upper, lower;  cout << "Enter upper limit: ";  cin >> upper;  cout << "Enter lower limit: ";  cin >> lower;  Patient p(name, Blood_Pressure(upper, lower));  if (p.bloodPressureIsNormal()) {    cout << "Blood pressure is normal." << endl;  } else {    cout << "Blood pressure is not normal." << endl;  }}

This topic is closed to new replies.

Advertisement