in the loop my gets_s is skipped in the second iteration

0

I am asking for the note and the names of 10 students but when it enters the second iteration of the for the gets_s it is skipped, do not let me enter the student's name.

#include <string.h>
#include <conio.h>
#include <stdio.h>

struct Student
{
    char name[50];
    int grade;
};

Student students[10];

int main()
{
    for (int i = 0; i < 10; i++)
    {
        printf("Introduce el nombre del alumno:");
        gets_s(students[i].name, sizeof(students[i].name));

        printf("Introduce la nota del alumno:");
        scanf_s("%d", &students[i].grade);
    }
}
    
asked by Humberto Escorce 14.10.2018 в 21:27
source

2 answers

3

scanf_s does not remove the line break, which is what the second iteration of gets_s reads. Simply delete that line break so that the reading is correct.

To eliminate that line break no it is advisable to use fflush , since its use is not guaranteed by the standard, as indicated by the documentation of the function :

  

In all other cases, the behavior depends on the specific library implementation. In some implementations, flushing a stream open for reading causes its input buffer to be cleared ( but this is not portable expected behavior ).

Although there is no standard way to clean the input buffer, the most common option is to use the following loop:

int c;
while ((c = getchar()) != '\n' && c != EOF);

On the other hand, since you label the question as C ++, the logical thing is that you use functions of C ++ instead of those inherited from C.

In the case of using C ++, the stream input, stdin yes that has a specific method to discard characters, the function ignore() . This function will discard the given number of characters unless it is previously with the delimiter character passed as a parameter. If this second happens, the delimiter will be discarded and characters will be discarded.

#include <iostream>
#include <string>

struct Student
{
    std::string name;
    int grade;
};

Student students[10];

int main()
{
    for (int i = 0; i < 10; i++)
    {
        std::cout << "Introduce el nombre del alumno:";
        std::cin.ignore(std::numeric_limits<int>::max(),'\n');
        std::getline(std::cin,students[i].name);

        std::cout << "Introduce la nota del alumno:";
        std::cin >> students[i].grade;
    }
}

numeric_limits is a template that has information about the basic types of the language. In this case the method max() returns the largest number that can be stored in a variable of type int . Using this value guarantees that before, whatever happens, we will find a line break (which is the character we want to eliminate).

    
answered by 15.10.2018 в 08:10
0

Investigating a bit I found in the documentation of Microsoft that you should use the fflush (...) function before using the gets_s (...) function as follows:

fflush( stdin );

So that you clean the entrance buffer, check the link that I left you, there is an example that will surely help you. Greetings.

    
answered by 14.10.2018 в 23:04