944,161 Members | Top Members by Rank

Ad:
  • C Discussion Thread
  • Unsolved
  • Views: 1663
  • C RSS
Oct 26th, 2009
0

How to make the output screen in C to stand for a long time.

Expand Post »
Hi .
i am using C++ editor to write my C code.
when i press Ctrl+F9 , the output screen comes and goes without stopping for a while.

Please note that i have included "#include<stdio.h>" and i am using "getchar()" function do stop the screen.
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
Ismailworking is offline Offline
1 posts
since Oct 2009
Oct 27th, 2009
0
Re: How to make the output screen in C to stand for a long time.
Hi .
i am using C++ editor to write my C code.
when i press Ctrl+F9 , the output screen comes and goes without stopping for a while.

Please note that i have included "#include<stdio.h>" and i am using "getchar()" function do stop the screen.
If you are using turbo C then use getch() not getchar(). Its in the header file conio.h.
Reputation Points: 121
Solved Threads: 61
Posting Pro in Training
dkalita is offline Offline
402 posts
since Sep 2009
Oct 27th, 2009
0
Re: How to make the output screen in C to stand for a long time.
Click to Expand / Collapse  Quote originally posted by dkalita ...
If you are using turbo C then use getch() not getchar(). Its in the header file conio.h.
Don't listen to dkalite. getch() is not standard, getchar() is.

As for your problem, my crystal ball tells me your problem is on line 75 of your code...
Moderator
Reputation Points: 3281
Solved Threads: 896
Posting Sage
WaltP is offline Offline
7,749 posts
since May 2006
Oct 27th, 2009
0
Re: How to make the output screen in C to stand for a long time.
Click to Expand / Collapse  Quote originally posted by WaltP ...
Don't listen to dkalite. getch() is not standard, getchar() is.
Thanks for the correction but may I know the hazards or any side-effect of using getch() it in TurboC.

Using getchar() we have to press the enter key but using getch() we can just press any key then why getchar() instead of getch().
Last edited by dkalita; Oct 27th, 2009 at 3:52 am.
Reputation Points: 121
Solved Threads: 61
Posting Pro in Training
dkalita is offline Offline
402 posts
since Sep 2009
Oct 27th, 2009
0
Re: How to make the output screen in C to stand for a long time.
Quote ...
when i press Ctrl+F9 , the output screen comes and goes without stopping for a while.

Please note that i have included "#include<stdio.h>" and i am using "getchar()" function do stop the screen.
A previous call to scanf() of getchar() is probably leaving characters in the stream. The getchar() you are using to stop the screen only works if the stream is completely empty. You can fix that by adding a loop that reads and throws away characters until a line break is found. Here is a utility function that can do what you want:
  1. void PauseForUser(int promptUser, int clearStream)
  2. {
  3. if (clearStream)
  4. {
  5. int c;
  6.  
  7. while ((c = getchar()) != '\n' && c != EOF)
  8. {
  9. /* all work in condition */
  10. }
  11.  
  12. clearerr(stdin);
  13. }
  14.  
  15. if (promptUser) fputs("Press [Enter] to continue", stdout);
  16.  
  17. getchar();
  18. }
In your code it might be called as PauseForUser(1, 1); .

Quote ...
may I know the hazards or any side-effect of using getch() it in TurboC.
Turbo C is becoming increasingly less compatible with the rest of the world as it ages, and using getch() is one way to lock your code into a specific compiler. The graphics.h header is another, worse way.

Quote ...
Using getchar() we have to press the enter key but using getch() we can just press any key then why getchar() instead of getch().
IMO that is not a deal breaker for getchar(), and it is not enough of a benefit to add an extra portability concern. Personally, I think this is a trivial thing to argue over, kind of like void main(). It is well known, easy to spot, easy to change, and not likely to cause horrible problems. But because of these things that are well known, easy to spot, and easy to change, they are also easy for coding snobs to act all elitist about.
Reputation Points: 1446
Solved Threads: 135
Practically a Master Poster
Tom Gunn is offline Offline
681 posts
since Jun 2009
Oct 27th, 2009
0
Re: How to make the output screen in C to stand for a long time.
Click to Expand / Collapse  Quote originally posted by Tom Gunn ...
  1. void PauseForUser(int promptUser, int clearStream)
  2. {
  3. if (clearStream)
  4. {
  5. int c;
  6.  
  7. while ((c = getchar()) != '\n' && c != EOF)
  8. {
  9. /* all work in condition */
  10. }
  11.  
  12. clearerr(stdin);
  13. }
  14.  
  15. if (promptUser) fputs("Press [Enter] to continue", stdout);
  16.  
  17. getchar();
  18. }
In your code it might be called as PauseForUser(1, 1); .
May I impose on you by asking, why clearerr(stdin); is necessary in this particular example? What risks I am not seeing when used without it? Thank you.
Aia
Reputation Points: 2224
Solved Threads: 218
Nearly a Posting Maven
Aia is offline Offline
2,304 posts
since Dec 2006
Oct 27th, 2009
0
Re: How to make the output screen in C to stand for a long time.
Quote ...
May I impose on you by asking, why clearerr(stdin); is necessary in this particular example?
It is there for C99 compatibility.

Quote ...
What risks I am not seeing when used without it?
In C99 getchar() will continue to return EOF if the error or end of file indicators are set on the stream. The last getchar() will not block, and that defeats the whole purpose of the function. In C89 I have not found an implementation that has the same behavior has C99, so it will probably work as intended without the call to clearerr(). But there is also no risk of not calling clearerr() in C89. It is better all around to assume the more strict rules of C99 and expect subsequent calls to an input function to fail after EOF or an error. That way your code works now and is future proofed.
Reputation Points: 1446
Solved Threads: 135
Practically a Master Poster
Tom Gunn is offline Offline
681 posts
since Jun 2009
Oct 27th, 2009
0
Re: How to make the output screen in C to stand for a long time.
just remember that
for windows getch and getchar can be used using the header file <conio.h>
In linux for the same you require <stdlib.h>
Reputation Points: 34
Solved Threads: 2
Light Poster
ankur_ is offline Offline
38 posts
since Oct 2009
Oct 27th, 2009
1
Re: How to make the output screen in C to stand for a long time.
Quote ...
for windows getch and getchar can be used using the header file <conio.h>
In linux for the same you require <stdlib.h>
Not quite. getchar() is declared in <stdlib.h>, which is a standard header so every compiler will have it. getch() is typically declared in <conio.h>, but only on some Windows compilers. getch() is not available on Linux except when using the curses library.
Last edited by Tom Gunn; Oct 27th, 2009 at 12:55 pm.
Reputation Points: 1446
Solved Threads: 135
Practically a Master Poster
Tom Gunn is offline Offline
681 posts
since Jun 2009
Oct 27th, 2009
0
Re: How to make the output screen in C to stand for a long time.
Thanks very much for the clarification.
Reputation Points: 34
Solved Threads: 2
Light Poster
ankur_ is offline Offline
38 posts
since Oct 2009

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C Forum Timeline: Structures And Unions
Next Thread in C Forum Timeline: Read Me : Always mark the thread solved when the problem is solved





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC