fgets vs scanf 
Author Message
 fgets vs scanf

When I use fgets, there are some bugs because the program "jump" (I
don't the exact English word) some others fgets call.
With scanf, I've no bugs but it's preferrable to use fgets.

Can someone help me, please ?

Thanks.
--



Tue, 21 Jan 2003 03:00:00 GMT  
 fgets vs scanf


Quote:
>When I use fgets, there are some bugs because the program "jump" (I
>don't the exact English word) some others fgets call.
>With scanf, I've no bugs but it's preferrable to use fgets.

>Can someone help me, please ?

Can you boil down your program to the smallest one that demonstrates
your problem.  

Francis Glassborow      Association of C & C++ Users
64 Southfield Rd
Oxford OX4 1PA          +44(0)1865 246490
All opinions are mine and do not represent those of any organisation
--



Tue, 21 Jan 2003 03:00:00 GMT  
 fgets vs scanf

Quote:

> When I use fgets, there are some bugs because the program "jump" (I
> don't the exact English word) some others fgets call.
> With scanf, I've no bugs but it's preferrable to use fgets.

> Can someone help me, please ?

No.  We have no way to know what your problem is without seeing
your code.  Post the important parts so that we can help you.
--
"I consider that the golden rule requires that if I like a program
 I must share it with other people who like it."
--Richard Stallman
--



Tue, 21 Jan 2003 03:00:00 GMT  
 fgets vs scanf


Quote:
>When I use fgets, there are some bugs because the program "jump" (I
>don't the exact English word) some others fgets call.
>With scanf, I've no bugs but it's preferrable to use fgets.

>Can someone help me, please ?

It sounds like you are mixing calls to scanf with calls to fgets, but
without seeing your code that shows the problem it is difficult to be
certain.

The following code shows what I mean: (Note that for clarity, newlines
have not been stripped )

#include <stdio.h>
#include <stdlib.h>

#define INPUT_LINE 50

int main(void)
{
    char init;
    char surname[INPUT_LINE];
    char address[INPUT_LINE];

    printf("Please enter your surname:  ");
    fflush(stdout);
    fgets(surname, INPUT_LINE, stdin);

    printf("Please enter your initial:  ");
    fflush(stdout);
    scanf("%c", &init);

    printf("Please enter your address:  ");
    fflush(stdout);
    fgets(address, INPUT_LINE, stdin);

    printf("\nWelcome %c %s, you live at %s\n", init, surname, address);

    return EXIT_SUCCESS;

Quote:
}

A sample run of this gives:

Please enter your surname:  wightman
Please enter your initial:  r
Please enter your address:
Welcome r wightman
, you live at

Please post the minimal program that shows your problem.

--

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Bob Wightman
--



Wed, 22 Jan 2003 03:00:00 GMT  
 fgets vs scanf

Quote:
> #include <stdio.h>
> #include <stdlib.h>
> #define INPUT_LINE 50
> int main(void)
> {
>     char init;
>     char surname[INPUT_LINE];
>     char address[INPUT_LINE];
>     printf("Please enter your surname:  ");
>     fflush(stdout);
>     fgets(surname, INPUT_LINE, stdin);
>     printf("Please enter your initial:  ");
>     fflush(stdout);
>     scanf("%c", &init);

You type R<enter> no doubt.  The scanf only reads the R and the <enter>
stays on stdin.  Try fgets-ing the initial, then sscanf-ing it into a char.

Quote:
>     printf("Please enter your address:  ");
>     fflush(stdout);
>     fgets(address, INPUT_LINE, stdin);

Now you get the enter, BEFORE the user has a chance to enter data.

Quote:
>     printf("\nWelcome %c %s, you live at %s\n", init, surname, address);
>     return EXIT_SUCCESS;
> }

--

<jholder> do you like FreeBSD?
<hal> I need to get the ISDN line running so that I will tell it to pass over
  me and replace my SuSE box with FreeBSD.
--



Fri, 31 Jan 2003 03:00:00 GMT  
 
 [ 5 post ] 

 Relevant Pages 

1. fgets vs scanf

2. fgets vs scanf

3. sscanf & fgets VS. scanf

4. scanf() vs. fgets()

5. fgets/gets vs. scanf

6. Using getchar() instead of fgets or scanf

7. program ignoring scanf & fgets

8. gets/fgets and scanf/fscanf

9. fgets() or scanf()

10. FGETS not working after using SCANF !

11. Using getchar() instead of fgets or scanf

12. fgets() after a scanf()

 

 
Powered by phpBB® Forum Software