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
- #include<stdio.h>
- #include<stdlib.h>
- #include<string.h>
- #include<errno.h>
- #include<limits.h>
- char *FindChr(char *szString, char chr);
- int main(int argc, char *argv[])
- {
- float fValueOneInputed;
- float fValueTwoInputed;
- float fCalculatorStore;
- //Test the input to see if the value entered if Float or Int
- FindChr(argv, '.');
- //Get the first input value in char form and take it to a float
- sscanf(argv[1],"%f",&fValueOneInputed);
- //Get the first input value in char form and take it to a float
- sscanf(argv[3],"%f",&fValueTwoInputed);
- if(*argv[2] == '+')printf("%0.3f", fCalculatorStore = fValueOneInputed + fValueTwoInputed);
- if(*argv[2] == '-')printf("%0.3f", fCalculatorStore = fValueOneInputed - fValueTwoInputed);
- if(*argv[2] == 'x')printf("%0.3f", fCalculatorStore = fValueOneInputed * fValueTwoInputed);
- if(*argv[2] == '/')printf("%0.3f", fCalculatorStore = fValueOneInputed / fValueTwoInputed);
- return 0;
- }
- char *FindChr(char *szString, char chr)
- {
- int base;
- int iLoop;
- long lTestVal;
- char *str;
- char *endptr;
- str = szString[1];
- base = 0;
- errno = 0;
- lTestVal = strtol(str, &endptr, base);
- /* Check for Error's */
- if((errno == ERANGE && (lTestVal == LONG_MAX || lTestVal == LONG_MIN)) || (errno != 0 && lTestVal == 0)){
- perror("strol");
- exit(EXIT_FAILURE);
- }
- if(*endptr != '\0'){
- printf("Further character after number: %s\n", endptr);
- exit(EXIT_SUCCESS);
- }
- return;
- }
1)It always segment faults
2)It is implimented correctly
Anyhelp would be appreached
Thanks
DOCMUR