ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
Hi all,
I'm trying to write a program that converts longitudinal and latitudinal co-ordinates from two places into x,y,z co-ordinates and calculates the surface distance between them. Part of the conversion calls for the addition of 90 degrees if the lattitudinal co-odinates are in the south or a subtraction if they are in the north. I was wondering how i would go about scanning in the North/ South portion of the input and comparing it with the formula so the program knows what to do next.
i'm doing this in C
and the input is a command line input
223(this would be the degrees), 30(this would be the minutes), 30(this would be seconds) North (the Direction)
so the input would be something like this
Please enter the lattitudinal and longitudinal co-ordinates:
223 30 30 North
145 45 15 West
essentially i will be scanning those numbers in and converting it into a final degree number and through a formula depending on whether it's North or South Lattitude you'll add or minus 90 degrees (this is where the problem is)
the same would apply for the East or west for the Longitude.
You'll have to parse the input somehow. I'd suggest looking at the strtok() function. You'd obviously want to use the space as your delimiter. As for the north/south/east/west bit. Look at the strcmp() function. Just be aware that strcmp is case-sensitive...
I've never used the strtok() function, so I can't really comment on how it works, but to do it down and dirty, I would suggest going through the whole input string and where ever there's a space, set it to 0 (null terminate). Then, using the address of the first character of each substring, use the atoi() function in <stdlib.h> to type cast the substring into an integer. For the Direction, it'll be a simple use of strcmp().
Here's a quick idea of what I mean, in case I didn't explain it well enough
Here is an example that does not contain any error or range checking. It's better to use a character buffer, which you can tokenize and apply library functions to covert the input into integral types. That way you can validate the input.
Code:
#include<stdio.h>
int main() {
int x,y,z;
char myStr[256];
printf("Enter the input line: ");
scanf("%d%d%d%s", &x, &y, &z, myStr);
printf("You entered: %d, %d, %d, and %s.\n",x,y,z,myStr);
//call functions
return 0;
}
The C++ fstreams are good for this, but most people are not familiar with them.
You'll have to forgive me but i'm not too familiar with character arrays. i could barely follow some of the code sienarot wrote. i don't think i'll have too much trouble scanning in the numbers because i can deal with integers, it's the string comparison i am shaky on. i'm not at all familiar with the strcmp() function.
C is the first programming language i'm learning so i don't know much about Perl any any of those other languages.
I have an ideia that is simpler. So what you can do it ask for n, w, e and s, what includes all directions. And then do a swith with your options. Also, if you think it is easier you can treat a char as an integer (ASCII).
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.