Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
908 views
in Technique[技术] by (71.8m points)

c - The scanf keeps being skipped

After I formatted the "direccion"'s scanf in function "leerdatos", it skip the "nro_corredor" in the second 'for' loop.

I've already read the related questions but still not getting an answer.

What can I do to solve it?

#include <stdio.h>

typedef struct {
    int minutos;
    int segundos;
} s_tiempo;

typedef struct {
        int nro_corredor;   // guarda nro de corredor
        s_tiempo tiempo;      // guarda el tiempo que tardo el corredor

} carrera;

typedef struct {
        int nro_corredor;  // guarda nro de corredor
        char apellido[20];
        char nombres[20];
        char direccion[30];
} datos;

datos leerdatos(); //declaro la funcion leer

int main (void) {
    int cp; //cantidad de participantes.
    datos aux;
    printf("Ingrese la cantidad de participantes: ");
    scanf("%d",&cp);
    datos corredor[cp];
    carrera carreras[cp];
    printf("A continuacion, ingrese los datos de los corredores: 
");
    for(int i=0;i<cp;i++){
        aux = leerdatos();
        corredor[i] = aux;
    }
}

datos leerdatos(void) {
    datos participante;
    printf("
Ingrese el numero de corredor: ");
    scanf("%d",&participante.nro_corredor);
    printf("
Ingrese el apellido:
");
    scanf("%s",participante.apellido);
    printf("
Ingrese los nombres:
");
    scanf("%s",participante.nombres);
    printf("
Ingrese la direccion:
");
    scanf(" %s
",participante.direccion);
    return(participante);
}
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Incorrect usage of scanf()

// scanf(" %s
", ...
scanf("%s", ...

A white-space like ' ' after the "%s" directs scanf() to look for white-space until a non-white space follows. That non-white-space is put back into stdin. This likely means OP had to enter 2 Enter for the 4th input.

Should always check the scanf() return value to see if it is as expected.

// scanf("%d",&participante.nro_corredor);
if (scanf("%d",&participante.nro_corredor) != 1) Handle_Error();

Putting a space before "%s" or "%d" in scanf() make no difference. These specifiers will consume optional white space even without a leading " ".

Recommend to use fgets() to read user input (which is evil) and then parse with sscanf(), strtok(), strtol(), etc.


Note: except inside a scanset "%[...]", white-space like ' ', '', ' ', and others each perform the same: They consume any number of white-space. That ' ' in scanf(" %s ",... does not scan for 1 ' '. It scans for any number of any white-space. It will keep scanning white-space, including multiple Enters until EOF or a non-whitespace is entered. That non-white-space is then put back into stdin as the next `char to be read.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...