int readFromInput(struct route A[N]){
int n,i;
scanf("%d",&n);
for (i=0;i<n;i++){
scanf("%s",A[i].shmek);
while ((getchar()) != '\n');
scanf("%d ",&A[i].mhkosmon);
scanf("%d ",&A[i].ypsanab);
scanf("%d ",&A[i].ypskatab);
scanf("%d ",&A[i].megyps);
scanf("%d ",&A[i].elyps);
scanf("%s",A[i].shmter);
while ((getchar()) != '\n');
}
return n;
}
this is the source code that works just fine but the problem occurs when i want the scanf's to input 2 words (like Mount Olympus and yes i know the scanf reads until a space cahracter) but when i wirte the exact same code with fgets my input info to the struct just gets all messed up and i get something like this
(input : 3
Mikro Papigo
5919 989 42 1928 977
Katafygio Astrakas
Katafygio Astrakas
2764 295 202 2002 1745
Drakolimni
Limni Triadiou
11100 591 591 744 153
Limni Triadiou)
expected output :
From Limni Triadiou to Limni Triadiou
Uphill 591, Downhill 591
Max altitude 744, Min altitude 153
Length 11100, Estimated time 230 min
From Mikro Papigo to Katafygio Astrakas
Uphill 989, Downhill 42
Max altitude 1928, Min altitude 977
Length 5919, Estimated time 190 min
From Katafygio Astrakas to Mikro Papigo
Uphill 42, Downhill 989
Max altitude 1928, Min altitude 977
Length 5919, Estimated time 95 min
From Katafygio Astrakas to Drakolimni
Uphill 295, Downhill 202
Max altitude 2002, Min altitude 1745
Length 2764, Estimated time 72 min
From Drakolimni to Katafygio Astrakas
Uphill 202, Downhill 295
Max altitude 2002, Min altitude 1745
Length 2764, Estimated time 63 min
output :
From Katafygio Astrakas to Drakolimni
Uphill 295, Downhill 202
Max altitude 2002, Min altitude 1745
Length 2764, Estimated time 71 min
From Drakolimni to Katafygio Astrakas
Uphill 202, Downhill 295
Max altitude 2002, Min altitude 1745
Length 2764, Estimated time 244 min
From to Mikro Papigo
Uphill 0, Downhill 0
Max altitude 0, Min altitude 0
Length 0, Estimated time 0 min
From Mikro Papigo to
Uphill 0, Downhill 0
Max altitude 0, Min altitude 0
Length 0, Estimated time 0 min
From 5919 989 42 1928 977 to Katafygio Astrakas
Uphill 0, Downhill 0
Max altitude 0, Min altitude 0
Length 0, Estimated time 0 min
From Katafygio Astrakas to 5919 989 42 1928 977
Uphill 0, Downhill 0
Max altitude 0, Min altitude 0
Length 0, Estimated time 0 min
i repeat code works fine with the scanf except that not the entire string in inputted but this doesnt work at all how can i fix it
int readFromInput(struct route A[N]){
int n,i;
scanf("%d",&n);
for (i=0;i<n;i++){
fgets(A[i].shmek,50,stdin);
A[i].shmek[strcspn(A[i].shmek, "\n")] = 0;
scanf("%d ",&A[i].mhkosmon);
scanf("%d ",&A[i].ypsanab);
scanf("%d ",&A[i].ypskatab);
scanf("%d ",&A[i].megyps);
scanf("%d ",&A[i].elyps);
fgets(A[i].shmter,50,stdin);
A[i].shmter[strcspn(A[i].shmter, "\n")] = 0;
}
return n;
}