User Name Password Register
DaniWeb IT Discussion Community
All
What is DaniWeb IT Discussion Community?
You're currently browsing the C section within the Software Development category of DaniWeb, a massive community of 423,508 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 4,686 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our C advertiser: Programming Forums
Views: 1254 | Replies: 17
Reply
Join Date: Jul 2007
Posts: 5
Reputation: wonder87 is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
wonder87 wonder87 is offline Offline
Newbie Poster

A simple problem

  #1  
Jul 15th, 2007
Hello!As a newbie programmer I don't know how to freeze my command line output,when I execute a C program,in order to see the output.Output opens and closes very fast.The trick I know from Pascal to put a 'Readln' statement as a last line doesn't work(with a 'scanf' or something like that.Could anybody give me a clue about that?Thanks in advance.
AddThis Social Bookmark Button
Reply With Quote  
Join Date: Mar 2005
Posts: 154
Reputation: TkTkorrovi is on a distinguished road 
Rep Power: 4
Solved Threads: 11
TkTkorrovi's Avatar
TkTkorrovi TkTkorrovi is offline Offline
Junior Poster

Re: A simple problem

  #2  
Jul 15th, 2007
This was the most frequently asked question once in dev-c++ forum, and i'm the most sure this topic has already been discussed many times in this forum as well. Maybe the shortest answer, use only fgets for input, this is the only right function for input, as it restricts the number of input characters, and it would always input the newline as well. Then you can easily use getchar to prevent program from ending. But if you still want to use some weird input methods, like cin, then you have to put fflush (stdin); before getchar.
Reply With Quote  
Join Date: Dec 2006
Posts: 1,404
Reputation: Aia is a name known to all Aia is a name known to all Aia is a name known to all Aia is a name known to all Aia is a name known to all Aia is a name known to all 
Rep Power: 10
Solved Threads: 95
Aia's Avatar
Aia Aia is offline Offline
Nearly a Posting Virtuoso

Re: A simple problem

  #3  
Jul 15th, 2007
A common way of pausing the program is to make use of the standard function getchar().

scanf() behaviour will mess up your ability to pause the program with getchar() sometimes. Read about it here.
Last edited by Aia : Jul 15th, 2007 at 6:33 pm.
"No man's life, liberty, or property is safe while the legislature is in session." ~ Mark Twain
Reply With Quote  
Join Date: Dec 2006
Posts: 1,404
Reputation: Aia is a name known to all Aia is a name known to all Aia is a name known to all Aia is a name known to all Aia is a name known to all Aia is a name known to all 
Rep Power: 10
Solved Threads: 95
Aia's Avatar
Aia Aia is offline Offline
Nearly a Posting Virtuoso

Re: A simple problem

  #4  
Jul 15th, 2007
Originally Posted by TkTkorrovi View Post
But if you still want to use some weird input methods, like cin, then you have to put fflush (stdin); before getchar.


Best way is to avoid any `weird input methods' and do not start even
using fflush( stdin ) at all. Here is why not to use it.
"No man's life, liberty, or property is safe while the legislature is in session." ~ Mark Twain
Reply With Quote  
Join Date: Mar 2005
Posts: 154
Reputation: TkTkorrovi is on a distinguished road 
Rep Power: 4
Solved Threads: 11
TkTkorrovi's Avatar
TkTkorrovi TkTkorrovi is offline Offline
Junior Poster

Re: A simple problem

  #5  
Jul 15th, 2007
Yes that's right, effect of fflush is undefined for stdin and it of course must not be used. But if they already do things insecurely, then they must use other uncertain things to compensate it, nothing to do, their choice. Then i say again that these who read would better remember, use only fgets for input. And if you use anything else for input, then delete all rubbish and use only fgets for input.
Reply With Quote  
Join Date: Sep 2004
Posts: 6,303
Reputation: Narue has much to be proud of Narue has much to be proud of Narue has much to be proud of Narue has much to be proud of Narue has much to be proud of Narue has much to be proud of Narue has much to be proud of Narue has much to be proud of Narue has much to be proud of 
Rep Power: 28
Solved Threads: 456
Super Moderator
Narue's Avatar
Narue Narue is offline Offline
Expert Meanie

Re: A simple problem

  #6  
Jul 15th, 2007
>Yes that's right, effect of fflush is undefined for stdin and it of course must not be used.
Then why did you tell the OP to use it?

>But if they already do things insecurely, then they must use other uncertain things to compensate it
There are better ways to discard pending characters than to introduce undefined behavior. The easiest is a simple loop:
void jsw_flush ( void ) 
{ 
  int ch;

  do 
    ch = getchar(); 
  while ( ch != EOF && ch != '\n' ); 

  clearerr ( stdin );
}
If you can't suggest one of the better options, you're better off sticking to the original advice of always using fgets.
I'm a programmer. My attitude starts with arrogance, holds steady at condescension, and ends with hostility. Get used to it.
Reply With Quote  
Join Date: Mar 2005
Posts: 154
Reputation: TkTkorrovi is on a distinguished road 
Rep Power: 4
Solved Threads: 11
TkTkorrovi's Avatar
TkTkorrovi TkTkorrovi is offline Offline
Junior Poster

Re: A simple problem

  #7  
Jul 15th, 2007
Thanks Narue, discarding pending characters is indeed better than fflush (stdin). I never use fflush (stdin), and thus tend to forget what standard says about it. This discarding pending characters until newline or eof is good when using fgets as well, because fgets doesn't do that. fgets isn't perfect input method, but the only way not to do it character by character. And for not doing anything character by character, the only option is to have the number of input characters large enough, so that the user couldn't likely write too many characters by mistake. I usually use FILENAME_MAX as size, this is 256 or so, and a defined constant, so not a magic number. But, when the user still manages to write too many characters before newline, then the next input would consume it, and would most likely cause a wrong input. The standard doesn't guarantee either, that the input stream would be flushed when the program exits, though mostly it will be flushed.
Last edited by TkTkorrovi : Jul 15th, 2007 at 8:46 pm. Reason: none
Reply With Quote  
Join Date: Sep 2004
Posts: 6,303
Reputation: Narue has much to be proud of Narue has much to be proud of Narue has much to be proud of Narue has much to be proud of Narue has much to be proud of Narue has much to be proud of Narue has much to be proud of Narue has much to be proud of Narue has much to be proud of 
Rep Power: 28
Solved Threads: 456
Super Moderator
Narue's Avatar
Narue Narue is offline Offline
Expert Meanie

Re: A simple problem

  #8  
Jul 15th, 2007
>I usually use FILENAME_MAX as size
I prefer BUFSIZ, personally.

>then the next input would consume it, and would most likely cause a wrong input.
That's a quality of implementation issue. You write your code so that it can intelligently handle excessively long lines.

>The standard doesn't guarantee either, that the input
>stream would be flushed when the program exits
I'm not sure where you're going this.
I'm a programmer. My attitude starts with arrogance, holds steady at condescension, and ends with hostility. Get used to it.
Reply With Quote  
Join Date: Mar 2005
Posts: 154
Reputation: TkTkorrovi is on a distinguished road 
Rep Power: 4
Solved Threads: 11
TkTkorrovi's Avatar
TkTkorrovi TkTkorrovi is offline Offline
Junior Poster

Re: A simple problem

  #9  
Jul 15th, 2007
> I prefer BUFSIZ, personally.

Yep, matter of taste, just because i must use FILENAME_MAX for file names, i prefer to use the same constant everywhere.

> That's a quality of implementation issue. You write your code so that it can intelligently handle excessively long lines.

Yes and this can only be done character by character, at least you have to consume pending characters that way. I talked about input which is not done character by character, using only fgets, which is not exactly a quality implementation, but just a somewhat satisfactory option.

> I'm not sure where you're going this.

??
Knowledge is regarded by the fool as ignorance, and the things that are profitable are to him hurtful. He liveth in death. -- Thoth the Atlantean
Reply With Quote  
Join Date: Mar 2005
Posts: 154
Reputation: TkTkorrovi is on a distinguished road 
Rep Power: 4
Solved Threads: 11
TkTkorrovi's Avatar
TkTkorrovi TkTkorrovi is offline Offline
Junior Poster

Re: A simple problem

  #10  
Jul 15th, 2007
I also use FILENAME_MAX because it used to be somewhat smaller that BUFSIZ, in Linux, BUFSIZ used to be some 8000, and FILENAME_MAX some 4000, it's smaller, but quite enough so that it is quite difficult to type in so many characters. FILENAME_MAX is somewhat related to the size of the command line, as it has to allow a long file name which still has to be shorter than the maximum command line, while BUFSIZ is not exactly for such purpose, it's likely bigger in the systems with more resources.
Knowledge is regarded by the fool as ignorance, and the things that are profitable are to him hurtful. He liveth in death. -- Thoth the Atlantean
Reply With Quote  
Reply

Only community members can participate in forum threads. You must register or log in to contribute.

DaniWeb C Marketplace
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)

 

Thread Tools Display Modes

Similar Threads
Other Threads in the C Forum

All times are GMT -4. The time now is 4:28 pm.
Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC