0
#include<stdio.h>
#include<math.h>

int main(){

    float L1,L2,G1,G2,D;
    printf("Enter The Latitudes: (L1,L2) :");
    scanf(" %f, %f \n",&L1,&L2);
    printf("Enter The Longitudes: (G1,G2) :");
    scanf(" %f, %f \n",&G1,&G2);

    D = 3963 *acos(sin(L1)*sin(L2) + cos(L1)*cos(L2)*cos(G2-G1));

    printf("Distance In Nautical Miles: %f",D);
    return 0;
}

The Second scanf is being ignored I guess and I have read many previous related answers and they were all about %c. but in my case it is %f.

Help will be appreciated.

littleadv
  • 20,100
  • 2
  • 36
  • 50
Asif
  • 1
  • Why do you think it matters if it's `%c` or `%f`? – littleadv Jan 29 '22 at 16:01
  • @littleadv Because `%c` and `%f` are completely different in their whitespace handling. There's a big problem which `%c` has, with many answers about it, but that's not the problem here, the problem here is different. – Steve Summit Jan 29 '22 at 16:04
  • 2
    The statement “The Second `scanf` is being ignored” is a conclusion. When describing computer problems to other people, you should describe observations, like this: “When I run the program, it prints the prompt for latitudes. When I enter latitudes, such as `30, 40`, and press return, the cursor returns and advances to the start of the next line, but there is no further response or output from the program at that time. If I then enter the longitudes, such as `50, 60`, then the program prints the prompt for longitudes. But then it just sits at that prompt, doing nothing.” – Eric Postpischil Jan 29 '22 at 16:05
  • @Asif Summary of the rules: (1) Never put whitespace (spaces or `\n`) at the end of a `scanf` format statement. (2) Unless you have special needs, if you're using `%c`, always put an explicit space before it (`" %c"`). (3) For other formats (like `%f`), leading whitespace is unnecessary, but won't cause any problems (other than perhaps confusion on the part of a later reader). – Steve Summit Jan 29 '22 at 16:09

1 Answers1

2

Spaces and newlines in a scanf format string will match any amount of whitespace. This means that if your format string ends with spaces or newlines, it will continue to read until it sees a character that is not a newline.

You can fix this by removing the spaces and newlines at the end of the format string. You also don't need spaces at the start because the %f format specifier will consume any leading whitespace.

scanf("%f,%f",&L1,&L2);
scanf("%f,%f",&G1,&G2);
dbush
  • 205,898
  • 23
  • 218
  • 273
  • There's a canonical duplicate for this problem — the question is now closed as a duplicate of it. The commas in the format string require commas in the input too, of course. – Jonathan Leffler Jan 29 '22 at 17:20
  • @JonathanLeffler I was unable to find one when I first looked at the question. Guess I wasn't using the right keywords. – dbush Jan 29 '22 at 17:24
  • It's one of a fairly large number of questions I have bookmarked for duplicates on SO. I wish SO would provide facilities to make it easier for people to record duplicates for their own reference. – Jonathan Leffler Jan 29 '22 at 17:26