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

Reply

Join Date: Oct 2009
Posts: 1
Reputation: Ismailworking is an unknown quantity at this point 
Solved Threads: 0
Ismailworking Ismailworking is offline Offline
Newbie Poster

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

 
0
  #1
29 Days Ago
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.
Reply With Quote Quick reply to this message  
Join Date: Sep 2009
Posts: 335
Reputation: dkalita will become famous soon enough dkalita will become famous soon enough 
Solved Threads: 48
dkalita's Avatar
dkalita dkalita is offline Offline
Posting Whiz
 
0
  #2
29 Days Ago
Originally Posted by Ismailworking View 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.
If you are using turbo C then use getch() not getchar(). Its in the header file conio.h.
Reply With Quote Quick reply to this message  
Join Date: May 2006
Posts: 3,114
Reputation: WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of 
Solved Threads: 281
Moderator
WaltP's Avatar
WaltP WaltP is offline Offline
Posting Sensei
 
0
  #3
29 Days Ago
Originally Posted by dkalita View Post
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...
The 3 Laws of the Procrastination Society:
1) Never do today that which can be put off until tomorrow
2) Tomorrow never comes
Reply With Quote Quick reply to this message  
Join Date: Sep 2009
Posts: 335
Reputation: dkalita will become famous soon enough dkalita will become famous soon enough 
Solved Threads: 48
dkalita's Avatar
dkalita dkalita is offline Offline
Posting Whiz
 
0
  #4
29 Days Ago
Originally Posted by WaltP View Post
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; 29 Days Ago at 3:52 am.
Reply With Quote Quick reply to this message  
Join Date: Jun 2009
Posts: 681
Reputation: Tom Gunn has much to be proud of Tom Gunn has much to be proud of Tom Gunn has much to be proud of Tom Gunn has much to be proud of Tom Gunn has much to be proud of Tom Gunn has much to be proud of Tom Gunn has much to be proud of Tom Gunn has much to be proud of Tom Gunn has much to be proud of Tom Gunn has much to be proud of 
Solved Threads: 132
Tom Gunn's Avatar
Tom Gunn Tom Gunn is offline Offline
Practically a Master Poster
 
0
  #5
29 Days Ago
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); .

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.

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.
-Tommy (For Great Justice!) Gunn
Reply With Quote Quick reply to this message  
Join Date: Dec 2006
Posts: 2,030
Reputation: Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of 
Solved Threads: 177
Aia's Avatar
Aia Aia is offline Offline
Postaholic
 
0
  #6
29 Days Ago
Originally Posted by Tom Gunn View Post
  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.
Reply With Quote Quick reply to this message  
Join Date: Jun 2009
Posts: 681
Reputation: Tom Gunn has much to be proud of Tom Gunn has much to be proud of Tom Gunn has much to be proud of Tom Gunn has much to be proud of Tom Gunn has much to be proud of Tom Gunn has much to be proud of Tom Gunn has much to be proud of Tom Gunn has much to be proud of Tom Gunn has much to be proud of Tom Gunn has much to be proud of 
Solved Threads: 132
Tom Gunn's Avatar
Tom Gunn Tom Gunn is offline Offline
Practically a Master Poster
 
0
  #7
29 Days Ago
May I impose on you by asking, why clearerr(stdin); is necessary in this particular example?
It is there for C99 compatibility.

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.
-Tommy (For Great Justice!) Gunn
Reply With Quote Quick reply to this message  
Join Date: Oct 2009
Posts: 27
Reputation: ankur_ is an unknown quantity at this point 
Solved Threads: 2
ankur_ ankur_ is offline Offline
Light Poster
 
0
  #8
29 Days Ago
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>
Reply With Quote Quick reply to this message  
Join Date: Jun 2009
Posts: 681
Reputation: Tom Gunn has much to be proud of Tom Gunn has much to be proud of Tom Gunn has much to be proud of Tom Gunn has much to be proud of Tom Gunn has much to be proud of Tom Gunn has much to be proud of Tom Gunn has much to be proud of Tom Gunn has much to be proud of Tom Gunn has much to be proud of Tom Gunn has much to be proud of 
Solved Threads: 132
Tom Gunn's Avatar
Tom Gunn Tom Gunn is offline Offline
Practically a Master Poster
 
1
  #9
29 Days Ago
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; 29 Days Ago at 12:55 pm.
-Tommy (For Great Justice!) Gunn
Reply With Quote Quick reply to this message  
Join Date: Oct 2009
Posts: 27
Reputation: ankur_ is an unknown quantity at this point 
Solved Threads: 2
ankur_ ankur_ is offline Offline
Light Poster
 
0
  #10
29 Days Ago
Thanks very much for the clarification.
Reply With Quote Quick reply to this message  
Reply

Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC