1

In my program if I use scanf(), the program works fine. But as it is obvious, scanf() stops reading the input if it encounters a whitespace. So, instead I used gets(). But the problem is I have to use gets(name[i]); twice. If I use it only once, it skips the 'Subject Name' input & jumps to 'Grade' input.

Can you tell me where the problem lies? Here is the code, I removed almost all irrelevant parts.

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

void selection1();

int main()
{
    int op;

    printf("\n\t\t\t\t\t\t\t\tCHOOSE A NUMBER FROM 0 to 4: ");
    scanf("%d", &op);
    switch(op)
    {
    case 0:
        exit(0);
        break;
    case 1:
        selection1();
        break;
    case 2:
        //selection2();
        break;
    case 3:
        //selection3();
        break;
    case 4:
        //selection4();
        break;
    default:
        printf("\t\t\t\t\t\t\t\tInvalid Selection!!!\n");
        break;
    }
    return 0;
}

void selection1()
{
    char name[20][50], grade[20][5];
    double credit[20][2];
    int i=0;

    for(;;)
    {
        printf("\t\t\t\t\t\t\t\tEnter Subject Name: ");
        gets(name[i]);
        //gets(name[i]);
        printf("\t\t\t\t\t\t\t\tEnter Grade: ");
        scanf("%s", &grade[i]);
        printf("\t\t\t\t\t\t\t\tEnter The Course Credits: ");
        scanf("%lf", &credit[i]);
    }
}
  • 6
    Run, not walk, from any learning resource that says to use `gets()`. – Shawn Feb 13 '22 at 17:53
  • 4
    Use `fgets()` instead of `gets()` here's [why?](https://stackoverflow.com/q/1694036/17939455) – Darth-CodeX Feb 13 '22 at 18:29
  • 3
    `gets()` is a pathway to many abilities some consider to be unnatural. – Darth-CodeX Feb 13 '22 at 18:30
  • 4
    The first thing you scan is a number, which doesn't consume the following newline. The first `gets` just reads the rest of the line with the number. Don't mix line-based input with `fgets` or `gets` and formatted input with `scanf`. Read lines first, then scan these lines with `sscanf`. – M Oehm Feb 13 '22 at 18:30
  • https://stackoverflow.com/questions/5240789/scanf-leaves-the-newline-character-in-the-buffer – pm100 Feb 13 '22 at 18:31
  • 2
    You can find answers to your questions in the comments and duplicate question from [this question from two days ago](https://stackoverflow.com/questions/71081124). – Steve Summit Feb 13 '22 at 19:31
  • @Darth-CodeX: It's keyboard-interactive. Use `getline()`. – Joshua Feb 13 '22 at 19:49

1 Answers1

1

So, I found using scanf(" %[^\n]%*c", &name[i]); instead of gets(name[i]); solve the problem. Thanks to everyone who commented. Your comments helped me to reach this answer :)

  • 1
    To avoid a potential buffer overflow, consider using `scanf(" %49[^\n]%*[^\n]", &name[i])` and note that contrary to `gets()` or `fgets()`, this use of `scanf()` will not allow for an empty name. Also test the return values of `scanf()` to detect invalid input and premature end of file. – chqrlie Feb 14 '22 at 07:35
  • @chqrlie, Tnx I will look into that. – Nureka Rodrigo Feb 14 '22 at 07:52