By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
429,078 Members | 2,164 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 429,078 IT Pros & Developers. It's quick & easy.

Decting user input for float or int

P: 2
Okay heres my problem I have a problem called Calc which the user enters 2 numbers and a sign to be done with what is entered
EX:

./calc 200 + 200
or
./calc 200.20 + 200

Okay thats easy where I can't seem to get, my prof wants the input checked to see if the char *argv[1] or char *argv[2] contains a float or int. so I have to check for a decimal point.

I know I can search the number like a string because it's in an array of char but my code seems to not work for that. So I tool another approach and I'm use strtol to convert it to a int. If the decimal point is found then I can assume the number is float.

Here is my code for examimation:

Expand|Select|Wrap|Line Numbers
  1. #include<stdio.h>
  2.  
  3. #include<stdlib.h>
  4. #include<string.h>
  5. #include<errno.h>
  6. #include<limits.h>
  7.  
  8.  
  9.  
  10. char *FindChr(char *szString, char chr);
  11.  
  12. int main(int argc, char *argv[])
  13.  
  14. {
  15.  
  16. float fValueOneInputed;
  17.  
  18. float fValueTwoInputed;
  19. float fCalculatorStore;      
  20.  
  21.     //Test the input to see if the value entered if Float or Int
  22.     FindChr(argv, '.');
  23.     //Get the first input value in char form and take it to a float          
  24.     sscanf(argv[1],"%f",&fValueOneInputed);
  25.  
  26.     //Get the first input value in char form and take it to a float    
  27.     sscanf(argv[3],"%f",&fValueTwoInputed);
  28.  
  29.     if(*argv[2] == '+')printf("%0.3f", fCalculatorStore = fValueOneInputed + fValueTwoInputed);
  30.  
  31.     if(*argv[2] == '-')printf("%0.3f", fCalculatorStore = fValueOneInputed - fValueTwoInputed);
  32.  
  33.     if(*argv[2] == 'x')printf("%0.3f", fCalculatorStore = fValueOneInputed * fValueTwoInputed);
  34.  
  35.     if(*argv[2] == '/')printf("%0.3f", fCalculatorStore = fValueOneInputed / fValueTwoInputed);
  36. return 0;
  37.  
  38. }
  39.  
  40. char *FindChr(char *szString, char chr)
  41.  
  42. {
  43.  
  44. int base;
  45. int iLoop;
  46. long lTestVal;
  47. char *str;
  48. char *endptr;
  49. str = szString[1];
  50. base = 0;
  51. errno = 0;
  52. lTestVal = strtol(str, &endptr, base);
  53.  
  54. /* Check for Error's */
  55. if((errno == ERANGE && (lTestVal == LONG_MAX || lTestVal == LONG_MIN)) || (errno != 0 && lTestVal == 0)){
  56.     perror("strol");
  57.     exit(EXIT_FAILURE);
  58. }
  59. if(*endptr != '\0'){
  60.     printf("Further character after number: %s\n", endptr);
  61.     exit(EXIT_SUCCESS);
  62. }    
  63. return;
  64. }
  65.  
I got the implimentation from man strtol and I've read that man page like 10 times but 2 problems exist.

1)It always segment faults
2)It is implimented correctly

Anyhelp would be appreached

Thanks

DOCMUR
Sep 27 '07 #1
Share this Question
Share on Google+
3 Replies


sicarie
Expert Mod 2.5K+
P: 4,677
Moving to C++ Forum (Please double check where you are next time. Thanks!)
Sep 27 '07 #2

P: 2
????Why the code is C not C++
Sep 27 '07 #3

sicarie
Expert Mod 2.5K+
P: 4,677
????Why the code is C not C++
Because you posted in the Articles forum, and the Proper forum name is C/C++ (we do both).
Sep 27 '07 #4

Post your reply

Sign in to post your reply or Sign up for a free account.