0
#include <stdio.h>
#include <stdlib.h>
struct subject
{
    int sub_code;
    char sub_name[20];
};
struct student
{
    char name[50];
    int id;
    struct subject etec;
};
void main()
{
    struct student s[3] = { NULL };
    int count;
    for (count = 0; count < 3; count++)
    {
        printf("****************STUDENT-%i*******************\n", (count + 1));
        printf("Enter name of student - ");
        gets_s(s[count].name);
        printf("Enter student id - ");
        scanf_s("%i", &s[count].id);
        printf("Enter subject code: ");
        scanf_s("%i", &s[count].etec.sub_code);
        printf("Enter subject name: ");
        gets_s(s[count].etec.sub_name); //ask for name of course
        system("cls");
    }
    for (count = 0; count < 3; count++)
    {
        printf("****************STUDENT-%i*******************\n", (count + 1));
        printf("Enter name of student - %s\n", s[count].name);
        printf("Enter student id - %i\n", s[count].id);
        printf("Enter subject code: %i\n", s[count].etec.sub_code);
        printf("Enter subject name: %s\n", s[count].etec.sub_name);
        puts("");
    }
    system("pause");
}

The following program when executed does not ask input for gets_s(s[count].etec.sub_name); Please tell how to correct this situation so that code asks me for all inputs. Also tell which one is better scanf_s() or gets_s() for strings in c programming.

learner369
  • 27
  • 6
  • It does not allow entry of subject name, or it doesn't print "Enter Subject name" because it has crashed? My guess is that a previous Carriage Return is being picked up by the scanf, but I will let others confirm. – Michael Dorgan Aug 27 '20 at 17:29

1 Answers1

2

Please tell how to correct this situation so that code asks me for all inputs.

There is a left newline from the scanf_s() call before. This newline character is fetched by gets_s which terminates the string input immediately.

Use getchar(); before the call to gets_s().

scanf_s("%i", &s[count].etec.sub_code);
getchar();       // fetch abandoned newline character from stdin.

printf("Enter subject name: ");
gets_s(s[count].etec.sub_name); 

Note that the gets_s form without second argument is only permissible in C++ and as far as I know stuck to Microsoft implementations only as it automatically gains the size from the buffer argument.

From the Microsoft gets_s documentation:

In C++, using these functions is simplified by template overloads; the overloads can infer buffer length automatically (eliminating the need to specify a size argument) and they can automatically replace older, non-secure functions with their newer, secure counterparts. For more information, see Secure Template Overloads.

Don't use a C++ compiler to compile C code. Your code is also not plain C code with this.

Also tell which one is better scanf_s() or gets_s() for strings in C programming?

None of them. Both of those functions aren't portable because they are not required to be implemented in each C implementation. Also, you're obviously use C++ code.

Use fgets() in C for reading strings from the input.