char input 
Author Message
 char input

Hi all, I have a newbie question that bugged me for the whole
afternoon.


scanf("%c", &symbol);

the %c specifies the scanf function that will read a character to
symbol, decleared in the header. However, the program, compiled by VC
6.0, simplied ends. ("Press any key to continue.") ANyone have an idea
how to do this?

thanks,

WILLIAM YUAN



Sun, 27 Jun 2004 02:27:33 GMT  
 char input

Quote:

> Hi all, I have a newbie question that bugged me for the whole
> afternoon.


> scanf("%c", &symbol);

> the %c specifies the scanf function that will read a character to
> symbol, decleared in the header. However, the program, compiled by VC
> 6.0, simplied ends. ("Press any key to continue.") ANyone have an idea
> how to do this?

Your problem is that you're using scanf. Probably a previous call to
it left some characters in the input stream (like a newline). Then
this call reads that newline and goes on. To avoid this kind of problem
use fgets, fgetc etc. This is detailed in the FAQ.
Tobias.


Sun, 27 Jun 2004 02:50:28 GMT  
 char input


Wed, 18 Jun 1902 08:00:00 GMT  
 char input
On Tue, 08 Jan 2002 13:50:28 -0500, Tobias Oed

Quote:


>> Hi all, I have a newbie question that bugged me for the whole
>> afternoon.


>> scanf("%c", &symbol);

>> the %c specifies the scanf function that will read a character to
>> symbol, decleared in the header. However, the program, compiled by VC
>> 6.0, simplied ends. ("Press any key to continue.") ANyone have an idea
>> how to do this?

>Your problem is that you're using scanf. Probably a previous call to
>it left some characters in the input stream (like a newline). Then
>this call reads that newline and goes on. To avoid this kind of problem
>use fgets, fgetc etc. This is detailed in the FAQ.
>Tobias.

thanks Tobias, that is exactly the problem when I use the printf
function to verify it. Only do I see a blank line. Could you please
tell me how can I flush the input stream, thanks.


Sun, 27 Jun 2004 02:53:35 GMT  
 char input

Quote:

> On Tue, 08 Jan 2002 13:50:28 -0500, Tobias Oed


> >> Hi all, I have a newbie question that bugged me for the whole
> >> afternoon.


> >> scanf("%c", &symbol);

> >> the %c specifies the scanf function that will read a character to
> >> symbol, decleared in the header. However, the program, compiled by VC
> >> 6.0, simplied ends. ("Press any key to continue.") ANyone have an idea
> >> how to do this?

> >Your problem is that you're using scanf. Probably a previous call to
> >it left some characters in the input stream (like a newline). Then
> >this call reads that newline and goes on. To avoid this kind of problem
> >use fgets, fgetc etc. This is detailed in the FAQ.
> >Tobias.

> thanks Tobias, that is exactly the problem when I use the printf
> function to verify it. Only do I see a blank line. Could you please
> tell me how can I flush the input stream, thanks.

You can't flush the input stream although some people may tell
you it's perfectly fine to do a fflush(stdin). This is bad and
fails on a bunch of system. What you can do is discard all
characters till the next end of line:

int c;

while((c=fgetc(stdin))!=EOF && c!='\n')
   ;

HTH, read the FAQ at http://www.eskimo.com/~scs/C-faq/top.html follow
the advice in there and forget that scanf even exists. It'll save you
from a lot of trouble.
Tobias.



Sun, 27 Jun 2004 03:21:43 GMT  
 char input

Quote:

<snip>

> thanks Tobias, that is exactly the problem when I use the printf
> function to verify it. Only do I see a blank line. Could you please
> tell me how can I flush the input stream, thanks.

You can't really flush an input stream. After all, what would it mean?

But here is a function which will read and discard characters from an
input stream for you until it encounters a newline; having read it, it
will terminate. It returns 0 normally, but 1 if EOF was encountered.

#include <stdio.h>

int discard_instream_line(FILE *fp)
{
  int eof_found = 0;
  int ch = 0;

  while((ch = getchar()) != '\n' && ch != EOF)
  {
    continue;
  }

  if(ch == EOF)
  {
    eof_found = 1;
  }

  return eof_found;

Quote:
}

--

"Usenet is a strange place." - Dennis M Ritchie, 29 July 1999.
C FAQ: http://www.eskimo.com/~scs/C-faq/top.html
K&R answers, C books, etc: http://users.powernet.co.uk/eton


Sun, 27 Jun 2004 03:24:43 GMT  
 char input


Quote:

> <snip>

>> thanks Tobias, that is exactly the problem when I use the printf
>> function to verify it. Only do I see a blank line. Could you please
>> tell me how can I flush the input stream, thanks.

> You can't really flush an input stream. After all, what would it mean?

