LinuxQuestions.org
Did you know LQ has a Linux Hardware Compatibility List?
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-15-2005, 08:55 AM   #1
purefan
Member
 
Registered: Aug 2003
Location: Sweden
Distribution: Ubuntu 10.04
Posts: 99

Rep: Reputation: Disabled
checking if entered a char


hello!
Well I must write a program that recieves 3 arguments and then returns if those 3 arguments make for a triangle (Equilateral triangle, Isosceles triangle and Scalene).
For such the program must recieve only integers but we have to use this sentence:

const char* Triang(int argc, char *argv[]) { ... }

so the user can enter text as the second argument... The idea behind is that we learn how to differ from a char/string and an integer...
however i do not know how to accomplish this...
I know how to find out which type of triangle it is but not how to know if the user entered a letter or a string (say 'apple', '/', etc).
Thanks for the time =D
 
Old 03-15-2005, 09:40 AM   #2
zeropash
Member
 
Registered: Apr 2003
Location: Bangalore,India
Distribution: FC2, RHES, RH9, FC3, FC1, Slackware 3.0
Posts: 208

Rep: Reputation: 30
/* returns 0 if not integer 1 if integer */
int is_input_integer(char *input) {
while (*input) {
if (!isdigit(*input++))
return 0;
}
return 1;
}


call it like
is_input_integer(argv[i])

Last edited by zeropash; 03-15-2005 at 09:41 AM.
 
Old 03-15-2005, 12:58 PM   #3
jonaskoelker
Senior Member
 
Registered: Jul 2004
Location: Denmark
Distribution: Ubuntu, Debian
Posts: 1,524

Rep: Reputation: 46
or you could use this:

int a, b, c, d, e, f;
if (1 == sscanf (argv[1], "%d", &a))
/*first number was an int*/;
if (1 == sscanf (argv[2], "%d", &b))
/*second too*/
if (1 == sscanf (argv[3], "%d", &c))
...
...
if (1 == sscanf (argv[6], "%d", &f))
/*Houston, we have a triangle*/

a `cleaner' design would be to use a struct point { int x, y; };

Hope this helps,

Jonas Kölker
 
Old 03-16-2005, 09:22 PM   #4
purefan
Member
 
Registered: Aug 2003
Location: Sweden
Distribution: Ubuntu 10.04
Posts: 99

Original Poster
Rep: Reputation: Disabled
Thank you both very much!!
your help has been of great use!
 
  


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
C pointers confusion - char ** = char [][] ?? saravkrish Programming 12 12-02-2004 10:06 AM
C Problem---convert char to char* totti10 Programming 11 11-06-2004 11:32 AM
invalid conversion from `char' to `const char* bru Programming 6 05-09-2004 03:07 PM
convert from char* to unsigned char* D J Linux - Software 2 02-20-2004 04:09 AM
Copy a char* to another char* sathyan Programming 8 07-24-2003 06:15 AM


All times are GMT -5. The time now is 02:01 PM.

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 Google+: linuxquestions
Open Source Consulting | Domain Registration