0

I have made the following C program to print the roll and name of a person after taking input from the user.

#include<stdio.h>
#include<conio.h>
main()
{
    int roll;
    char name[50];
    printf("\nEnter roll: ");
    scanf("%d",&roll);
    printf("\nEnter name: ");
    gets(name);
    printf("\n%d",roll);
    puts(name);
}

But the problem is, when I run the program, the program terminates without taking the 'name' input. Why is this happening?

melpomene
  • 84,125
  • 8
  • 85
  • 148
user3114849
  • 101
  • 1
  • 7
  • Have you tried debugging it? – Lasse V. Karlsen Oct 28 '17 at 20:43
  • 5
    1. Don't use `gets`; use `fgets()` instead. 2. Don't mix scanf with other input methods. Or better, yet don't use scanf at all. – P.P Oct 28 '17 at 20:45
  • 4
    `gets()` was removed from C11 for a good reason. – myaut Oct 28 '17 at 20:45
  • [never use gets()](https://www.quora.com/Why-is-the-gets-function-risky-to-use) – user3629249 Oct 29 '17 at 00:00
  • why posted code not reading the second input: 1) this statement: `scanf("%d",&roll);` leaves the trailing newline '\n' in the input stream. then this statement: `gets(name);` which stops inputting when it sees a newline, does not read anything into the array `name[]` – user3629249 Oct 29 '17 at 00:03

0 Answers0