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.
scanfleaves the newline that sent the input to the programme in the input buffer.fgetsfinds a newline as the first character in the input buffer and doesn’t look further, leaving the train’s name in the input buffer.Which
scanffails to convert into anint.Mixing
scanfandfgetsfor user input is error-prone becaus of the different newline-behaviour. Unless you clear the input buffer between ascanfand a followingfgets, thefgetsmost often will only get the newline left over form thescanf(there are a fewscanfformats that consume the newline).You can clear the buffer
or use
fgetsfor all input and usesscanf(andstrtoland friends) to parse the input.