GIDForums  

Go Back   GIDForums > Computer Programming Forums > C Programming Language
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #11  
Old 21-Sep-2009, 19:17
MetalWarrior MetalWarrior is offline
New Member
 
Join Date: Sep 2009
Posts: 3
MetalWarrior is an unknown quantity at this point

Re: Things to Avoid in C/C++ -- system("pause"), Part 4


Quote:
Originally Posted by L7Sqr
When I do system("Pause"); the behavior I see is which I will assume is not what people want.
If you have a particular platform you want to support and need to make things 'professional' for that then write something specific on your own like turning off echo functionality, capturing keystrokes until Enter is pressed, and then restoring state and exiting your program.

Hey hey hey! This thread is about this : www.gidnetwork.com/b-61.html
It says one should avoid to use that command and the author is wondering why it's so popular.

I'm only explainning why it is popular and the good reasons it has to be.
I also say myself if you read a little more in my post that yes indeed system command should be avoided and that I know why. If you can't use the command... well it's just another flaw, I'm not defending it I'm explainning why it is popular.

I'm not trying to pick a fight or anything here... I think it's unreasonnable.

I'm only trying to ask for a better way to do a "Press ANY key" and not a silly replacement that will only take the key enter. Since I'm nothing but a student here I not try to be a smartass because I'm learning, not teaching.

I also seek the way to do it on the internet but couldn't get it quite well. The closest thing I got to was:

CPP / C++ / C Code:
    cout << "Press any key to continue." ;
    cin.clear();
    getch();

It needs to include conio.h for the getch();
It might not be the best thing but hey, at least I'm getting closer to it.

Oh yeah...

Quote:
Originally Posted by L7Sqr
Ranting to a year-old post doesnt help to solve your problem (or any other). Get a grip, read some documentation, and get to work. You'll find that the more you DO the less you are going to NEED.

Well if you check that article I told you about before : www.gidnetwork.com/b-61.html

It asks down below if I'd wish to comment about it. Well yes indeed I do want to comment about this article. If you leave me the possibility to comment about it when it may appears old and then bitch on me about it and tell me that asking for help is not going to help me here but that I have to search for documentation (which I waster hours and hours on doing) is what will help me well SORRY.

I'm comming here before the author of this page as a student and I am trying to have a civilised conversation in which exchange of informations which might lead to a solution for the situation's problem will occur.

So please, unless your post is related and helpfull toward a better situation I'm asking you to leave. I don't like talking with people that doesn't use their reasons and tell me to seek information somewhere else than on a discussion forum about programming languages such as C++.

Thanks for reading. ^^
  #12  
Old 22-Sep-2009, 07:19
Mexican Bob's Avatar
Mexican Bob Mexican Bob is offline
Member
 
Join Date: Mar 2008
Location: Chicxulub, Yucatán
Posts: 226
Mexican Bob is a jewel in the roughMexican Bob is a jewel in the roughMexican Bob is a jewel in the rough

Re: Things to Avoid in C/C++ -- system("pause"), Part 4


Quote:
Originally Posted by MetalWarrior
I'm not trying to pick a fight or anything here... I think it's unreasonnable.

...with a name like MetalWarrior, it could be easier to say that you're not trying to pick a fight had your name been Ghandi or something

Quote:
Originally Posted by MetalWarrior
I'm only trying to ask for a better way to do a "Press ANY key" and not a silly replacement that will only take the key enter.

The stream terminator by default is the result of the enter key being pressed. Modify the stream terminator to terminate on any character.

Quote:
Originally Posted by MetalWarrior
The closest thing I got to was:

CPP / C++ / C Code:
    cout << "Press any key to continue." ;
    cin.clear();
    getch();

It needs to include conio.h for the getch();

There is no "conio.h" in standard C or C++. It is a platform and compiler specific header. I advise against using it. The mere fact that it is still around is something to abhor.

Quote:
Originally Posted by MetalWarrior
It might not be the best thing but hey, at least I'm getting closer to it.

Trust me, you're definitely NOT getting any "closer" by using conio.h in any piece of code anywhere for any reason.


Quote:
Originally Posted by MetalWarrior
It asks down below if I'd wish to comment about it. Well yes indeed I do want to comment about this article.

...and I agree with you that you should post regardless of how old the original posting was/is.

Quote:
Originally Posted by MetalWarrior
I'm comming here before the author of this page as a student and I am trying to have a civilised conversation in which exchange of informations which might lead to a solution for the situation's problem will occur.

Welcome. Ask away.



