![]() |
|
#1
|
||||
|
||||
Re: Things to Avoid in C/C++ -- system("pause"), Part 4I just posted a GID Community blog entitled: Things to Avoid in C/C++ -- system("pause"), Part 4.
Quote:
Please let me know what you think. This is an auto-generated message posted by GIDNetwork™ on behalf of the author. __________________
During the election they said Obama could only be elected when pigs fly. Well, we currently have an epidemic of Swine Flu. Coincidence? |
||||
|
#2
|
|||
|
|||
Re: Things to Avoid in C/C++ -- system("pause"), Part 4I read your article and it makes sense. But it also raises another question for me. Why does it exist?
|
|
#3
|
||||
|
||||
Re: Things to Avoid in C/C++ -- system("pause"), Part 4Because the system() function can be used to execute any operating system command, like dir, ls, rename, etc. Even shutdown given the proper command syntax.
The pause command is used in batch files to pause execution of the file. And like many things in life, you are allowed to combine things that should not be combined, like peanut butter and pickles. Both are useful, but together they suck. __________________
During the election they said Obama could only be elected when pigs fly. Well, we currently have an epidemic of Swine Flu. Coincidence? |
|
#4
|
|||
|
|||
Re: Things to Avoid in C/C++ -- system("pause"), Part 4The problem is when you try to substitute one command for another, you often times lose functionality. The problem with the proposed solution is that it doesn't do the same thing as 'pause'. getchar and get require you to press the return key. 'pause' let's you exit on any key press. 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.
The other problem is that getchar() at least takes the last keystrok out of the keyboard buffer. But the return stroke isn't removed from the buffer when entering data. So the problem is that if the last thing you did before calling getchar was hitting the return key to enter data, then getchar will pull that last return from the buffer and your program will exit anyway, so in order to insure the program pauses correctly, you must call getchar twice. But then what if you have a program that sometimes ends on data entry, and sometimes doesn't. Then occasionally you will have to instruct the user to press the return key twice. But sometimes only once. And now you have suddenly gotten into a lot more trouble. If you are pausing your program anyway you aren't exactly in the middle of a long series of calculations and so efficiency doesn't matter anyway. Just go ahead and call system("pause"). That's my opinion anyway. |
|
#5
|
|||
|
|||
Re: Things to Avoid in C/C++ -- system("pause"), Part 4The problem with using it is when you start mixing portable C/C++ with code that is not portable. Heck, my CS class told us to use system( "pause" ) in an intro C++ course, and most of the students had no idea it was a Windows only use of the system command. It was a stupid problem when they went to later classes and started programming in Linux environments.
|
|
#6
|
|||
|
|||
Re: Things to Avoid in C/C++ -- system("pause"), Part 4Oh I agree. I think everything Walt said initially was correct as well. I'm just saying that, when you want to use system("pause") it isn't so much as a bad thing. Really though, there needs to be a C command that mimics this, or, you need to write your own function that does it.
|
|
#7
|
||||
|
||||
Re: Things to Avoid in C/C++ -- system("pause"), Part 4If you wish to be a professional programmer, system("pause") is a bad thing, for all the reasons I stated. And since C/C++ is not designed with keyboard I/O in mind, the functionality you are missing with proper coding doesn't really exist anyway. They are nonportable enhancements in the first place.
If you wish to disagree, that's your prerogative. But, at the expense of flogging a deceased equine, I for one refuse to go to the corner store via Northwest Airlines just because they serve pretzels. __________________
During the election they said Obama could only be elected when pigs fly. Well, we currently have an epidemic of Swine Flu. Coincidence? |
|
#8
|
|||
|
|||
Re: Things to Avoid in C/C++ -- system("pause"), Part 4Unless what you are trying to do is go for a flight and eat pretzels. If that's the case, walking to the corner store won't do.
|
|
#9
|
|||
|
|||
Re: Things to Avoid in C/C++ -- system("pause"), Part 4Dear WaltP
I am utterly pissed off after hours and hours of unfructuous ressearches. Upon falling on your article my head simply exploded in rage in my defeat to find a meaningfull answer. Yes I understand that the system("Pause"); is not cost efficient. Yes I know that this is the reason one should avoid system commands. But do you know why it is so popular? Because it is clean and perfect. Don't understand what I mean? I'll explain. 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. I'm not telling you you're stupid but it doesn't take a C++ genius to use 'cin' to pause. 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 (Especially when it uses frames and that the text goes over it) and over all, it might be cost efficient but it is a rather poor way to do it. You seem to be calling yourself as an average guy when it comes to C++ script so... Me, the little student in computer sciences from Québec will not call you stupid or anything... No no I have far less knowledges than you in that. What people want is an efficient "Press any key to continue." and here they have it. I know of the cost calling a system command does so I am seeking a way to do it without the system pause. It is pretty hard to find to tell the truth. I've been searching, googling but it just give nothing. 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. After all, creating a keylogger kind of virus is not as hard as this. TL;DR system("Pause"); = Press any key to continue. cin.get(); & getchar(); = Press the Return key to continue. lol writting stuff before pressing enter Thus ( cinget(); + getchar(); ) is worst than ( system("Pause"); ) when it comes to looking professional. NEED ANOTHER WAY! |
|
#10
|
|||
|
|||
Re: Things to Avoid in C/C++ -- system("pause"), Part 4Quote:
Quote:
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. 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. __________________
My personal site: Utilities for text processing, debugging, testing and plotting |
Recent GIDBlog
Review: Gel laptop cooling pad by crystalattice
| Thread Tools | Search this Thread |
| Rate This Thread | |
|
|
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