问题描述
在我格式化direccion的scanf函数的功能leerdatos,它跳过nro_corredor第二'for'循环。
我已经阅读了相关的问题,但仍然没有得到答案。
我能做些什么来解决呢?
的#include<&stdio.h中GT;typedef结构{
INT minutos;
INT segundos;
} s_tiempo;typedef结构{
INT nro_corredor; //瓜尔达NRO德corredor
s_tiempo蒂恩波; //瓜尔达时代报阙tardo埃尔corredor
}卡雷拉;typedef结构{
INT nro_corredor; //瓜尔达NRO德corredor
炭apellido [20];
炭nombres [20];
CHAR direccion [30];
} DATOS;DATOS leerdatos(); // declaro拉funcion秋波诠释主要(无效){
INT CP; // cantidad德participantes。
DATOS辅助;
的printf(Ingrese拉cantidad德participantes:);
scanf函数(%d个,&安培; CP);
DATOS corredor [CP]
卡雷拉卡雷拉斯[CP]
的printf(A continuacion,ingrese洛杉矶DATOS德洛斯corredores:\\ n);
的for(int i = 0; I< CP;我++){
辅助= leerdatos();
corredor [I] =辅助;
}
}DATOS leerdatos(无效){
DATOS PARTICIPANTE;
的printf(\\ nIngrese EL NUMERO德corredor:);
scanf函数(%d个,&安培; participante.nro_corredor);
的printf(\\ nIngrese EL apellido:\\ n);
scanf函数(%S,participante.apellido);
的printf(\\ nIngrese洛杉矶nombres:\\ n);
scanf函数(%S,participante.nombres);
的printf(\\ nIngrese LA direccion:\\ n);
scanf函数(%S \\ n,participante.direccion);
返回(PARTICIPANTE);
}
你面对的另一个问题是需要为调用后刷新输入缓冲区(标准输入) scanf函数
。作为chux指出, scanf函数
不消耗的'\\ n'
把它留在输入缓冲器被解读为换行符下一个字符。这是一件好事每次使用 scanf函数
的尤其在 scanf函数被多次调用。一个简单的方法来刷新输入缓冲区为:
INT C = 0; / *内部测试的的getchar * /
...
scanf函数(%d个,&安培; CP);
做{C =的getchar(); }而(C ='\\ n'!); / *刷新输入缓冲区* /
注意:
fflushf
确实不留在清除字符标准输入
。
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: \n");
for(int i=0;i<cp;i++){
aux = leerdatos();
corredor[i] = aux;
}
}
datos leerdatos(void) {
datos participante;
printf("\nIngrese el numero de corredor: ");
scanf("%d",&participante.nro_corredor);
printf("\nIngrese el apellido:\n");
scanf("%s",participante.apellido);
printf("\nIngrese los nombres:\n");
scanf("%s",participante.nombres);
printf("\nIngrese la direccion:\n");
scanf(" %s\n",participante.direccion);
return(participante);
}
解决方案
One other issue you are faced with is the need to flush the input buffer (stdin) after calling
scanf
. As chux noted, scanf
does not consume the newline '\n'
leaving it in the input buffer to be read as the next character. It is always good to flush the input buffer after each use of scanf
, especially when scanf
is called multiple times. A simple way to flush the input buffer is:
int c = 0; /* test int for getchar */
...
scanf("%d",&cp);
do { c=getchar(); } while ( c != '\n'); /* flush input buffer */
Note:
fflushf
does not clear characters remaining in stdin
.
这篇关于scanf函数不断被跳过的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!