MxB
  #13  
Old 22-Sep-2009, 13:04
L7Sqr L7Sqr is offline
Member
 
Join Date: Jul 2005
Location: constant limbo
Posts: 234
L7Sqr is a jewel in the roughL7Sqr is a jewel in the rough

Re: Things to Avoid in C/C++ -- system("pause"), Part 4


Quote:
Originally Posted by MetalWarrior
It asks down below if I'd wish to comment about it. Well yes indeed I do want to comment about this article.
I'm not suggesting that you dont have something to say, I'm saying that proper etiquette suggests that you start your own thread, reference this (and the original) post and ask your question.

Quote:
Originally Posted by MetalWarrior
I'm only explainning why it is popular and the good reasons it has to be.
The fact that something is widely used does not make it a good choice; consider the history of the gets function.

In reference to the original post, the whole reason the system("PAUSE") issue comes up is that there exist environments where an executable is hosted in its own command window. When the executable exits, so does the hosting window. To prevent this from happening one must suspend the exit of their program in one way or another or run the command by hand (instead of double-clicking an icon or from within an IDE).
Windows provides a PAUSE command that will enable a user to suspend the current command window - it is trivial to shell-out to that command to do the dirty work of pausing the program (this is what system("PAUSE") does). Realize, thought, that it is the PAUSE command that handles the input and output you see, not your program. That means that if your hosting environment doesnt support the PAUSE command, you cant do that.
This topic is not new and is well debated. I question your search techniques if you could not find any relevant information on the subject. Here is the top google hit for 'portable system pause'.

As an aside, the whole idea of this being an efficiency concern is likely a bit of a red herring. At any point where you are handing control to a user before continuing execution (especially directly before termination) is unlikely to be a point where you are going to be concerned with bottlenecks and resource hogs.
__________________
My personal site: Utilities for text processing, debugging, testing and plotting
  #14  
Old 22-Sep-2009, 15:20
MetalWarrior MetalWarrior is offline
New Member
 
Join Date: Sep 2009
Posts: 3
MetalWarrior is an unknown quantity at this point

Re: Things to Avoid in C/C++ -- system("pause"), Part 4


To L7Sqr
Thanks... I'm sorry for beingsuch a bother so I'm going to check out some more.
I learned a lot thanks to you.

Also... I'm going to look any way to get the inputs from the keyboard dirrectly... Thanks again for the information.
  #15  
Old 22-Sep-2009, 19:42
davekw7x davekw7x is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Left Coast, USA
Posts: 5,218
davekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to behold

Re: Things to Avoid in C/C++ -- system("pause"), Part 4


Quote:
Originally Posted by L7Sqr
...Here is the ...top google hit[/url] for 'portable system pause'.

Ummm---If people don't realize it's a portability problem, how the heck would they know to use that in a search phrase? It's up to We Who Know about such things to bring it up, as you have very capably done. But, in my opinion, it's not fair to knock them for not knowing. See Footnote.

Quote:
Originally Posted by L7Sqr
As an aside, the whole idea of this being an efficiency concern is likely a bit of a red herring.
Spot on! I have mentioned this to Walt a number of times. The argument about efficiency: It's just plain silly (in my opinion) under the circumstances being discussed.

Also, in my opinion, adding silly arguments just makes people think that the whole issue is, well, silly, and it waters down the importance of the significant part of the message. Namely the portability factor.


I mean, if it is "wrong" then explain why it is wrong in no uncertain terms and in terms that mean something to the target audience. "Expensive function?"---give us a break!

On the other hand, if it is not exactly "wrong" but maybe not exactly"right" either, meaning that one has an opinion that it should be dis-recommended, then I think the arguments should be sound and consistent either from a pedagogical point of view or from a practical point of view (maybe even both). But that's just my opinion.

I mean, saying that a person deserves a prison sentence because he "robbed a bank" is one thing, but adding a charge that he also deserves time in the Pokey because he "married a horse" doesn't contribute to the gravity of the matter, in my opinion.

As far as a previous poster's argument about looking "professional" I have to ask (rhetorically), now that we are approaching the second decade of the 21st century and people surf the web on their mobile phones, how "professional" is a command-line program that looks just about the way that it would have looked on DOS 3.3 (or whatever) in 1986? Don't paying customers expect more?

Don't get me wrong, I write command-line programs just about every day, for my own use and for embedded systems. A "professional" look is not part of the mix. The emphasis is on functionality. (You don't need to be told to, "Ignore the Man Behind the Curtain," because there is no curtain!)