> But here is a function which will read and discard characters from an
> input stream for you until it encounters a newline; having read it, it
> will terminate. It returns 0 normally, but 1 if EOF was encountered.

> #include <stdio.h>

> int discard_instream_line(FILE *fp)
> {
>   int eof_found = 0;
>   int ch = 0;

>   while((ch = getchar()) != '\n' && ch != EOF)
>   {
>     continue;
>   }

>   if(ch == EOF)
>   {
>     eof_found = 1;
>   }

>   return eof_found;
> }

I like this but, if I may...

#include <stdio.h>

int discardInstreamLine(void)
{
    int inChar;

    for (inChar = 0; inChar != '\n' && inChar != EOF; inChar = getchar());

    return EOF == inChar;

Quote:
}

Is mine really so awful?

--
- Mark A. Odell
- Embedded Firmware Design, Inc.
- http://www.embeddedfw.com



Sun, 27 Jun 2004 03:31:32 GMT  
 char input

Quote:


> > int discard_instream_line(FILE *fp)
> > {
> >   int eof_found = 0;
> >   int ch = 0;

> >   while((ch = getchar()) != '\n' && ch != EOF)
> >   {
> >     continue;
> >   }

> >   if(ch == EOF)
> >   {
> >     eof_found = 1;
> >   }

> >   return eof_found;
> > }

> int discardInstreamLine(void)
> {
>     int inChar;

>     for (inChar = 0; inChar != '\n' && inChar != EOF; inChar = getchar());

>     return EOF == inChar;
> }

While we're at it:

int
drain_stream (FILE *stream)
{
    for (;;) {
        int c = getc (stream);
        if (c == EOF)
            return 1;
        if (c == '\n')
            return 0;
    }

- Show quoted text -

Quote:
}



Sun, 27 Jun 2004 03:38:03 GMT  
 char input

Quote:




> > <snip>

> >> thanks Tobias, that is exactly the problem when I use the printf
> >> function to verify it. Only do I see a blank line. Could you please
> >> tell me how can I flush the input stream, thanks.

> > You can't really flush an input stream. After all, what would it mean?

> > But here is a function which will read and discard characters from an
> > input stream for you until it encounters a newline; having read it, it
> > will terminate. It returns 0 normally, but 1 if EOF was encountered.

> > #include <stdio.h>

> > int discard_instream_line(FILE *fp)
> > {
> >   int eof_found = 0;
> >   int ch = 0;

> >   while((ch = getchar()) != '\n' && ch != EOF)
> >   {
> >     continue;
> >   }

> >   if(ch == EOF)
> >   {
> >     eof_found = 1;
> >   }

> >   return eof_found;
> > }

> I like this but, if I may...

> #include <stdio.h>

> int discardInstreamLine(void)
> {
>     int inChar;

>     for (inChar = 0; inChar != '\n' && inChar != EOF; inChar = getchar());

>     return EOF == inChar;
> }

> Is mine really so awful?

How about mine ?

#include <stdio.h>
int discardInstreamLine(void)
{
        int ch;

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

        return ch == EOF;

Quote:
}

(Copy pasted Richard's and removed the usless - buf self
documenting - stuff).
Tobias.


Sun, 27 Jun 2004 03:57:17 GMT  
 char input


Quote:



>> > int discard_instream_line(FILE *fp)
>> > {
>> >   int eof_found = 0;
>> >   int ch = 0;

>> >   while((ch = getchar()) != '\n' && ch != EOF)
>> >   {
>> >     continue;
>> >   }

>> >   if(ch == EOF)
>> >   {
>> >     eof_found = 1; }

>> >   return eof_found; }

>> int discardInstreamLine(void)
>> {
>>     int inChar;

>>     for (inChar = 0; inChar != '\n' && inChar != EOF; inChar =
getchar());

>>     return EOF == inChar; }

> While we're at it:

> int
> drain_stream (FILE *stream)
> {
>     for (;;) {
>         int c = getc (stream);
>         if (c == EOF)
>             return 1;
>         if (c == '\n')
>             return 0;
>     }
> }

Oh the humanity! You're going to upset Richard. My code was written without
a "forever" loop just to appease his ideals. Why can't you conform? :-)

--
- Mark A. Odell
- Embedded Firmware Design, Inc.
- http://www.embeddedfw.com



Sun, 27 Jun 2004 03:52:29 GMT  
 char input


Quote:




>> > <snip>

>> >> thanks Tobias, that is exactly the problem when I use the printf
>> >> function to verify it. Only do I see a blank line. Could you please
>> >> tell me how can I flush the input stream, thanks.

>> > You can't really flush an input stream. After all, what would it mean?

>> > But here is a function which will read and discard characters from an
>> > input stream for you until it encounters a newline; having read it, it
>> > will terminate. It returns 0 normally, but 1 if EOF was encountered.

