0
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;
}
  • Please see [fgets() doesn't work after scanf](https://stackoverflow.com/questions/5918079/fgets-doesnt-work-after-scanf). – Weather Vane Dec 29 '21 at 17:54
  • Not a good idea to mix `scanf()` and `fgets()` calls. – alex01011 Dec 29 '21 at 18:09
  • The trailing space in `"%d "` will make interactive input a bit clunky, but will consume the newline. But you need to do it consistently. The first `scanf` in the function does not have that trailing newline, so the first `fgets` is reading the remainder of the line. – William Pursell Dec 29 '21 at 18:09
  • 1
    You can avoid problems like this by *always* checking the value returned by scanf. If you use `scanf("%d",....)` and don't check the return value, you don't know if scanf read a value. In your case, it probably didn't. – William Pursell Dec 29 '21 at 18:10
  • 1
    You know the format, so you can read each line with `fgets()` and then `sscanf()` to extract the correct number of integers *in a single statement*. Always check the return value of the `scanf` function family. So the line with 5 integers can be say `sscanf(str, "%d%d%d%d%d", ...)` which should return `5` from the function call. – Weather Vane Dec 29 '21 at 18:19

0 Answers0