Thread: integer problems

  1. #1
    Registered User
    Join Date
    Feb 2003
    Posts
    15

    Exclamation integer problems

    hi, I would like my program to nottice something, like a letter, and give an error, right now its just transforming it to an int, a very large one, and using the wrong error msg...here is my code

    #include <stdio.h>




    unsigned int getInputData(){
    unsigned int digit;
    printf("Please Enter in a positive number, then hit ENTER\n");
    scanf("%d", &digit);

    printf("The number you entered is: %d\n", digit);

    if (sizeof(digit) > 2)
    printf("Fatal error: Integer size too large\n");
    else if (digit==0)
    printf("That is not an Integer");
    else
    printf("Good, you've entered an integer!");
    return(digit);
    }

    int main(void){

    getInputData();
    return(0);
    }

    so like say when i run it, I type in 'hello' instead of an int, first it tells me i entered soemthing like
    The number you entered is: -1073743180

    then it gives me the integer too large error, but I want it to give me the other error...please help me!
    Last edited by vanilly; 02-13-2003 at 12:23 PM.
    thanks eveyone!
    from Mel

  2. #2
    ....
    Join Date
    Aug 2001
    Location
    Groningen (NL)
    Posts
    2,380
    The function sizeof() returns the number of bytes a datatype takes. You could input a string and then use the function isdigit(), include ctypes.h for this, to check if each character is a digit. In that case you can convert the string to int, using function atoi() and then use the number you got.

  3. #3
    Registered User
    Join Date
    Feb 2003
    Posts
    15

    RE:

    well, see I can't use strings or arrays i can only use the usual I/O method functions to read single characters only
    thanks eveyone!
    from Mel

  4. #4
    Registered User Cela's Avatar
    Join Date
    Jan 2003
    Posts
    362
    Oh, that's what you wanted to do :-)
    Code:
    #include <stdio.h>
    #include <string.h>
    #include <stdlib.h>
    #include <errno.h>
    #include <limits.h>
    
    unsigned int getInputData(){
      char *p;
      char *end;
      char buff[25];
      unsigned long digit;
    
      printf("Please Enter in a positive number, then hit ENTER\n");
      if (fgets(buff, sizeof(buff), stdin) == 0)
      {
        printf("Fatal error: No input\n");
        return 0;
      }
    
      if ((p = strrchr(buff, '\n')) != 0)
        /* Remove the newline */
      {
        *p = '\0';
      }
    
      errno = 0;
      digit = strtoul(buff, &end, 0);
      
      if (errno == ERANGE)
        printf("Fatal error: Out of range\n");
      else if (end == buff || end < buff+strlen(buff))
        printf("Not an integer\n");
      else
        printf("Good, you've entered an integer!\n");
    
      return(digit);
    }
    
    int main(void){
      getInputData();
    
      return(0);
    }
    *Cela*

  5. #5
    Casual Visitor
    Join Date
    Oct 2001
    Posts
    350

    Re: integer problems



    so like say when i run it, I type in 'hello' instead of an int, first it tells me i entered soemthing like
    The number you entered is: -1073743180

    then it gives me the integer too large error, but I want it to give me the other error...please help me!
    You should test the input for an invalid data type error before testing anything since the program has gone loopy

    Code:
    #include <stdio.h>
    #include <limits.h>
    
    int main( void )
    {
      int num1;
    	
      printf("Enter a string or a number: ");
    	
      if(scanf("%i", &num1) != 1)
      {
        printf("I know that I said enter a string...");
        return -1; /* input stream error */
      }	
    	
      else if(num1 > SHRT_MAX)
      {
        printf("Whoa that's a big number... oops");
        return -1;
      }
    	
      printf("Good you didn't fall for it as you entered %i\n"
                 , num1);
      
      return 0;
    }
    [edit]Doh Cela...... explained it better
    Last edited by ronin; 02-13-2003 at 12:42 PM.
    I haven't used a compiler in ages, so please be gentle as I try to reacclimate myself. :P

  6. #6
    Registered User
    Join Date
    Feb 2003
    Posts
    15

    hmm,....

    I can't really use anything i haven't learned yet, so i can't implement limits.h, and use the SZE_Max or whatever it was...I was told to use the SizeOf function, basically, the getInputData function gets characters one at a time and if the character is a digit then the result should be stored in an "int". if there are illegal characters or if the integer is too large, then the remaining characters are discarded until a carriage return is found...and the user should then be prompted again...
    thanks eveyone!
    from Mel

  7. #7
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    You can't use sizeof to determine to contents of a variable, as its for telling you the size of the variable itself.

    To do what you ask, you'll need to check the return code from scanf(). If it isn't 1, then use this loop to empty the input buffer, then prompt and read another int.
    >while (getchar() != '\n');
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  8. #8
    Registered User
    Join Date
    Feb 2003
    Posts
    15

    like this?

    #include <stdio.h>




    unsigned int getInputData(){
    unsigned int digit;
    printf("Please Enter in a positive number, then hit ENTER\n");



    if (scanf("%d", &digit) !=1){
    printf("That is not an integer\n");
    return(-1);}
    else if (sizeOf(digit)>4){
    printf("That integer is way too large!");
    return(-1);}
    else
    printf("Good, you've entered an integer!");
    printf("The number you entered is: %d\n", digit);
    return(digit);
    }

    int main(void){

    getInputData();
    return(0);
    }
    thanks eveyone!
    from Mel

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Link List math
    By t014y in forum C Programming
    Replies: 17
    Last Post: 02-20-2009, 06:55 PM
  2. No clue how to make a code to solve problems!
    By ctnzn in forum C Programming
    Replies: 8
    Last Post: 10-16-2008, 02:59 AM
  3. Replies: 4
    Last Post: 08-24-2007, 05:28 PM
  4. Replies: 7
    Last Post: 08-19-2007, 08:10 AM