Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > Press enter to continue

Reply
Thread Tools

Press enter to continue

 
 
junk mail
Guest
Posts: n/a
 
      02-28-2004
My friend is trying to code a small c program where he wants to force the
user to press enter and only enter to continue.

Currently he is using getchar() with a loop but you can type any number of
characters, which are echoed to screen before you have to press enter.

For example:-

do
{ /* Begin loop */
printf("\Press Enter"); /* Output to screen */
} while ((ch = getchar()) != '\n') ; /* While ( Condition -
must press enter ) */


 
Reply With Quote
 
 
 
 
Joona I Palaste
Guest
Posts: n/a
 
      02-28-2004
junk mail <> scribbled the following:
> My friend is trying to code a small c program where he wants to force the
> user to press enter and only enter to continue.


Can't be done in ISO standard C.

> Currently he is using getchar() with a loop but you can type any number of
> characters, which are echoed to screen before you have to press enter.


ISO standard C can only read from stdin and write to stdout. It cannot
control anything about what happens to characters before they come to
stdin, or after they go to stdout.
Doing what your friend wants requires meddling with the characters
before the "read from keyboard" and "put into stdin" phases. This is
outside of C's scope and requires OS-specific code, which we don't
discuss here.

--
/-- Joona Palaste () ------------- Finland --------\
\-- http://www.helsinki.fi/~palaste --------------------- rules! --------/
"No, Maggie, not Aztec, Olmec! Ol-mec!"
- Lisa Simpson
 
Reply With Quote
 
 
 
 
Mike Wahler
Guest
Posts: n/a
 
      02-28-2004

"Joona I Palaste" <> wrote in message
news:c1r2gd$6o$...
> junk mail <> scribbled the following:
> > My friend is trying to code a small c program where he wants to force

the
> > user to press enter and only enter to continue.

>
> Can't be done in ISO standard C.


#include <stdio.h>

int main()
{
int count = 0;

do
{
int c = 0;
count = 0;
printf("Press ENTER to continue...");
fflush(stdout);

while((c = getchar()) != '\n' && c != EOF)
++count;

if(count)
puts("ENTER only, I say!");

} while(count);

puts("Thank you.");
return 0;
}




-Mike


 
Reply With Quote
 
Joona I Palaste
Guest
Posts: n/a
 
      02-29-2004
Mike Wahler <> scribbled the following:
> "Joona I Palaste" <> wrote in message
> news:c1r2gd$6o$...
>> junk mail <> scribbled the following:
>> > My friend is trying to code a small c program where he wants to force

> the
>> > user to press enter and only enter to continue.

>>
>> Can't be done in ISO standard C.


> #include <stdio.h>


> int main()
> {
> int count = 0;


> do
> {
> int c = 0;
> count = 0;
> printf("Press ENTER to continue...");
> fflush(stdout);


> while((c = getchar()) != '\n' && c != EOF)
> ++count;


> if(count)
> puts("ENTER only, I say!");


> } while(count);


> puts("Thank you.");
> return 0;
> }


>


Which doesn't solve the problem of characters getting echoed to the
screen, so the code is useless.

--
/-- Joona Palaste () ------------- Finland --------\
\-- http://www.helsinki.fi/~palaste --------------------- rules! --------/
"Shh! The maestro is decomposing!"
- Gary Larson
 
Reply With Quote
 
Mike Wahler
Guest
Posts: n/a
 
      02-29-2004

"Joona I Palaste" <> wrote in message
news:c1s3np$gah$...
> Mike Wahler <> scribbled the following:
> > "Joona I Palaste" <> wrote in message
> > news:c1r2gd$6o$...
> >> junk mail <> scribbled the following:
> >> > My friend is trying to code a small c program where he wants to force

> > the
> >> > user to press enter and only enter to continue.
> >>
> >> Can't be done in ISO standard C.

>
> > #include <stdio.h>

>
> > int main()
> > {
> > int count = 0;

>
> > do
> > {
> > int c = 0;
> > count = 0;
> > printf("Press ENTER to continue...");
> > fflush(stdout);

>
> > while((c = getchar()) != '\n' && c != EOF)
> > ++count;

>
> > if(count)
> > puts("ENTER only, I say!");

>
> > } while(count);

>
> > puts("Thank you.");
> > return 0;
> > }

>
> >

>
> Which doesn't solve the problem of characters getting echoed to the
> screen, so the code is useless.


I wasn't sure if inhibiting 'echo' was a strict requirement.
The code above does eliminate the problem of the 'extra'
characters getting submitted to subsequent inputs, which
is indeed a common complaint. But yes, you're right, no
way to inhibit the echo with standard code.

-Mike


 
Reply With Quote
 
Dan Pop
Guest
Posts: n/a
 
      03-01-2004
In <sU70c.929$GQ.883@newsfe1-win> "junk mail" <> writes:

>My friend is trying to code a small c program where he wants to force the
>user to press enter and only enter to continue.


He's lucky, because Enter/Return is the only key for which a standard
solution exists.

>Currently he is using getchar() with a loop but you can type any number of
>characters, which are echoed to screen before you have to press enter.


So what? The program is still guaranteed to wait until Enter is pressed.
There is no way to prevent the user from pressing anything he wants
(short of giving him a keyboard with a single key), but the program can
still wait until the Enter key is pressed and discard everything, the
Enter key included.

>For example:-
>
>do
> { /* Begin loop */
> printf("\Press Enter"); /* Output to screen */
> } while ((ch = getchar()) != '\n') ; /* While ( Condition -
>must press enter ) */


In such cases you may also want to detect the user typing the eof key,
and handle this case separately. Otherwise, you may end up waiting for
an Enter key that will *never* come.

int rc = scanf("%*[^\n]");
if (rc < 0) /* the eof key was pressed */ ;
else getchar(); /* remove the newline character from stdin */

Dan
--
Dan Pop
DESY Zeuthen, RZ group
Email:
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
capturing from text area Shft+Enter, Control+Enter, Alt+Enter and browser issue. HopfZ Javascript 0 08-28-2006 10:11 AM
Message "Press any key to continue" Timur Ametov C++ 8 04-27-2006 05:11 PM
Re: Color ink out. Replace color ink cartridge. Press Enter to continue. Bill Williams Digital Photography 7 12-31-2004 05:15 PM
Re: Color ink out. Replace color ink cartridge. Press Enter to continue. Donna Michaelson Digital Photography 1 12-25-2004 09:40 AM
Newbie C programming question - "Press any key to continue" Andrew Robert C Programming 51 11-13-2003 10:34 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57