If I wanted a non-graphical semi-professional look, I would probably go for a (non-standard) ncurses (it even has getch()) application, but portability will still be an issue. I mean, even though the basic ncurses functions exist in libraries for Windows and for Linux machines, there are (almost) always implementation-dependent differences.

Oh, yeah, another thing...

The "Press any key . . . " prompt that I get from system("pause") compiled with a Borland or Microsoft compiler for my Windows xp box is kind of confusing to me. I mean, I can't even find a key marked 'any' on the keyboard! I tried a bunch of keys and I found that it doesn't respond to a 'Shift' key or a 'Ctrl' key or an 'Alt' key or the 'PrtScrn' key or the 'ScrLck' key or the 'Pause' key. Don't even get me started on 'Caps Lock' or 'Num Lock!"

"Press any key," indeed! Hmph!

Anyhow for people going beyond the beginning phase (or for people for whom system("pause") is not an option), they should expect to have to learn a little about how input data from a keyboard interacts with programs. (Maybe that comes in a later lesson.)

As has been stated by a number of people, including yours truly (but apparently Walt hasn't "got it" yet), the reason that system("pause") is popular with people for whom it is an option is that it is easy to use and makes it easy for beginners (maybe even not-so-beginners) to run programs from their IDE without learning much about user input. I hate to repeat myself, but I am not challenging Walt's "wrong" designation with this particular statement (I do that elsewhere) or claiming it is "right." We are just trying to explain to Walt, one more time, why it is popular.

The problem (from the beginner's point of view) is that there are no standard C or C++ library functions that implement the exact functionality of the desired system("pause"). Period. Full Stop.

(I probably should say that again, but as I have said so very many times, I hate to repeat myself.)


If I ever needed to do such a thing, I would probably change the paradigm a little and use something like:

CPP / C++ / C Code:
    printf("Press 'Enter' to exit the program: (Ironic isn't it?) . . . ");
    getchar();

Now if there were any characters left in the input buffer (as there would be if there had been any scanf() statements to read stuff into a numeric variable, for example), there's a little more to it;
CPP / C++ / C Code:
    int x;
    printf("Enter an integer: ");
    scanf("%d", &x);
.
.
.
    /* 
     * An "empty" loop to get rid if everything in the input stream buffer
     * so that the program will wait for something new from the user.
     */
    while (getchar() != '\n')
        ;

    printf("Press 'Enter' to exit the program: (Ironic isn't it?) . . . ");
    getchar();
    return 0;
} /* end of main() */


Or some such thing.

Now the user can press anything he/she wants and as many times as he/she wants. When he/she finally gets around to pressing the 'Enter' key, as he/she was instructed to do, things proceed.

(Well, actually, with the setup that I am using at this moment, if I hit 'ScrLck' a couple of times the KVM switch changes the Keyboard, Video, and Mouse inputs to another computer, but that has nothing to do with system("pause") or getchar() or any other C or C++ thing. I hope you get the idea in general. I mean, other than that...)

Regards,

Dave

P.S. I like Walt. I really do. Walt has helped lots of folks here and otherwhere. I hope my comments are taken in the spirit in which they are intended. They are, after all, only opinions. (I also know that Walt is tough. He can take it!)

Footnote:

"I was born not knowing,
and have only had a little time
to change that, here and there."
---Richard Feynman
  #16  
Old 23-Sep-2009, 00:48
WaltP's Avatar
WaltP WaltP is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Midwest US
Posts: 3,335
WaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to all

Re: Things to Avoid in C/C++ -- system("pause"), Part 4


Quote:
Originally Posted by davekw7x
Spot on! I have mentioned this to Walt a number of times. The argument about efficiency: It's just plain silly (in my opinion) under the circumstances being discussed.
Considering that's not - repeat not -- my only objection, I still think it's worth considering. Granted at the end of the list...

Quote:
Originally Posted by davekw7x
As has been stated by a number of people, including yours truly (but apparently Walt hasn't "got it" yet), the reason that system("pause") is popular with people for whom it is an option is that it is easy to use and makes it easy for beginners (maybe even not-so-beginners) to run programs from their IDE without learning much about user input. I hate to repeat myself, but I am not challenging Walt's "wrong" designation with this particular statement (I do that elsewhere) or claiming it is "right." We are just trying to explain to Walt, one more time, why it is popular.
duh! Why do you think this has to be explained to me? What makes you think I haven't "got it"? The reason for my writing this 'article' is to teach, to raise knowledge of the neo. And if a neo want's to argue with the professionals, that's a good thing. Hope he can learn something. After all, I was a neo-programmer back in the 70's and still remember the easy ways (and still use some if truth be told). But I also know the 'correct' ways, too. And why they are the correct ways.

I actually think I resent this paragraph as it seems to make me out as an ego that can't understand other opinions and I need to be told multiple times because I'm dense. No, it's a 5-year old post. Just because I haven't updated it to make it longer by adding all the irrelevant info ("it's easier", "it's the way teachers teach it", etc) does not mean I don't "get it." Anyway, I have no idea if I can update it. It's 5 years old after all. It's old enough for kindergarten...

Anyway, I respect you too much to take it to heart, though. Knowing you, I'm sure you didn't mean quite how it reads. At least I hope I know this...




Quote:
Originally Posted by davekw7x
P.S. I like Walt. I really do. Walt has helped lots of folks here and otherwhere. I hope my comments are taken in the spirit in which they are intended. They are, after all, only opinions. (I also know that Walt is tough. He can take it!)
You rock, Dave. And yes, I can take it. Especially from you. Friendly jabs are always welcome. (It's that other guy I won't take it from -- no one here, by the way)

=================================
Additional comments:
Just because Microsludge came up with "Press any key" does not make it a standard. It is not efficient. And to people that are strictly users,
Press any key to continue
and
Press ENTER to continue
are the same. They don't really care that much.

Quote:
Originally Posted by MetalWarrior
When you do system("Pause"); it does :

Press any key to continue.

then the user press any key and it goes on... It can be ANY key! Even an arrow will do.
And what are the ramifications to pressing an arrow key? Unless you know that, suddenly you're in a world you don't want to be in.


Quote:
Originally Posted by MetalWarrior
But what your 'replacement' (if we can call this a replacement that is) does is :

Press the Return key to continue.
bdl;dfgjlds lol I can write things

Then the user press enter and THEN it continues. It isn't clean, it doesn't look proffesionnal
A professional will program the input, not simply use exactly what I suggested. And what I suggested is a beginning step out of the neo world of I/O. And if you want professional, "Press any key" is and always has been the non-professional hack that separates neophytes from programmers. Anytime I see that phrase, I know the programmer was too lazy to "do it right" and just bought into the Microsludge way of doing things.

Come to think of it, can you name any professional programs that use the phrase "Press any key to continue" anywhere in their program? This is a serious question -- I really want to know because I can't think of any at all.


Quote:
Originally Posted by MetalWarrior
So please... if you REALLY have an efficient replacement, that doesn't need to include any library, I'd be more than happy to hear it.
Using your definition of efficient, no there isn't one. As mentioned elsewhere on this board (and probably all other C/C++ boards as well) the languages were not designed with keyboard input in mind, therefore keyboard input is relatively primitive. As programmers, we simply have to deal with that fact.


Quote:
Originally Posted by switch72
So the user has to press specific key, and translating that to the user can be more difficult than you think. What if the users keyboard doesn't have a return key, but rather an enter key. Users don't always know these mean the same thing.
In my experience, ALL keyboards today have an Enter key. How can that be a translation nightmare? Yes, I realize I wrote Return in the article -- a faux pas on my part. Sorry.



As for posting to a ?? old thread, this is a teaching thread -- therefore open to comments and criticisms any time. It's the dubious help that's given in the main forums that are months/years too late that is frowned upon. This thread is an appropriate place for MetalWarrior's comments.


=================================
Quote:
Originally Posted by davekw7x
Footnote:

"I was born not knowing,
and have only had a little time
to change that, here and there."
---Richard Feynman
And to quote George Fenneman: Three brownie points to those who know this reference...
"And here he is, the one, the only... GROUCHO!"
__________________

During the election they said Obama could only be elected when pigs fly. Well, we currently have an epidemic of Swine Flu. Coincidence?
  #17  
Old 23-Sep-2009, 04:12
Mexican Bob's Avatar
Mexican Bob Mexican Bob is offline
Member
 
Join Date: Mar 2008
Location: Chicxulub, Yucatán
Posts: 226
Mexican Bob is a jewel in the roughMexican Bob is a jewel in the roughMexican Bob is a jewel in the rough

Re: Things to Avoid in C/C++ -- system("pause"), Part 4


Quote:
Originally Posted by WaltP
In my experience, ALL keyboards today have an Enter key. How can that be a translation nightmare? Yes, I realize I wrote Return in the article -- a faux pas on my part. Sorry.

No need to kick thy self, WaltP. My MacBook has a key labeled "enter" AND "return."




Return is printed in larger letters! Long live the RETURN KEY!


MxB
  #18  
Old 23-Sep-2009, 09:43
davekw7x davekw7x is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Left Coast, USA
Posts: 5,218
davekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to behold

Re: Things to Avoid in C/C++ -- system("pause"), Part 4


Quote:
Originally Posted by WaltP

duh! Why do you think this has to be explained to me?
I was responding to a poster who quoted the very first line of the narrative in the article at http://www.gidnetwork.com/b-61.html
that said
Quote:
Originally Posted by WaltP
I've never understood why system("PAUSE") is so popular.
If you now understand why it is popular, maybe you can change it to that we know that you understand.
Quote:
Originally Posted by WaltP
...But I also know the 'correct' ways, too. And why they are the correct ways.
I appreciate your contributions and your opinions. I think that giving others the benefits of your experience is a Good Thing. I share most of your views, and in particular I don't think I have ever used system("pause") except during testing so that I could post my feeble attempt at humor: The chronicles of my search for that ever-elusive 'any' key---not exactly as gripping as the search for Bigfoot, but it amuses me from time to time.
Quote:
Originally Posted by WaltP

...mean quite how it reads
I was trying to poke the bear a little, and my comments might be silly (there's that word again, but this time I'm calling myself silly), but I meant no disrespect. That's why I added the P.S. I don't think I have ever called you silly; just this particular argument.
Quote:
Originally Posted by WaltP
...It is not efficient....
I don't think "neos" can grasp the concept of why system("pause") is somehow less efficient than getchar(). Furthermore for non-"neos" I don't think its terribly important that system("pause") may make the system do more work than getchar(). Awareness of what happens with system() calls is something that they probably don't teach in beginning programming classes, and I think it's good to talk about it.
However...
I may be repeating myself, but sometimes I just can't help it: I just think that mentioning its inefficiency adds bupkiss to the discussion of the use of system("pause"), and is actually distracting from the part with which I whole-heartedly agree: It's not portable. (But that's only my opinion.)

Sometimes "neos" are given an assignment that requires them to use system("pause"). If they take our advice and use getchar(), it will not be possible to satisfy the assignment. I just pointed out that if you change the functionality, you should make sure that the prompt matches the functionality.

I was also trying to emphasize the good point that you made in your article that using getchar() is an alternative to system("pause") that has "almost" the same functionality, but is portable.

Quote:
Originally Posted by WaltP
..."Press any key" is and always has been the non-professional hack that separates neophytes from programmers.
Well there are several things, but I agree that this one is pretty easy to spot from the user's view even if he/she doesn't see the source code.
Quote:
Originally Posted by WaltP
...programmer was too lazy...
I think that's a little harsh. New programmers don't think of these things on their own. They probably picked it up from a tutorial or from an instructor who wants to save teaching the "good stuff" about user I/O for a later lesson. The reason that your article is important and useful is that some instructors don't get around to the "good stuff", or maybe they just don't know better (maybe because the instructor's instructors didn't know better, or even the instructor's instructors' instructors didn't know better...) and were never put into an environment that "real" programmers live in, where system("pause") is not an option. See Footnote.
Quote:
Originally Posted by WaltP
And to quote George Fenneman: Three brownie points to those who know this reference...
"And here he is, the one, the only... GROUCHO!"

Well, I'm waiting for the duck to drop down.

Groucho had a line that went, "If you've heard this story before, don't stop me, because I'd like to hear it again." (Unlike me, he didn't mind repeating himself.)


Regards,

Dave

Footnote:
"Men are born ignorant, not stupid. They are made stupid by education."
---Bertrand Russel
 
 

Recent GIDBlogProblems with the Navy (Chiefs) by crystalattice

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: Things to Avoid in C/C++ -- gets() , Part 1 WaltP C Programming Language 5 21-Jun-2007 13:13
Re: Things to Avoid in C/C++ -- feof(), Part 3 WaltP C Programming Language 0 19-Sep-2005 21:00
Re: Things to Avoid in C/C++ -- fflush(stdin), Part 2 WaltP C Programming Language 0 17-Sep-2005 21:39
Web Site Optimization - the basic things dimos31 Search Engine Optimization Forum 3 19-Jun-2005 13:00
[Tutorial] Pointers in C (Part II) Stack Overflow C Programming Language 0 27-Apr-2005 18:36

Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 16:33.


vBulletin, Copyright © 2000 - 2009, Jelsoft Enterprises Ltd.