UNIX Socket FAQ

A forum for questions and answers about network programming on Linux and all other Unix-like systems

You are not logged in.

  • Index
  • » C
  • » scanf problems

#1 2009-03-17 03:59 PM

Ronin
Member
From: San Antonio, TX
Registered: 2009-03-09
Posts: 21

Re: scanf problems

do{
     NewFile = TRUE; // sets flag to allow for new file to be written
     Answer = 'n';
     do{
          status = 0;
          printf("\nEnter diameter of ice drop in mm: ");
          status = scanf("%lf", &DropDiameter);
          fflush( stdin ); // clears out 'newline' still in the input stream
          if(status != 1){
               printf("\nInput must be a decimal value. Try again.\n");
               continue;
          }else{
               fprintf(stdout,"%.2lf: Is this correct?[y/n] ", DropDiameter);
               Answer = getAnswer();
          }
     }while(Answer == 'n' || Answer == 'N');

     FileOut = CheckFile(FileName); // go and get file name and see if its unique

     #if defined LITTLE_BUG
          printf("in Main: %s\n", FileOut);
     #endif

     DropRadius = DropDiameter / 2; // convert drop diameter to radius
     printf("\nWorking...\n");

     ErrorNo = Calculations(DropRadius, FileOut);

     DisplayErrorMsg(ErrorNo);
     printf("\nAnother calculation?[y/n] ");
     Answer = getAnswer();
}while(Answer == 'y' || Answer == 'Y');

"Science without religion is lame. Religion without science is blind."
-Albert Einstein

Offline

#2 2009-03-17 07:52 PM

RobSeace
Administrator
From: Boston, MA
Registered: 2002-06-12
Posts: 3,839
Website

Re: scanf problems

