0

I am getting a -NaN message and I don't understand why?
The program so far compiles okay. But prints "the value of lat1 is -NaN."
Any help would be greatly appreciated.

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


float get_lat1();

int main (void)
{
  float lat1;

  get_lat1();

  printf("the value of lat1 is %f \n", lat1);

  return (0);
}


float get_lat1()
{
  int degrees;
  int minutes;
  float seconds;
  float lat1;
  char direction;

  printf("Enter the Prepare to enter data for first coordinate:\n"); 
  printf("Enter the latitude in the form (DD MM SS.S):\n");
  scanf("%d" ,&degrees);
  scanf("%d" ,&minutes);
  scanf("%f" ,&seconds);

  lat1 = degrees + (minutes / 60) + (seconds / 3600);

  do
  {
    printf("Enter N for North or S for South: \n");
    fflush(stdin);
    scanf("%c", &direction);


    if(direction == 83)
      {
        lat1 = -lat1;

        return(lat1);
      }
    else if(direction == 78) 
      {
        lat1 = lat1;

        return(lat1);
      }
    else
      {
        printf("Invalid Input! Please Try Again!\n");
      }
  }while (direction != 83 || direction != 78);
printf("the value of lat1 is %f \n", lat1);

  return(lat1);
}

Edited by Dani: Formatting fixed

2
Contributors
1
Reply
2
Views
13 Years
Discussion Span
Last Post by Dave Sinkula
0

> Any help would be greatly appreciated.

#include <stdio.h>

float get_lat1(void);

int main (void)
{
   float latitude = get_lat1();
   printf("the value of latitude is %f \n", latitude);
   return 0;
}

float get_lat1(void)
{
   int degrees, minutes;
   float seconds, latitude = 0;
   char direction;

   printf("Enter the Prepare to enter data for first coordinate:\n");
   printf("Enter the latitude in the form (DD MM SS.S): ");
   fflush(stdout);
   if(scanf("%d%d%f", &degrees, &minutes, &seconds) == 3)
   {
	  latitude = degrees + (minutes / 60.0) + (seconds / 3600);
	  for(;;)
	  {
		 printf("Enter N for North or S for South: ");
		 fflush(stdout);
		 do { int c; while ( (c = getchar()) != '\n' && c != EOF); } while(0);
		 scanf("%c", &direction);
		 switch(direction)
		 {
		 case 'S': return -latitude;
		 case 'N': return  latitude;
		 default:  break;
		 }
		 printf("Invalid Input! Please Try Again!\n");
	  }
   }
   return latitude;
}

/* my output
Enter the Prepare to enter data for first coordinate:
Enter the latitude in the form (DD MM SS.S): 45 15 36.54
Enter N for North or S for South: N
the value of latitude is 45.260151
*/
This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.