Thread: Isspace ??? Anyone Know?

  1. #1
    Registered User
    Join Date
    Sep 2002
    Posts
    53

    Isspace ??? Anyone Know?

    123
    Last edited by justin69enoch; 01-25-2003 at 08:49 PM.

  2. #2
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    >>I get a double value from another function
    >>and return a value of 1 if it is a double.
    Eh?
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  3. #3
    Registered User
    Join Date
    Sep 2002
    Posts
    53

    RE

    In my program main() not posted due to length... it calls several functions that in turn call functions.

    This is a small portion of my last function to write. ANyway, the main program calls this function, yet passes it either a double or an integer depending on the input from the main function. The variable name is temp. This function has to determine if the input has a decimal place in it or not and if it does, it returns a -1; if however it is an integer, it should return a 1.

    Does that help??... or even make sense?

  4. #4
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    There is no function overloading in C, so this is as close as you get without using a structure or a void pointer.
    Code:
    int function( int i, float f, int status )
    {
        if( status != 0 )
        {
            /* use the integer */
            return 1;
        }
        else
        {
            /* use the float */
        }
        return 0;
    }
    Call it:

    function ( 0, myFloat, 0 ); /*use a float */
    function( myInt, 0.0, 1 ); /* use an int */

    Quzah.
    Hope is the first step on the road to disappointment.

  5. #5
    Registered User
    Join Date
    Sep 2002
    Posts
    53

    RE.

    I won't know if it is an integer or a float until i run the function. This function has to determine what it is.

    Thanks for the quick responses thus far though. I know I haven't been entirely clear.

  6. #6
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    >>This function has to determine what it is.
    Like quzah said, the function can't determine that, you have to tell it up front. Thats what the parameter types are for.
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  7. #7
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    How can you not know what you're processing? Surely you're getting the value from some place. If it's user input, validate it first, then pass the correct data to the appropriate function. You can't just not know what you're dealing with and expect it to work.

    Example: I have a data file that contains 300 bytes of information. What is it? See how impossible that question is? You have to tell it what you're reading. Bytes are just bytes. They can "be" anything you want. It doesn't mean the values are accurate, just that you can access the bytes of data.

    Quzah.
    Hope is the first step on the road to disappointment.

  8. #8
    Registered User
    Join Date
    Sep 2002
    Posts
    53
    The user will input a number. if it is a float, I need to reject the input and tell them that it is invalid; that is basically what I am doing. But the thing is, if I just limit integers from 0 - 500, and someone enters 10.3, it will simply round it to 10 and store 10 as a value rather than reporting an error. ??? any suggestions?

  9. #9
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    Originally posted by justin69enoch
    The user will input a number. if it is a float, I need to reject the input and tell them that it is invalid; that is basically what I am doing. But the thing is, if I just limit integers from 0 - 500, and someone enters 10.3, it will simply round it to 10 and store 10 as a value rather than reporting an error. ??? any suggestions?
    Then your answer is simple. You have to read the input from the keyboard with fgets(), into a char array. Then you perform you own custom validation on the data to ensure its what you expected. Or maybe this function will help you do some of the work too.
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  10. #10
    Registered User
    Join Date
    Sep 2002
    Posts
    53

    Current Code

    Okay, this is what I have so far... It only accepts one char per enter... If i use fgets, won't all it do is store the string into one allocated spot in an array? If so, how would I be able to verify that a '.' is not in the string itself?

    I know this is a stupid thing for you guys, but I am really trying to learn this stuff. All help is appreciated and again, i thank you for the quick responses. This forum is really great to get help.

    Code:
    #define SIZE 2
    
    int main()  {
      char A[SIZE];
      char text; 
      int sum,count=0;  
    
      printf("Enter Data: "); 
      scanf(" %c", &text);
      
      while (count<SIZE)  {
         if (text != '.')  {
           A[count]=text;
           printf("Checksum:  %c  %d\n\n",sum,sum);
           scanf(" %c", &text);
           count++;
         }
         else  {
           printf("this is an error!\n"
           break; \
         }
      }
    
    }
    Last edited by justin69enoch; 01-22-2003 at 09:34 PM.

  11. #11
    Registered User Bajanine's Avatar
    Join Date
    Dec 2001
    Location
    The most peaks over 10,000 feet!
    Posts
    396

    Hammer told you what u need 2 do!

    Loop through your char array and use isdigit to determine if the input is a integer.

  12. #12
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    Try doing a board search, you never know what you might find
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  13. #13
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    Code:
    #include <ctype.h>
    #include <stdio.h>
    #include <math.h>
    
    int is_float(double num);
    
    int main(void)
    {
    double temp;
    
    printf("Enter a integer: ");
    scanf("%lf",&temp);
    while (is_float(temp))
    {
       printf("Invalid input.  Please enter an integer: ");
       scanf("%lf",&temp);
    }
    
    return 0;
    }
    
    int is_float(double num)
    {
       double ipart;
       if (modf(num,&ipart) > 0.)
          return 1;
       return 0;
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. for and isspace
    By izzy in forum C Programming
    Replies: 2
    Last Post: 03-15-2009, 06:18 PM
  2. pointer with isspace()
    By aze in forum C Programming
    Replies: 6
    Last Post: 04-17-2004, 09:43 PM
  3. isspace() is the way!
    By Luigi in forum C++ Programming
    Replies: 3
    Last Post: 02-02-2003, 10:23 AM
  4. Need help.
    By geetee in forum C Programming
    Replies: 6
    Last Post: 10-16-2001, 09:51 PM