The "e" is likely an attempt to parse the value as scientific notation...  Eg: "5.1e2" is the
same as "510"...  (It's saying "5.1 x 10^2"...)

The "i" is likely being parsed as "inf", the standard floating point representation of
infinity...  And, "n" is likely parsed as "nan", the bogus "not a number" value...  But,
I wouldn't expect it to do so unless you really have the full "inf" and "nan" strings in
the input... *shrug*

On a side note:

fflush( stdin ); // clears out 'newline' still in the input stream

That's not doing what you think it's doing...  In fact, flushing stdin does absolutely
nothing of value at all...  The newline is still going to be read the next time you
scanf(), but it'll just be ignored, since scanf() skips over all leading whitespace...

Offline

#3 2009-03-17 08:19 PM

Ronin
Member
From: San Antonio, TX
Registered: 2009-03-09
Posts: 21

Re: scanf problems

fflush( stdin ); // clears out 'newline' still in the input stream

"Science without religion is lame. Religion without science is blind."
-Albert Einstein

Offline

#4 2009-03-17 10:47 PM

RobSeace
Administrator
From: Boston, MA
Registered: 2002-06-12
Posts: 3,839
Website

Re: scanf problems

Offline

#5 2009-03-18 10:50 AM

Nope
Administrator
From: Germany
Registered: 2004-01-24
Posts: 385
Website

Re: scanf problems

In all my time as c coder, I've never used scanf to directly read user input. I always just read in text lines and then I checked the input with sth like isalpha before using for example atof() for the final conversion. I clearly recall a statement of my prof some 20 or so years ago where he threatened a coleague of mine to be thrown out of the course if he ever again uses scanf for anything but reading in of clearly defined files.

So, while scanf seems to be a viable solution at first glance, it does not provide any meaningful protection against user errors.

Offline

#6 2009-03-18 01:05 PM

BdyJ9wcC
Guest

Re: scanf problems

#7 2009-03-18 02:16 PM

Ronin
Member
From: San Antonio, TX
Registered: 2009-03-09
Posts: 21

Re: scanf problems

char getAnswer(void){

   char Answer;

        scanf(" %c", &Answer);
        while((Answer != 'y') && (Answer != 'Y') && (Answer != 'n') && (Answer != 'N')){
             fprintf(stderr,"\nWrong response. Try again.[y/n]");
             scanf(" %c", &Answer);
        }
        return (Answer);
}

"Science without religion is lame. Religion without science is blind."
-Albert Einstein

Offline

#8 2009-03-18 03:15 PM

RobSeace
Administrator
From: Boston, MA
Registered: 2002-06-12
Posts: 3,839
Website

Re: scanf problems

Offline

#9 2009-03-18 03:46 PM

Ronin
Member
From: San Antonio, TX
Registered: 2009-03-09
Posts: 21

Re: scanf problems

Thanks again for all your help with everything.


"Science without religion is lame. Religion without science is blind."
-Albert Einstein

Offline

#10 2009-03-18 07:18 PM

Ronin
Member
From: San Antonio, TX
Registered: 2009-03-09
Posts: 21

Re: scanf problems


"Science without religion is lame. Religion without science is blind."
-Albert Einstein

Offline

#11 2009-03-18 07:58 PM

jfriesne
Administrator
From: California
Registered: 2005-07-06
Posts: 348
Website

Re: scanf problems

Offline

#12 2009-03-18 09:12 PM

RobSeace
Administrator
From: Boston, MA
Registered: 2002-06-12
Posts: 3,839
Website

Re: scanf problems

Yeah, eg. here's a simple Y/N function and double input function:

int get_yn ()
{
    char buf[128];
    char *p;
    int ret = -1;

    while (fgets (buf, sizeof (buf), stdin)) {
        for (p = buf; *p && isspace (*p); p++) ;
        if (toupper (*p) == 'Y') {
            ret = 1; break;
        } else if (toupper (*p) == 'N') {
            ret = 0; break;
        } else {
            fprintf (stderr, "Bogus value entered.  Enter Y or N!\n");
        }
    }

    return (ret);
}

double get_dbl ()
{
    char buf[128];
    char *p;
    double d, ret = NAN;

    while (fgets (buf, sizeof (buf), stdin)) {
        d = strtod (buf, &p);
        if (p == buf) {
            fprintf (stderr, "Bogus value entered.  Enter a numeric value!\n");
        } else if (d == HUGE_VAL) {
            fprintf (stderr, "Overflow!  Enter a sane number!\n");
        } else if (d == -HUGE_VAL) {
            fprintf (stderr, "Underflow!  Enter a sane number!\n");
        } else if (d == NAN) {
            fprintf (stderr, "Not a number!  Enter a real number!\n");
        } else {
            ret = d; break;
        }
    }

    return (ret);
}

You can be more anal by also checking for trailing junk after the correct value, as
well, if desired...  These just ignore any trailing junk...

Offline

#13 2009-03-19 02:33 PM

Ronin
Member
From: San Antonio, TX
Registered: 2009-03-09
Posts: 21

Re: scanf problems

for (p = buf; *p && isspace (*p); p++) ;

"Science without religion is lame. Religion without science is blind."
-Albert Einstein

Offline

#14 2009-03-19 02:50 PM

i3839
Oddministrator
From: Amsterdam
Registered: 2003-06-07
Posts: 2,239

Re: scanf problems

It skips all spaces at the start of the string.

Offline

#15 2009-03-20 09:30 AM

Nope
Administrator
From: Germany
Registered: 2004-01-24
Posts: 385
Website

Re: scanf problems

Actually I didn't use fgets either. Normally I use identical code to read from file and console. So read(...) or fread(...) from stdin instead of fgets. I started that when fgets had some problems on a platform I had to work with.

Offline

#16 2009-03-20 02:17 PM

Ronin
Member
From: San Antonio, TX
Registered: 2009-03-09
Posts: 21

Re: scanf problems

this might seem like a dumb question, but is NAN different from machine to machine or is it different from compiler to compiler or neither? the reason i ask is it tried out RobSeace code out from his double getdbl() function above at work, when i enter a number it defaults to the elseif statement that it isn't a number. When I did it it home, it worked the way it should.

I use eclipse at home and wedit at work.


"Science without religion is lame. Religion without science is blind."
-Albert Einstein

Offline

#17 2009-03-20 05:47 PM

jfriesne
Administrator
From: California
Registered: 2005-07-06
Posts: 348
Website

Re: scanf problems

Offline

#18 2009-03-20 07:12 PM

Nope
Administrator
From: Germany
Registered: 2004-01-24
Posts: 385
Website

Re: scanf problems

Offline

#19 2009-03-20 07:22 PM

Ronin
Member
From: San Antonio, TX
Registered: 2009-03-09
Posts: 21

Re: scanf problems


"Science without religion is lame. Religion without science is blind."
-Albert Einstein

Offline

#20 2009-03-20 07:53 PM

Nope
Administrator
From: Germany
Registered: 2004-01-24
Posts: 385
Website

Re: scanf problems

Offline

#21 2009-03-20 07:58 PM

Nope
Administrator
From: Germany
Registered: 2004-01-24
Posts: 385
Website

Re: scanf problems

Offline

#22 2009-03-20 08:03 PM

Ronin
Member
From: San Antonio, TX
Registered: 2009-03-09
Posts: 21

Re: scanf problems


"Science without religion is lame. Religion without science is blind."
-Albert Einstein

Offline

#23 2009-03-20 08:42 PM

RobSeace
Administrator
From: Boston, MA
Registered: 2002-06-12
Posts: 3,839
Website

Re: scanf problems

Offline

#24 2009-03-20 08:52 PM

RobSeace
Administrator
From: Boston, MA
Registered: 2002-06-12
Posts: 3,839
Website

Re: scanf problems

Offline

#25 2009-03-20 09:13 PM

Ronin
Member
From: San Antonio, TX
Registered: 2009-03-09
Posts: 21

Re: scanf problems

double getNumber ( void ){

   char buf[128];
   char *p;
   double d, ret = NAN; 
      while (fgets (buf, sizeof (buf), stdin)) {
      d = strtod (buf, &p); 
      if (p == buf) {
         fprintf (stderr, "I can't do anything with that.  Enter a numeric value!\n");
      } else if (d == HUGE_VAL) {
         fprintf (stderr, "Overflow!  Enter a sane number!\n");
      } else if (d == LOW_VAL) {
         fprintf (stderr, "Underflow!  Value has to be positive!\n");
[color=red]      } else if (isnan(d)) { [/color] 
         fprintf (stderr, "That's not even a number!  Enter a real number!\n");
      } else {
         ret = d; break;
      }
   }
   return (ret);
}

"Science without religion is lame. Religion without science is blind."
-Albert Einstein

Offline

  • Index
  • » C
  • » scanf problems

Board footer

Powered by FluxBB