Checking input from program

Discussion in 'C' started by docmur, Sep 27, 2007.

  1. docmur

    docmur New Member

    Joined:
    Sep 27, 2007
    Messages:
    3
    Likes Received:
    0
    Trophy Points:
    0
    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
     
  2. shabbir

    shabbir Administrator Staff Member

    Joined:
    Jul 12, 2004
    Messages:
    15,375
    Likes Received:
    388
    Trophy Points:
    83
    Check for . in the string.
     
  3. docmur

    docmur New Member

    Joined:
    Sep 27, 2007
    Messages:
    3
    Likes Received:
    0
    Trophy Points:
    0
    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.
     
    Last edited by a moderator: Sep 27, 2007
  4. shabbir

    shabbir Administrator Staff Member

    Joined:
    Jul 12, 2004
    Messages:
    15,375
    Likes Received:
    388
    Trophy Points:
    83
    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.
     

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice