Go4Expert

Go4Expert (http://www.go4expert.com/)
-   C (http://www.go4expert.com/forums/c/)
-   -   Checking input from program (http://www.go4expert.com/forums/checking-input-program-t6553/)

docmur 27Sep2007 05:40

Checking input from program
 
Okay here's the problem I can't seem to figure out. I have a program called Calc, and the user enter a number on the command line such as 239.43. The inputed number gets stored in char *argv[] making, the 239.43 stored in argv[1] I need to test that for a decimal point so I can determine is it's a float or int. I just cant think of how to test it to check

shabbir 27Sep2007 10:22

Re: Checking input from program
 
Check for . in the string.

docmur 27Sep2007 20:24

Re: Checking input from program
 
Okay I tried that and it didn't work I'll post my code
Code:

#include<stdio.h>

#include<string.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 iLoop;

    //Run the Loop though until the end of the string is located
    for(iLoop = 0; *(szString + iLoop) != '\0' ; iLoop++){

              if(*(szString + iLoop) != chr){

                            printf("Value inputed is float\n");

              }

              else

                            printf("Value inputed is int\n");

    }

//Return the array when the decimal is found
return (szString + iLoop);

}

When I run ./calc 200 + 200 or ./calc 200.20 + 200 no matter what I get return it's a float.

shabbir 27Sep2007 21:13

Re: Checking input from program
 
You should be getting what you are getting because you should not be printing in every step what the number is but you should be going through the complete string and set a flag if you find the "." ( without quote ) and if flag is set it should be float or else not.


All times are GMT +5.5. The time now is 10:42.