>> > #include <stdio.h>

>> > int discard_instream_line(FILE *fp)
>> > {
>> >   int eof_found = 0;
>> >   int ch = 0;

>> >   while((ch = getchar()) != '\n' && ch != EOF)
>> >   {
>> >     continue;
>> >   }

>> >   if(ch == EOF)
>> >   {
>> >     eof_found = 1; }

>> >   return eof_found; }

>> I like this but, if I may...

>> #include <stdio.h>

>> int discardInstreamLine(void)
>> {
>>     int inChar;

>>     for (inChar = 0; inChar != '\n' && inChar != EOF; inChar =
>>     getchar());

>>     return EOF == inChar; }

>> Is mine really so awful?

> How about mine ?

> #include <stdio.h>
> int discardInstreamLine(void)
> {
>      int ch;

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

>      return EOF == ch;
> }

With my inline "adjustments", we have a deal.

--
- Mark A. Odell
- Embedded Firmware Design, Inc.
- http://www.embeddedfw.com



Sun, 27 Jun 2004 03:54:34 GMT  
 char input

Quote:

> But here is a function which will read and discard characters
> from an input stream for you until it encounters a newline;

>   while((ch = getchar()) != '\n' && ch != EOF)
>   {
>     continue;
>   }

For some reason I have the feeling that I've seen this strange
continue statement in your source more often lately. Sorry for
asking this, but what can possibly be the purpose of adding
these three meaningless lines to the code? I really can't
figure out any reason why you would prefer this over the plain:

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

Please elaborate ... did I miss something in the discussions
in c.l.c lately?

willem



Sun, 27 Jun 2004 04:04:00 GMT  
 char input

Quote:



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

<snip>

Quote:
>         int c = getc (stream);

                  ^^^^^^^^^^^^^

Yes, that was what mine was meant to do, dammit.

--

"Usenet is a strange place." - Dennis M Ritchie, 29 July 1999.
C FAQ: http://www.eskimo.com/~scs/C-faq/top.html
K&R answers, C books, etc: http://users.powernet.co.uk/eton



Sun, 27 Jun 2004 05:00:16 GMT  
 char input

Quote:



>> > >   while((ch = getchar()) != '\n' && ch != EOF)
> <snip>
>>         int c = getc (stream);
>                   ^^^^^^^^^^^^^
> Yes, that was what mine was meant to do, dammit.

Aren't you invading implementation namespace with stream? Or does that
just concern file-scope names and not function parameter names?

--

| Kingpriest of "The Flying Lemon Tree" G++ FR FW+ M- #108 D+ ADA N+++|
| http://www.helsinki.fi/~palaste       W++ B OP+                     |
\----------------------------------------- Finland rules! ------------/
"Hasta la Vista, Abie!"
   - Bart Simpson



Sun, 27 Jun 2004 04:56:08 GMT  
 char input

Quote:


> > But here is a function which will read and discard characters
> > from an input stream for you until it encounters a newline;

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

Should have been getc(stream), of course...

Quote:
> >   {
> >     continue;
> >   }

> For some reason I have the feeling that I've seen this strange
> continue statement in your source more often lately.

It's a habit I've cultivated over the last four or five years, I guess.

Quote:
> Sorry for
> asking this, but what can possibly be the purpose of adding
> these three meaningless lines to the code?

If you think of them as meaningless, then <shrug> so be it. I think of
it this way:

(1) I always use braces in if/while/dowhile/for contexts, even if the
body is a single statement. IMHO this is defensive coding which protects
against maintenance mistakes.

(2) I consider the 'continue' to be a cheap n' cheerful way to document
the fact that the loop body is intended do be empty.

Quote:
> I really can't
> figure out any reason why you would prefer this over the plain:

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

> Please elaborate ... did I miss something in the discussions
> in c.l.c lately?

No, I don't think so. I have a rather large collection of idiosyncratic
coding habits which, IMHO, form a coherent, robust, readable,
maintainable style. Naturally, YMMV.

--

"Usenet is a strange place." - Dennis M Ritchie, 29 July 1999.
C FAQ: http://www.eskimo.com/~scs/C-faq/top.html
K&R answers, C books, etc: http://users.powernet.co.uk/eton



Sun, 27 Jun 2004 05:05:54 GMT  
 
 [ 22 post ]  Go to page: [1] [2]

 Relevant Pages 

1. char input problems

2. Single char input in Unix C

3. How to remove the char inputted?

4. How to get a char input in CWnd?

5. VAX C function for char input

6. CComboBox capture char input

7. Custom Control Beep when char input

8. Howto pass array of char as input for method

9. please help a student in the input of chars in Visual c++

10. Newbie question: multi-char string input

11. char* file input

12. char* file input

 

 
Powered by phpBB® Forum Software