Trouble with scanf() (newbie here) 
Author Message
 Trouble with scanf() (newbie here)

First of all I know this code is probably sloppy as hell...but I'm new
to this so if you would be so kind as to excuse that I'd be grateful
(though any input whatsoever would be greatly appreciated).
Anyway...the problem I'm having is with scanf(). It seems that when I
use more than one scanf() in any code every other one is ignored. Case
in point:

/* This prog illustrates the modulus operator. */
/* Inputs seconds and converts to hours minutes and seconds */

#include <stdio.h>

/* Define constants */

#define SECS_P_MIN 60
#define SECS_P_HR 3600

unsigned seconds, minutes, hours, SecsLeft, MinsLeft;

char check = 'y';

int main()
{
        /* Create loop so that program may be repeated without
restarting */
        while (check == 'y')
        {
                    /* Input number of seconds and calculate
conversion */

                printf("\nEnter the number of seconds ( < 65000): ");
                scanf("%d", &seconds);

           hours = seconds / SECS_P_HR;
                minutes = seconds / SECS_P_MIN;
                MinsLeft = minutes % SECS_P_MIN;
                SecsLeft = seconds % SECS_P_MIN;

                printf("%u seconds is equal to ", seconds);
                printf("%u h, %u m, and %u s\n\n", hours, MinsLeft,
SecsLeft);

                /* Ask to retry or not...repeat if neither y nor n is
typed */
           do
                {
                        printf("Do you want to try another one? (y/n)
");
                        scanf("%c", &check);
                        scanf("%c", &check);
                }
                while ( (check != 'y') && (check != 'n' ) );
        }

        return 0;

Quote:
}

When it gets to the do-while loop it repeats the program (because
check is still 'y')  unless I add another scanf() just below the first
one. Is it a problem with my structure? Or maybe my compiler?
Anyway...thanks in advance.


Mon, 13 Nov 2000 03:00:00 GMT  
 Trouble with scanf() (newbie here)

:First of all I know this code is probably sloppy as hell...but I'm new
:to this so if you would be so kind as to excuse that I'd be grateful
:(though any input whatsoever would be greatly appreciated).
:Anyway...the problem I'm having is with scanf(). It seems that when I
:use more than one scanf() in any code every other one is ignored. Case
:in point:
=============
Since scanf() is an vile function, here is a replacement for your code.
You will need to add your own error checking.
#include <stdio.h>
#include <ctype.h>

int main()
{
    unsigned long seconds, minutes, hours;
    char done = 0;
    char buffer[BUFSIZ];
    while (!done) {
        printf("\nEnter the number of seconds ( < 429467295): ");
        fflush(stdout);
        fgets(buffer, sizeof buffer, stdin);
        sscanf(buffer, "%lu", &seconds);
        minutes = hours = 0;
        while (seconds > 59) {
            seconds -= 60;
            minutes++;
        }
        while (minutes > 59) {
            minutes -= 60;
            hours++;
        }
        printf(" = %lu:%02lu:%02lu\n", hours, minutes, seconds);
        printf("Do you want to try another one? (y/n) ");
        fgets(buffer, sizeof buffer, stdin);
        done = tolower(buffer[0]);
        if (done == 'y')
            done = 0;
        else if (done != 'n')
            printf("I'm pretending you meant 'n'\n");
    }
    return 0;

Quote:
}

=============
:
:/* This prog illustrates the modulus operator. */
:/* Inputs seconds and converts to hours minutes and seconds */
:
:#include <stdio.h>
:
:/* Define constants */
:
:#define SECS_P_MIN 60
:#define SECS_P_HR 3600
:
:unsigned seconds, minutes, hours, SecsLeft, MinsLeft;
:
:char check = 'y';
:
:int main()
:{
:        /* Create loop so that program may be repeated without
:restarting */
:        while (check == 'y')
:        {
:                    /* Input number of seconds and calculate
:conversion */
:
:                printf("\nEnter the number of seconds ( < 65000): ");
:                scanf("%d", &seconds);
:
:    hours = seconds / SECS_P_HR;
:                minutes = seconds / SECS_P_MIN;
:                MinsLeft = minutes % SECS_P_MIN;
:                SecsLeft = seconds % SECS_P_MIN;
:
:                printf("%u seconds is equal to ", seconds);
:                printf("%u h, %u m, and %u s\n\n", hours, MinsLeft,
:SecsLeft);
:
:                /* Ask to retry or not...repeat if neither y nor n is
:typed */
:    do
:                {
:                        printf("Do you want to try another one? (y/n)
:");
:                        scanf("%c", &check);
:                        scanf("%c", &check);
:                }
:                while ( (check != 'y') && (check != 'n' ) );
:        }
:
:        return 0;
:}
:
:
:
:
:When it gets to the do-while loop it repeats the program (because
:check is still 'y')  unless I add another scanf() just below the first
:one. Is it a problem with my structure? Or maybe my compiler?
:Anyway...thanks in advance.


Mon, 13 Nov 2000 03:00:00 GMT  
 Trouble with scanf() (newbie here)

: First of all I know this code is probably sloppy as hell...but I'm new
: to this so if you would be so kind as to excuse that I'd be grateful
: (though any input whatsoever would be greatly appreciated).
: Anyway...the problem I'm having is with scanf(). It seems that when I
: use more than one scanf() in any code every other one is ignored. Case
: in point:

: /* This prog illustrates the modulus operator. */
: /* Inputs seconds and converts to hours minutes and seconds */

: #include <stdio.h>

: /* Define constants */

: #define SECS_P_MIN 60
: #define SECS_P_HR 3600

: unsigned seconds, minutes, hours, SecsLeft, MinsLeft;

: char check = 'y';

: int main()
: {
:         /* Create loop so that program may be repeated without
: restarting */
:         while (check == 'y')
:         {
:                     /* Input number of seconds and calculate
: conversion */

:                 printf("\nEnter the number of seconds ( < 65000): ");
/* ### add fflush(stdout); here or terminate the printf() string with
   a newline.
 */
:                 scanf("%d", &seconds);

:          hours = seconds / SECS_P_HR;
:                 minutes = seconds / SECS_P_MIN;
:                 MinsLeft = minutes % SECS_P_MIN;
:                 SecsLeft = seconds % SECS_P_MIN;

:                 printf("%u seconds is equal to ", seconds);
:                 printf("%u h, %u m, and %u s\n\n", hours, MinsLeft,
: SecsLeft);

:                 /* Ask to retry or not...repeat if neither y nor n is
: typed */
:          do
:                 {
:                         printf("Do you want to try another one? (y/n)
: ");
:                         scanf("%c", &check);
:                         scanf("%c", &check);

/* ### you're hitting scanf's problems with whitespace: try
   scanf(" %c", &check);
   getc() is a more reliable method of clearing a trailing newline.
   There are ways of reading a newline with
   scanf(), but they seem to be unreliable under MSDOS.
   And check the return value from scanf().  You'd be better
   off avoiding scanf() for user input; it's too fragile.  Use
   fgets(), then sscanf() or strtok().
 */
:                 }
:                 while ( (check != 'y') && (check != 'n' ) );
:         }

:         return 0;
: }

: When it gets to the do-while loop it repeats the program (because
: check is still 'y')  unless I add another scanf() just below the first
: one. Is it a problem with my structure? Or maybe my compiler?
: Anyway...thanks in advance.

No, just a problem with scanf().

Will



Mon, 13 Nov 2000 03:00:00 GMT  
 Trouble with scanf() (newbie here)

<snip>

Quote:
> Anyway...the problem I'm having is with scanf(). It seems that when I
> use more than one scanf() in any code every other one is ignored. Case
> in point:
> #include <stdio.h>
> #define SECS_P_MIN 60
> #define SECS_P_HR 3600
> unsigned seconds, minutes, hours, SecsLeft, MinsLeft;
> char check = 'y';
> int main()
> {
>     while (check == 'y')
>     {
>         printf("\nEnter the number of seconds ( < 65000): ");

This scanf() is leaving the '\n' on the stdin stream.  You may want
to try using fgets() and atoi().  Include stdlib.h to use atoi(), and
string.h to manipulate the string you get from fgets(). See the faq.

Quote:
>         scanf("%d", &seconds);
>         hours = seconds / SECS_P_HR;
>         minutes = seconds / SECS_P_MIN;
>         MinsLeft = minutes % SECS_P_MIN;
>         SecsLeft = seconds % SECS_P_MIN;

Either put a '\n' at the end of this printf() or use an fflush(stdout)
after it.

Quote:
>         printf("%u seconds is equal to ", seconds);
>         printf("%u h, %u m, and %u s\n\n", hours,
>               MinsLeft, SecsLeft);
>         do
>         {
>             printf("Do you want to try another one? (y/n)");

This scanf is now satisfied with the '\n' that was left on stdin from
the above scanf.  So check is no longer 'y', its now '\n'.

Quote:
>             scanf("%c", &check);

Finally your getting your character.

Quote:
>             scanf("%c", &check);
>         } while ( (check != 'y') && (check != 'n' ) );
>     }
>     return 0;
> }
> When it gets to the do-while loop it repeats the program (because
> check is still 'y')  unless I add another scanf() just below the first

Its not because check is still 'y', its because its not still 'y'
your first scanf() is leaving the '\n' on stdin, your second scanf() is
accepting that '\n' making the do/while loop run twice.  After you
added the third scanf(), you were able to get the character you wanted
without the loop running twice.

Quote:
> one. Is it a problem with my structure? Or maybe my compiler?
> Anyway...thanks in advance.

Read the faq for some better ways to get your input.  Stay away from
scanf() and gets().
ftp://rtfm.mit.edu/pub/usenet-by-group/comp.lang.c/C-FAQ-list

Nick



Mon, 13 Nov 2000 03:00:00 GMT  
 Trouble with scanf() (newbie here)

Quote:

>    If u do want to use scanf(),
> use it with fflush(stdin)

No; while your thinking is right here, it's a little off.

'fflush(stdin)' is used with the 'get_demons(nose)' function, not 'scanf().'

--
(initiator of the campaign for grumpiness where grumpiness is due in c.l.c)

Attempting to write in a hybrid which can be compiled by either a C compiler
or a C++ compiler produces a compromise language which combines the drawbacks
of both with the advantages of neither.



Thu, 16 Nov 2000 03:00:00 GMT  
 Trouble with scanf() (newbie here)

Hi,
        If u do want to use scanf(),
use it with fflush(stdin)

regards,
Shiva

Quote:

> First of all I know this code is probably sloppy as hell...but I'm new
> to this so if you would be so kind as to excuse that I'd be grateful
> (though any input whatsoever would be greatly appreciated).
> Anyway...the problem I'm having is with scanf(). It seems that when I
> use more than one scanf() in any code every other one is ignored. Case
> in point:

> /* This prog illustrates the modulus operator. */
> /* Inputs seconds and converts to hours minutes and seconds */

> #include <stdio.h>

> /* Define constants */

> #define SECS_P_MIN 60
> #define SECS_P_HR 3600

> unsigned seconds, minutes, hours, SecsLeft, MinsLeft;

> char check = 'y';

> int main()
> {
>         /* Create loop so that program may be repeated without
> restarting */
>         while (check == 'y')
>         {
>                     /* Input number of seconds and calculate
> conversion */

>                 printf("\nEnter the number of seconds ( < 65000): ");
>                 scanf("%d", &seconds);

>            hours = seconds / SECS_P_HR;
>                 minutes = seconds / SECS_P_MIN;
>                 MinsLeft = minutes % SECS_P_MIN;
>                 SecsLeft = seconds % SECS_P_MIN;

>                 printf("%u seconds is equal to ", seconds);
>                 printf("%u h, %u m, and %u s\n\n", hours, MinsLeft,
> SecsLeft);

>                 /* Ask to retry or not...repeat if neither y nor n is
> typed */
>            do
>                 {
>                         printf("Do you want to try another one? (y/n)
> ");
>                         scanf("%c", &check);
>                         scanf("%c", &check);
>                 }
>                 while ( (check != 'y') && (check != 'n' ) );
>         }

>         return 0;
> }

> When it gets to the do-while loop it repeats the program (because
> check is still 'y')  unless I add another scanf() just below the first
> one. Is it a problem with my structure? Or maybe my compiler?
> Anyway...thanks in advance.



Fri, 17 Nov 2000 03:00:00 GMT  
 Trouble with scanf() (newbie here)

On Mon, 01 Jun 1998 10:29:56 -0700, Shiva

Quote:


>> First of all I know this code is probably sloppy as hell...but I'm new
>> to this so if you would be so kind as to excuse that I'd be grateful
>> (though any input whatsoever would be greatly appreciated).
>> Anyway...the problem I'm having is with scanf(). It seems that when I
>> use more than one scanf() in any code every other one is ignored. Case
>> in point:

[snip]

Quote:
>    If u do want to use scanf(), use it with fflush(stdin)

Ugh.

The correct answer is to visit the C-faq Web page.

  http://www.eskimo.com/~scs/C-faq/top.html

In particular, see the answers to questions 12.20 and 12.26:

12.20:  Why does everyone say not to use scanf()?  What should I use
        instead?

12.26:  How can I flush pending input so that a user's typeahead isn't
        read at the next prompt?  Will fflush(stdin) work?

The Corps is Mother.  The Corps is Father.  Read the C-faq!

--

http://www.cs.wustl.edu/~jxh/        Washington University in Saint Louis

Quote:
>>>>>>>>>>>>> I use *SpamBeGone* <URL:http://www.internz.com/SpamBeGone/>



Fri, 17 Nov 2000 03:00:00 GMT  
 Trouble with scanf() (newbie here)

:Hi,
: If u do want to use scanf(),
:use it with fflush(stdin)
===========
OTOH, you could try writing in legal C.
That would mean ignoring Shiva.
Reading the FAQ (from ftp://rtfm.mit.edu/pub/usenet-by-group)
wouldn't hurt, either.
============
:
:regards,
:Shiva
:
:
:>
:> First of all I know this code is probably sloppy as hell...but I'm new
:> to this so if you would be so kind as to excuse that I'd be grateful
:> (though any input whatsoever would be greatly appreciated).
:> Anyway...the problem I'm having is with scanf(). It seems that when I
:> use more than one scanf() in any code every other one is ignored. Case
:> in point:
:>
:> /* This prog illustrates the modulus operator. */
:> /* Inputs seconds and converts to hours minutes and seconds */
:>
:> #include <stdio.h>
:>
:> /* Define constants */
:>
:> #define SECS_P_MIN 60
:> #define SECS_P_HR 3600
:>
:> unsigned seconds, minutes, hours, SecsLeft, MinsLeft;
:>
:> char check = 'y';
:>
:> int main()
:> {
:>         /* Create loop so that program may be repeated without
:> restarting */
:>         while (check == 'y')
:>         {
:>                     /* Input number of seconds and calculate
:> conversion */
:>
:>                 printf("\nEnter the number of seconds ( < 65000): ");
:>                 scanf("%d", &seconds);
:>
:>            hours = seconds / SECS_P_HR;
:>                 minutes = seconds / SECS_P_MIN;
:>                 MinsLeft = minutes % SECS_P_MIN;
:>                 SecsLeft = seconds % SECS_P_MIN;
:>
:>                 printf("%u seconds is equal to ", seconds);
:>                 printf("%u h, %u m, and %u s\n\n", hours, MinsLeft,
:> SecsLeft);
:>
:>                 /* Ask to retry or not...repeat if neither y nor n is
:> typed */
:>            do
:>                 {
:>                         printf("Do you want to try another one? (y/n)
:> ");
:>                         scanf("%c", &check);
:>                         scanf("%c", &check);
:>                 }
:>                 while ( (check != 'y') && (check != 'n' ) );
:>         }
:>
:>         return 0;
:> }
:>
:> When it gets to the do-while loop it repeats the program (because
:> check is still 'y')  unless I add another scanf() just below the first
:> one. Is it a problem with my structure? Or maybe my compiler?
:> Anyway...thanks in advance.



Fri, 17 Nov 2000 03:00:00 GMT  
 Trouble with scanf() (newbie here)

/********************************************************************************
**  Here is your posting, only slightly modified:
**  Hopefully, this will be helpful,
**
**  Good Luck
**    A
**  B.C.Weber (dr_mysterio at hotamil dot com dot letstopspam)
**    E
**
*/

/* This prog illustrates the modulus operator. */
/* Inputs seconds and converts to hours minutes and seconds */

#include <stdio.h>
#include <ctype.h> /*+ Added for the 'tolower' function +*/

/* Define constants */
#define NoResponse 'n'-'y'
#define YesResponse 'y'-'y'
#define NewLineResponse '\n'-'y'

#define SECS_P_MIN 60
#define SECS_P_HR 3600

unsigned seconds, minutes, hours, SecsLeft, MinsLeft;

char done = 0;
int Scanned;

int main()
{
   /* Create loop so that program may be repeated without restarting */
   while (!done)
   {
      /* Input number of seconds and calculate conversion */
      printf("\nEnter the number of seconds ( < 65000): ");
      Scanned = scanf("%d", &seconds);
          if (Scanned < 1)
                return -1;

      hours = seconds / SECS_P_HR;
      minutes = seconds / SECS_P_MIN;
      MinsLeft = minutes % SECS_P_MIN;
      SecsLeft = seconds % SECS_P_MIN;

      printf("%u seconds is equal to ", seconds);
      printf("%u h, %u m, and %u s\n\n", hours, MinsLeft, SecsLeft);

      /* Ask to retry or not...repeat if neither y nor n is typed */
      /*+ Put the prompt outside of the scanf loop, also instead of
       ** do...while, use initial_statements followed by a while
      +*/
      printf("Do you want to try another one? (y/n) ");
      Scanned = scanf("%c", &done);
      /*+ Insert after this line +*/
      if (Scanned < 1)
         return -1;
      done = (char) (tolower( (int) done) - 'y');
            /*+ Convert to lower case +*/

      while ( (done != YesResponse) && (done != NoResponse) )
      {
         Scanned = scanf("%c", &done);
         /*+ Insert after this line +*/
         if (Scanned < 1)
            return -1;
         done = (char) (tolower( (int) done) - 'y');
         if( (done != YesResponse) &&
             (done != NoResponse) &&
             (done != NewLineResponse) )
            printf("\a"); /*+ Sound beep if not a y, n, or \n +*/
      }
   }
   return 0;

Quote:
}

/********************************************************************
** Other notes:
** To get a better understanding of scanf try adding the following
** lines to the places in the above source marked
** /*+ Insert after this line +*/
/*
** printf("\t%d \'%c\' / %2X \n", Scanned, done, done);
**
** Also try entering lines such as the following:
** 1234 y 2345 y 3456 n 4567
** 5432    561y     6248xy    1234y n
** and observe the results
**
*/


Fri, 17 Nov 2000 03:00:00 GMT  
 
 [ 9 post ] 

 Relevant Pages 

1. Newbie: Trouble with scanf - HELP!!!

2. newbie trouble: CoCreateInstance Error + IWebBrowser trouble...

3. scanf troubles

4. trouble bulltproofing scanf

5. printf/scanf trouble on SPARCstation

6. Troubles using C's scanf Function

7. trouble with scanf?

8. Scanf troubles

9. Trouble with scanf( "...%[^...]", ...)

10. SCANF() NEWBIE

11. Newbie with scanf question

12. Newbie problem w/ scanf

 

 
Powered by phpBB® Forum Software