当我运行我的程序,与fgets()被忽略和scanf()不输入数据结构 [英] When I run my program, fgets() is ignored and scanf() doesn't input data to structure
问题描述
我磨片运行FGETS()会被忽略和scanf()不输入数据结构。
我的code:
的#includestdio.h中
#包括stdlib.h中typedef结构{
焦炭nazv [50];
INT numr;
INT日[3];
INT时间[2];
}列车;无效输入(火车*锉刀,INT N);
无效打印(火车*锉刀,INT N);
无效输出(火车*锉刀,INT N,为int *日期为int *时间);主(){
INT N;
INT T [2];
INT D [3];
的printf(多少钱?);
scanf函数(%d个,&安培; N);
列车*锉刀=(火车*)malloc的(N * sizeof的(火车));
输入(锉刀,N);
的printf(请输入日期:);
对于(INT日期= 0;日期3;;日期++){
scanf函数(%D,和D [日期]);
}
的printf(请输入时间:);
对(INT时间= 0;时间2;时间++){
scanf函数(%d个,& T公司[时间]);
}
的for(int i = 0; I< N;我++)
打印(锉刀,N);
输出(锉刀,N,D,T);
免费(锉);
返回0;
}
无效输入(火车*锉刀,INT N){
的for(int i = 0; I< N;我++){
的printf(火车#%d个\\ N,I + 1);
的printf(请输入列车的名字:);
与fgets(锉刀[I] .nazv,50,标准输入);
的printf(请输入列车的编号:);
scanf函数(%d个,&安培;锉刀[I] .numr);
的printf(请输入日期:);
为(中间体D = 0; D&下; 3;ð++){
scanf函数(%d个,&安培;锉刀[I] .date [D]。);
}
的printf(请输入时间:);
对于(INT T = 0; T&2;吨++){
scanf函数(%d个,&安培;锉刀[I]。时间[T]);
}
}
的printf(\\挪威克朗。);
}
无效打印(火车*锉刀,INT N){
的printf(姓名:);
的printf(%S,锉刀[N] .nazv);
的printf(\\ n已接收数:%d,锉刀[N] .numr);
的printf(\\ nDate:%D%D,锉刀[N] .date [0],锉刀[N] .date [1],锉刀[N] .date [2]);
的printf(\\ n时间:%D:%d个\\ N,锉刀[N]。时间[0],锉刀[N]。时间);
}
无效输出(火车*锉刀,INT N,为int *日期为int *时间){
的for(int i = 0; I< N;我++){
如果(锉刀[n]的.date [2]所述;日期[2])
继续;
如果(锉刀[n]的.date [2]≥日期[2]){
打印(锉刀,N);
}其他{
如果(锉刀[N] .date [1],日[1])
继续;
如果(锉刀[n]的.date [1]≥日期[1]){
打印(锉刀,N);
}其他{
如果(锉刀[N] .date [0]<日期[0])
继续;
如果(锉刀[N] .date [0]≥日期[0]){
打印(锉刀,N);
}其他{
如果(锉刀[n]的。时间[0]&下;时间[0])
继续;
如果(锉刀[n]的。时间[0]≥时间[0]){
打印(锉刀,N);
}其他{
如果(锉刀[n]的。时间[1] =时间[1])
继续;
打印(锉刀,N);
}
}
}
}
}
}
input()函数应该把火车到数据库。
输出()函数只能用打印日期从数据库列车晚于之前提供的一个用户。
scanf函数(%d个,&安培; N);
scanf函数
叶,在输入缓冲器发送的输入到程序的换行符。
列车*锉刀=(火车*)malloc的(N * sizeof的(火车));
输入(锉刀,N);无效输入(火车*锉刀,INT N){
的for(int i = 0; I< N;我++){
的printf(火车#%d个\\ N,I + 1);
的printf(请输入列车的名字:);
与fgets(锉刀[I] .nazv,50,标准输入);
与fgets
发现一个换行符作为输入缓冲器中的第一个字符,并且不进一步看,在输入缓冲区离开列车的名称。
的printf(请输入列车的编号:);
scanf函数(%d个,&安培;锉刀[I] .numr);
其中 scanf函数
未能转换成 INT
。
混合 scanf函数
和与fgets
用户输入的是不同的换行符的行为容易出错是因。分析除非你清除之间的 scanf函数
及以下与fgets
的与fgets 输入缓冲区code>最常见的交易只会换行遗留形成
scanf函数
(也有少数 scanf函数
格式消耗换行)。
您可以清除缓存
无效clear_input_buffer(无效){
INT℃;
而((C =的getchar())= EOF和放大器;!&安培;!C ='\\ n');
如果(C == EOF){
//输入流断了,现在该怎么办?
出口(EXIT_FAILURE);
}
}
或使用与fgets
所有输入,并使用的sscanf
(和与strtol
和朋友)来解析输入。
Whe I run it fgets() is simply ignored and scanf() doesn't input data to structure.
My code:
#include"stdio.h"
#include"stdlib.h"
typedef struct {
char nazv[50];
int numr;
int date[3];
int time[2];
} train;
void input(train*rasp,int n);
void print(train*rasp,int n);
void output(train*rasp, int n, int*date, int*time);
main() {
int n;
int t[2];
int d[3];
printf("How much? ");
scanf("%d",&n);
train* rasp=(train*)malloc(n*sizeof(train));
input(rasp,n);
printf("Enter date: ");
for (int date=0;date<3;date++) {
scanf("%d",&d[date]);
}
printf("Enter time: ");
for (int time=0;time<2;time++) {
scanf("%d",&t[time]);
}
for (int i=0;i<n;i++)
print(rasp,n);
output(rasp,n,d,t);
free(rasp);
return 0;
}
void input(train*rasp,int n) {
for (int i=0;i<n;i++) {
printf("Train #%d\n",i+1);
printf("Enter train's name: ");
fgets(rasp[i].nazv,50,stdin);
printf("Enter train's number: ");
scanf("%d",&rasp[i].numr);
printf("Enter date: ");
for (int d=0;d<3;d++) {
scanf("%d",&rasp[i].date[d]);
}
printf("Enter time: ");
for (int t=0;t<2;t++) {
scanf("%d",&rasp[i].time[t]);
}
}
printf("\nOK.");
}
void print(train*rasp,int n) {
printf("Name: ");
printf("%s",rasp[n].nazv);
printf("\nNumber: %d",rasp[n].numr);
printf("\nDate: %d.%d.%d", rasp[n].date[0],rasp[n].date[1],rasp[n].date[2]);
printf("\nTime: %d:%d\n", rasp[n].time[0],rasp[n].time);
}
void output(train*rasp,int n,int*date,int*time) {
for (int i=0;i<n;i++) {
if (rasp[n].date[2]<date[2])
continue;
if (rasp[n].date[2]>date[2]) {
print(rasp,n);
} else {
if (rasp[n].date[1]<date[1])
continue;
if (rasp[n].date[1]>date[1]) {
print(rasp,n);
} else {
if (rasp[n].date[0]<date[0])
continue;
if (rasp[n].date[0]>date[0]) {
print(rasp,n);
} else {
if (rasp[n].time[0]<time[0])
continue;
if (rasp[n].time[0]>time[0]) {
print(rasp,n);
} else {
if (rasp[n].time[1]<=time[1])
continue;
print(rasp,n);
}
}
}
}
}
}
input() function should put trains into to database. output() function should only print trains from database with a date later than the one user supplied before.
scanf("%d",&n);
scanf
leaves the newline that sent the input to the programme in the input buffer.
train* rasp=(train*)malloc(n*sizeof(train));
input(rasp,n);
void input(train*rasp,int n) {
for (int i=0;i<n;i++) {
printf("Train #%d\n",i+1);
printf("Enter train's name: ");
fgets(rasp[i].nazv,50,stdin);
fgets
finds a newline as the first character in the input buffer and doesn't look further, leaving the train's name in the input buffer.
printf("Enter train's number: ");
scanf("%d",&rasp[i].numr);
Which scanf
fails to convert into an int
.
Mixing scanf
and fgets
for user input is error-prone becaus of the different newline-behaviour. Unless you clear the input buffer between a scanf
and a following fgets
, the fgets
most often will only get the newline left over form the scanf
(there are a few scanf
formats that consume the newline).
You can clear the buffer
void clear_input_buffer(void) {
int c;
while((c = getchar()) != EOF && c != '\n');
if (c == EOF) {
// input stream broken, what now?
exit(EXIT_FAILURE);
}
}
or use fgets
for all input and use sscanf
(and strtol
and friends) to parse the input.
这篇关于当我运行我的程序,与fgets()被忽略和scanf()不输入数据结构的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!