Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > Re: scanf dilemma for beginner

Reply
Thread Tools

Re: scanf dilemma for beginner

 
 
Greg P.
Guest
Posts: n/a
 
      08-29-2003
I modified your code a little bit. You needed some buffer flushes to sync
your I/O with the OS. I also changed your if statements into a switch:
------------------------
#include <stdio.h>

int main(void)
{

int num = 0;
int ch = 0;
int i;

printf("Enter decimal number (or zero for list): ");
fflush(stdout);
fflush(stdin);
scanf("%d",&num);

if (num == 0)
{
for (i = 1; i <= 100; ++i)
printf("\nDecimal: %3d\tOctal: %3o\tHex: %3x", i , i, i);
return(1);
}

printf("\nWould you like (h)ex or (o)ctal? ");
fflush(stdout);
fflush(stdin);
ch = getchar();

switch(ch)
{
case 'h':
case 'H':
printf("\n\n%d in Hexadecimal is: %x", num, num);
break;

case 'o':
case 'O':
printf("\n\n%d in Octal is: %o", num, num);
break;

default:
printf("\nSorry, but there was some sort of error.
Cheers!\n\n");
break;
}
return 0;
}


 
Reply With Quote
 
 
 
 
Mac
Guest
Posts: n/a
 
      08-29-2003
On Fri, 29 Aug 2003 01:32:23 +0000, Greg P. wrote:

> I modified your code a little bit. You needed some buffer flushes to sync
> your I/O with the OS. I also changed your if statements into a switch:
> ------------------------
> #include <stdio.h>
>
> int main(void)
> {
>
> int num = 0;
> int ch = 0;
> int i;
>
> printf("Enter decimal number (or zero for list): ");
> fflush(stdout);
> fflush(stdin);


Don't flush input streams. This is UB.

[snip]

Mac
--
 
Reply With Quote
 
 
 
 
Greg P.
Guest
Posts: n/a
 
      08-29-2003
"Mac" <(E-Mail Removed)> wrote in message
news(E-Mail Removed)...
| Don't flush input streams. This is UB.

I thought the same thing before I typed it, but without them the program
does not work properly (in this case).


 
Reply With Quote
 
Jirka Klaue
Guest
Posts: n/a
 
      08-29-2003
Greg P. wrote:

> "Mac" <(E-Mail Removed)> wrote in message
> news(E-Mail Removed)...
> | Don't flush input streams. This is UB.
>
> I thought the same thing before I typed it, but without them the program
> does not work properly (in this case).


No, it doesn't. The reason is that scanf leaves the '\n' in the input stream.
It can be removed with a simple getchar() or, if you want to be sure with

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

Jirka

 
Reply With Quote
 
Jirka Klaue
Guest
Posts: n/a
 
      08-29-2003
Greg P. wrote:
> "Jirka Klaue" <(E-Mail Removed)-berlin.de> wrote:
> | No, it doesn't. The reason is that scanf leaves the '\n' in the input
> stream.
> | It can be removed with a simple getchar() or, if you want to be sure with
> |
> | while ((c = getchar()) != '\n' && c != EOF)
> | ;
>
> I was thinking: since flushing input is obviously not a good idea, would a
> scanf() function suffice:
>
> scanf("\n%c", &char);

^^^^
This is not a good idea.

> I mean, do all implementations (or at least most) leave the newline
> character for the next input routine? If that's so could we then just add
> '\n' to the scanf format string as above?


scanf(" %c", &c); discards leading whitespace.

But ...
.... what if the user entered 42xxx before?

Jirka

 
Reply With Quote
 
Greg P.
Guest
Posts: n/a
 
      08-29-2003
"Jirka Klaue" <(E-Mail Removed)-berlin.de> wrote in message
news:bin1ui$gbr$(E-Mail Removed)-Berlin.DE...
| But ...
| ... what if the user entered 42xxx before?

Ah! Very perseverant. I did not think of that <g>


 
Reply With Quote
 
Arthur J. O'Dwyer
Guest
Posts: n/a
 
      08-29-2003

On Fri, 29 Aug 2003, Greg P. wrote:
>
> "Jirka Klaue" <(E-Mail Removed)-berlin.de> wrote...
> |
> | No, it doesn't. The reason is that scanf leaves the '\n' in the input
> | stream. It can be removed with a simple getchar() or, if you want to
> | be sure, with
> |
> | while ((c = getchar()) != '\n' && c != EOF)
> | ;
>
> I was thinking: since flushing input is obviously not a good idea, would
> a scanf() function suffice:
>
> scanf("\n%c", &char);


The scanf() format corresponding to Jirka's algorithm is

scanf("%*[^\n]\n");

This is definitely the way to do it, if you're looking for a one-line
approach and don't mind the obscurity.

> I mean, do all implementations (or at least most) leave the newline
> character for the next input routine?


Yes, of course. All conforming implementations, that is.

> If that's so could we then just add
> '\n' to the scanf format string as above?


You could, but what if the user *didn't* enter '\n'? Then the scanf()
fails, and you're still left with garbage in the input buffer.

-Arthur
 
Reply With Quote
 
Kevin Easton
Guest
Posts: n/a
 
      08-30-2003
Arthur J. O'Dwyer <(E-Mail Removed)> wrote:
>
> On Fri, 29 Aug 2003, Greg P. wrote:
>>
>> "Jirka Klaue" <(E-Mail Removed)-berlin.de> wrote...
>> |
>> | No, it doesn't. The reason is that scanf leaves the '\n' in the input
>> | stream. It can be removed with a simple getchar() or, if you want to
>> | be sure, with
>> |
>> | while ((c = getchar()) != '\n' && c != EOF)
>> | ;
>>
>> I was thinking: since flushing input is obviously not a good idea, would
>> a scanf() function suffice:
>>
>> scanf("\n%c", &char);

>
> The scanf() format corresponding to Jirka's algorithm is
>
> scanf("%*[^\n]\n");
>
> This is definitely the way to do it, if you're looking for a one-line
> approach and don't mind the obscurity.


I don't think that works - that last \n is treated the same as any other
whitespace (skip any amount of whitespace) - you want %*c there, to read
and discard the single newline character.

- Kevin.

 
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
difference between scanf("%i") and scanf("%d") ??? perhaps bug inVS2005? =?ISO-8859-1?Q?Martin_J=F8rgensen?= C Programming 18 05-02-2006 10:53 AM
scanf (yes/no) - doesn't work + deprecation errors scanf, fopen etc. =?ISO-8859-1?Q?Martin_J=F8rgensen?= C Programming 185 04-03-2006 02:49 PM
Nikon RAW Beginner's Question / Dilemma Xtx99 Digital Photography 13 01-13-2005 02:25 PM
Re: scanf dilemma for beginner chris C Programming 11 09-01-2003 09:36 PM
Re: scanf dilemma for beginner Ian Roddis C Programming 0 08-29-2003 01:28 AM



Advertisments