LinuxQuestions.org
View the Most Wanted LQ Wiki articles.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
LinkBack Search this Thread
Old 03-09-2003, 10:49 AM   #1
Randall
Member
 
Registered: Oct 2001
Location: Ontario, Canada (for now, i'm from NJ )
Distribution: Redhat 7.2
Posts: 106

Rep: Reputation: 15
storing and comparing a string


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 appreciate any input at all

thanks,
Randall
 
Old 03-09-2003, 12:31 PM   #2
sienarot
Member
 
Registered: Mar 2003
Location: Calgary, Alberta, Canada
Distribution: Gentoo 1.4, Debian Woody, Mandrake 8.1
Posts: 43

Rep: Reputation: 15
what language?
How is the information inputted? Command line? File? Give an example of what the input would look like.

Last edited by sienarot; 03-09-2003 at 12:33 PM.
 
Old 03-09-2003, 01:13 PM   #3
Randall
Member
 
Registered: Oct 2001
Location: Ontario, Canada (for now, i'm from NJ )
Distribution: Redhat 7.2
Posts: 106

Original Poster
Rep: Reputation: 15
Sorry about the lack of information.

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.
 
Old 03-09-2003, 01:46 PM   #4
cuckoopint
Member
 
Registered: Feb 2003
Distribution: Debian
Posts: 797

Rep: Reputation: 30
Quote:
you'll add or minus 90 degrees (this is where the problem is)
you mean...an IF statement?
/me all of a sudden feels stupid, since he's clearly missing the point
 
Old 03-09-2003, 02:08 PM   #5
Dark_Helmet
Senior Member
 
Registered: Jan 2003
Posts: 2,786

Rep: Reputation: 361Reputation: 361Reputation: 361Reputation: 361
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...
 
Old 03-09-2003, 02:53 PM   #6
sienarot
Member
 
Registered: Mar 2003
Location: Calgary, Alberta, Canada
Distribution: Gentoo 1.4, Debian Woody, Mandrake 8.1
Posts: 43

Rep: Reputation: 15
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

------------------------------
#include<iostream.h>
#include<stdlib.h>


int main(){

char arr[] = {"123 456 678 test\0"};
short index = 0;
short x, y, z;


while(arr[index] != 0) // until string NULL terminated
{
if(arr[index] == 32) // if space
arr[index] = 0; // null terminate spaces

index++;
}

x = atoi(&arr[0]); // assign substrings
y = atoi(&arr[4]);
z = atoi(&arr[8]);

cout << x << endl;
cout << y << endl;
cout << z << endl;




return 0;
}

Last edited by sienarot; 03-09-2003 at 03:33 PM.
 
Old 03-09-2003, 03:22 PM   #7
JStew
Member
 
Registered: Oct 2002
Location: North Atlanta
Distribution: LFS
Posts: 229

Rep: Reputation: 30
why not do this in perl? the perl parsing functions are a lot simpler and save a bit of code to do this
 
Old 03-09-2003, 03:30 PM   #8
Dark_Helmet
Senior Member
 
Registered: Jan 2003
Posts: 2,786

Rep: Reputation: 361Reputation: 361Reputation: 361Reputation: 361
oooooh... well, maybe you shouldn't use strtok(). There's a suggestion not to use it in the man page. It's ok for simple stuff like this though.

sieranot's example would work just as well.
 
Old 03-10-2003, 10:53 AM   #9
GtkUser
Member
 
Registered: Sep 2002
Location: Canada
Distribution: Redhat 9.0
Posts: 637

Rep: Reputation: 30
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.

Last edited by GtkUser; 03-10-2003 at 10:54 AM.
 
Old 03-10-2003, 05:25 PM   #10
JStew
Member
 
Registered: Oct 2002
Location: North Atlanta
Distribution: LFS
Posts: 229

Rep: Reputation: 30
anyone ever use apstring.h? we have that in our class at school but have never seen anyone out in the "real world" ever use it.
 
Old 03-10-2003, 05:52 PM   #11
Randall
Member
 
Registered: Oct 2001
Location: Ontario, Canada (for now, i'm from NJ )
Distribution: Redhat 7.2
Posts: 106

Original Poster
Rep: Reputation: 15
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.
 
Old 03-10-2003, 06:00 PM   #12
Dark_Helmet
Senior Member
 
Registered: Jan 2003
Posts: 2,786

Rep: Reputation: 361Reputation: 361Reputation: 361Reputation: 361
Well, you can use the man pages for most functions.

man strcmp

will give you a description of the arguments it takes and what the return values are.

Is there anything particular that throws you off in its description?
 
Old 03-10-2003, 06:18 PM   #13
Dark_Helmet
Senior Member
 
Registered: Jan 2003
Posts: 2,786

Rep: Reputation: 361Reputation: 361Reputation: 361Reputation: 361
Actually, you might want to use strcasecmp() instead; it is not a case-sensitive comparison.
 
Old 03-10-2003, 06:34 PM   #14
loke137
Member
 
Registered: Feb 2003
Location: Brasil
Distribution: Debian Etch
Posts: 147

Rep: Reputation: 15
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).
 
Old 03-10-2003, 06:44 PM   #15
Randall
Member
 
Registered: Oct 2001
Location: Ontario, Canada (for now, i'm from NJ )
Distribution: Redhat 7.2
Posts: 106

Original Poster
Rep: Reputation: 15
would i use it like this ?

Code:
#include <stdio.h>
#include <string.h>
int main(void)
{
              char s1[] = "password",
                      s2[];
              printf(" Enter a password:\n");
              scanf("%s", s2);
              x = strcmp(s1,s2);
              if (x == 0);
                     printf("password accepted");
              else
                     printf("access denied");
              
              return(0);
}
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
storing 5 chars into an int in C++? jwn7 Programming 28 05-30-2005 06:54 AM
[php] comparing found username with string Erhnam Programming 4 03-15-2005 09:34 PM
Storing Strings ToothlessRebel Programming 4 09-05-2004 03:16 AM
java test if string in string array is null. exodist Programming 3 02-21-2004 01:39 PM
Storing /home in CVS Cyth Linux - General 2 01-03-2003 03:10 AM


All times are GMT -5. The time now is 05:29 AM.

Main Menu
 
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: @linuxquestions
Open Source Consulting | Domain Registration