0
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
struct user
{
    char name[30];
    char accNo[20];
    char phoneNo[15];
    char password[20];
};

int main()
{

    int choice;
    struct user Deepak;

    printf("select your choice\n\n");
    printf("1. Open new account.\n\n");
    printf("2. Login to account.\n\n");

    printf("Your choice : ");
    scanf("%d", &choice);
    

    if (choice == 1)
    {
        system("cls");
        printf("Enter your name : ");
        fgets(Deepak.name, sizeof(Deepak.name),stdin);
        printf("Enter your phone number : ");
        scanf("%s",Deepak.phoneNo);
        printf("Create your password : ");
        scanf("%s",Deepak.password);
    }

    return 0;
}

Here in line 31 the fgets is not taking inputs and directly jumping to the line 33. I tried scanf("%[^\n]%*c",input) but it did not work either. So I used the fgets but it is also not working. How can I fix it?

Deepak
  • 720
  • 1
  • 3
  • 12
  • 1
    How do you remove the `'\n'` left in `stdin` by `scanf()` before you call `fgets()`?? Perhaps `for (int c = getchar(); c != '\n' && c != EOF; c = getchar()) {}`? (after `scanf()` and before `fgets()`...) You also need to check the return of `scanf()`, e.g. `if (scanf ("%d", &choice) != 1) { /* handle error */ }` – David C. Rankin Apr 30 '22 at 06:09
  • 1
    Does this answer your question? https://stackoverflow.com/questions/5918079/fgets-doesnt-work-after-scanf – Yunnosch Apr 30 '22 at 06:11

0 Answers0