-1
#include<stdio.h>
int main()
{
   int choice;
   char sl[10];
   char phn[10];
   printf("Enter choice: ");
   scanf("%d",&choice);
   switch(choice)
   {
   case 1:
       printf("\nEnter Student Details: \n\n");
       printf("\nEnter serial number: ");
       gets(sl);
       printf("Roll number: ");
       gets(phn);
   default:
      break;
   }
   return 0;
}

C PROGRAMMING:

I used 2 gets() function here. second one is working but first one is not working. why?? how to make it work?

N.B: I want to take "sl" and "phn" variable as char type and I want to use gets() to take input. please someone help....

melpomene
  • 84,125
  • 8
  • 85
  • 148
jake
  • 19
  • 1
  • 6
  • 4
    [DO NOT use `gets()`, it is dangerous](http://stackoverflow.com/q/1694036/2173917). use [`fgets()`](https://linux.die.net/man/3/fgets) instead. – Sourav Ghosh Apr 02 '17 at 17:15
  • 2
    Think a little: What is the key you use to end the input for the number you input for `scanf`? Hint: The `Enter` key will also be put into the input buffer as a newline character. – Some programmer dude Apr 02 '17 at 17:16
  • 1
    `gets` is dangerous and deprecated (and removed from C11 standard). Use `fgets` or perhaps [getline](http://man7.org/linux/man-pages/man3/getline.3.html) – Basile Starynkevitch Apr 02 '17 at 17:16

2 Answers2

2

Use fgets for all input. scanf usually leaves a newline in the input stream that will cause problems for fgets. Parse the input for an integer with sscanf and check the return to make sure an integer was input.

#include  <stdio.h>
int main()
{
    int choice;
    int result = 0;
    char sl[10];
    char phn[10];
    char input[256];
    do {
        printf("Enter choice: ");
        if ( ( fgets ( input, sizeof input, stdin))) {
            result = sscanf( input, "%d", &choice);
            if ( result == 0) {
                printf ( "try again\n");
            }
        }
        else {
            fprintf ( stderr, "problem getting input\n");
            return 1;
        }
    } while ( result != 1);
    switch(choice)
    {
        case 1:
            printf("\nEnter Student Details: \n\n");
            printf("\nEnter serial number: ");
            if ( ( fgets ( sl, sizeof sl, stdin)) == NULL) {
                fprintf ( stderr, "problem getting serial number\n");
                return 1;
            }
            printf("Roll number: ");
            if ( ( fgets ( phn, sizeof phn, stdin)) == NULL) {
                fprintf ( stderr, "problem getting roll number\n");
                return 1;
            }
            break;
        default:
            break;
    }
    return 0;
}
xing
  • 2,125
  • 2
  • 14
  • 10
0

Never use gets(). It offers no protections against a buffer overflow vulnerability (that is, you cannot tell it how big the buffer you pass to it is, so it cannot prevent a user from entering a line larger than the buffer and clobbering memory).

fgets() is safe because you can guarantee you never overflow the input string buffer by passing in the buffer size (which includes room for the NULL.)

The most important difference is that fgets() knows how big the string is and gets() does not. That means it is nearly impossible to write safe code using gets() so don't do it.

in there use fgets() instead of gets()

Sachith Wickramaarachchi
  • 5,546
  • 6
  • 39
  • 68