Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How to halt in program

Status
Not open for further replies.

amarg

Programmer
Nov 19, 2002
106
IN
Hi,

Before execuation, i want to display the text

Press enter to continue.....

and wait for the user to press enter key.
After that i want to execute my rest of the program.

I am working on ANSIC. Please guide me how only Enter is
enough as a input.

Regards,
Amar
 
#include <stdio.h>

void main(void){
char c;
printf(&quot;Press enter to continue...\n&quot;);
do {
c=scanf(&quot;%c&quot;,&c);
}while (c!=&quot;\n&quot;);
clrscr();
}

hope it'll help you.
hisham.
 
> void main(void){
Nope, main returns an int

> c=scanf(&quot;%c&quot;,&c);
scanf() returns the number of conversions, not the char entered.

> }while (c!=&quot;\n&quot;);
That's a string on the right hand side.

To answer the question

Code:
int ch;
while ( (ch=getchar()) != EOF && ch != '\n' ); /* do nothing */


--
 
Hi,

Thanks Salem. Your solution is working,
But before while i have to use fflush(stdin).

Regards,
Amar
 
> But before while i have to use fflush(stdin).
Nope, that's not right either, use the while loop I posted earlier.

> I am working on ANSIC
In ANSI-C, fflush() is defined only for
- output streams
- update streams where the last operation was a write
Since stdin in input only, you can never call fflush(stdin) and have it do anything meaningful in every ANSI-C implementation. More specifically, it is undefined, which translates to being &quot;anything could happen&quot;.

Now some old PC compilers seemed to think that fflush(stdin) was a good idea, but you'll generally be in for a shock when all your implementation specific tricks stop working one day.

--
 
Hi,

I have one switch statement having lot of cases. For the
default case i am giving this halt. Now without using the
fflush(stdin), process is not wating for the enter key.

I don't have any explation about that but what u said is
perfect. I am not writing any program. I just want to check
that halt is working.
If you want i can send me the file. It's a same.


Regards,
Amar
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top