Hi i am working on this program. I have looked over it and i cannot determine an explanation for its behavior. Can someone help explain why and how to overcome this issue? Thanks a bunch.
Code:
#include <string.h>
#include <stdio.h>
int
main ()
{
char *the_message = "42.77417833333333|-78.31375166666667";
char latitude[17];
char longitude[17];
int lat_counter_x = 0;
int long_counter_x = 0;
int long_counter_y = 0;
int pipe_location;
for (lat_counter_x = 0; lat_counter_x <= strlen (the_message);
lat_counter_x++)
{
if (the_message[lat_counter_x] == '|')
{
pipe_location = lat_counter_x; //shows character location of | - separates lat/long numbers
}
}
if (pipe_location)
{
for (lat_counter_x = 0; lat_counter_x < (pipe_location - 1);
lat_counter_x++)
{
latitude[lat_counter_x] = the_message[lat_counter_x];
}
latitude[lat_counter_x + 1] = '\0';
}
printf ("Latitude: %s\n", latitude); // displays properly 42.77417833333333
for (long_counter_x = (pipe_location + 1);
long_counter_x <= strlen (the_message); long_counter_x++)
{
longitude[long_counter_y] = the_message[long_counter_x];
long_counter_y++;
}
longitude[long_counter_y] = '\0';
printf ("Longitude: %s\n", longitude); // displays properly -78.31375166666667
printf ("Latitude: %s\n", latitude); //the number changes .... why? - latitude is now the last char of longitude
printf ("Longitude: %s\n", longitude); // displays properly -78.31375166666667
}
//If i comment out the second for loop, latitude is not affected.
/* Output
Latitude: 42.7741783333333
Longitude: -78.31375166666667
Latitude: 7
Longitude: -78.31375166666667
*/