0

I am working on my c project,just started.I need to get "Name" from users input.

fgets() works fine inside the main function,but skipped when used outside the main function.I found many related questions,so i read all the answers but i cannot solve my problem.

After reading some answer,I tried using "fflush(stdin)", scanf("[%^\n]") but no reslove.

void newacc()
    {       
        struct tenant new;
        printf("Enter Name:");
        fgets(new.name,20,stdin);
        printf("\nEnter Phone number:\n");
        scanf("%d",&new.ph);
        printf("Enter Rental Date (DD-MM-YYYY):\n");
        scanf("%d%d%d",&new.td.dd,&new.td.mm,&new.td.yy);
    }
azajit
  • 11
  • 1
  • 4
    Probably a stray newline left in the input buffer after previous input? Hard to say without a [mcve]. – Some programmer dude Aug 26 '19 at 16:26
  • 2
    Never mix `scanf` and `fgets`. If you must use `scanf`, use `scanf` to read *all* input -- don't try to mix in `fgets` and `getc`/`getchar`. If you try to mix them, you will have newline and other whitespace problems until the end of your days. (And if you do stick to `scanf`, you will still have to be careful of `%c` and `%[...]`, because they handle whitespace differently from the rest of `scanf`'s input directives.) – Steve Summit Aug 26 '19 at 16:27
  • 1
    Also note that the C specification explicitly mention that calling `fflush` on an input-only stream (like `stdin`) as *undefined behavior*. Some systems add it as a non-portable extension, never use it. – Some programmer dude Aug 26 '19 at 16:28
  • How do you know it's "getting skipped"? What do you even mean by that? Check the return status of all calls to `fgets` and `scanf`. Until you know what they're doing, you really cannot expect to resolve the issue. – William Pursell Aug 26 '19 at 16:40
  • 1
    Do you really intend new.td.mm to be a negative value? – William Pursell Aug 26 '19 at 16:45
  • Aside: store a phone number as a **string**, not an integer. Even if `int` is large enough to store, say `01234987654` you can't recover the leading `0`. – Weather Vane Aug 26 '19 at 17:09
  • @user3121023 hey i use scanf(" %[^\n]",new.name) with space,it works.Thank you. – azajit Aug 26 '19 at 17:43
  • @WilliamPursell i was guessing it skipped because program didn't stop for any input and proceed to print next line. (DD-MM-YYYY) is just format for user to enter,not negative values!!. – azajit Aug 26 '19 at 17:49
  • If you pass the format string `"%d%d%d"` to scanf and the user enters data in the form `DD-MM-YYYY`, then `new.td.mm` and `new.td.yy` will be negative values. Which I expect is not your intention. Fix the format string. – William Pursell Aug 27 '19 at 02:54

2 Answers2

1

I'm not familiar with scanf, but you're 2nd scanf ( "%d%d%d" ) seems error-prone. And how do you know that something is skipped ?

Here is a version that expect a space between DD, MM and YYYY ( "%d %d %d" ) so that each number are clearly separated. There are other issue with the way you collect your inputs, but I don't see any skip.

#include <stdio.h>

void newacc()
{
    char    name[20];
    int     phone;
    int     day;
    int     month;
    int     year;

    printf("Enter Name: ");
    fgets(name,20,stdin);
    printf("Enter Phone number: ");
    scanf("%d",&phone);
    printf("Enter Rental Date (DD MM YYYY):\n");
    scanf("%d %d %d",&day, &month, &year);
    printf("%s %d %02d-%02d-%04d\n", name, phone, day, month, year);
}

int main(void)
{
    newacc();
    return (0);
}
AugustinLopez
  • 348
  • 1
  • 3
  • 13
-2

Try this

#include <stdio.h>

void newacc()
{ 
    struct tenant per;
    printf("Enter Name:");
    gets_s(per.name,20);
    printf("\nEnter Phone number:\n");
    scanf("%d",&per.ph);
    printf("Enter Rental Date (DD-MM-YYYY):\n");
    scanf("%d%d%d",&per.td.dd,&per.td.mm,&per.td.yy);
}

since new is a keyword for C++, here its a bad name

and mostly please try not to use scanf and fgets at the same time

  • 1
    'new' is keyword for c++ not c which is what the OP is tagged as - bad name in any case. –  Aug 26 '19 at 16:36
  • `gets` has been deprecated for an insanely long time. It should only be used when reproducing text from prior to about 1985, and even then must be accompanied by a warning that it should never be used in new code. – William Pursell Aug 26 '19 at 16:41
  • @Andy you are correct, really a very bad name in this case – Hrishav Dhawaj Purkayastha Aug 26 '19 at 16:41
  • @WilliamPursell yes gets() is depricated but I think gets_s() can be used It totally depends which version of compiler is used in this case If its the latest one then we have to use `#include ` instead of `#include` – Hrishav Dhawaj Purkayastha Aug 26 '19 at 16:46
  • `gets` is not merely deprecated; it has been removed from the language entirely! – William Pursell Aug 26 '19 at 16:47
  • But `gets_s()` can be used instead of `gets()` as it is now removed – Hrishav Dhawaj Purkayastha Aug 26 '19 at 16:48
  • 1
    @HrishavDhawajPurkayastha You used `gets_s` incorrectly. It needs to be passed a `size` argument. It's also not present in many implementations of the standard C library, do use `fgets` instead. – S.S. Anne Aug 26 '19 at 16:55
  • Anyway, it does not solve the problem. The `scanf` will still leave a newline in the buffer which will be read by the next `gets_s` as an empty string. – Weather Vane Aug 26 '19 at 17:07