why won't this code compile?

This is a discussion on why won't this code compile? within the C Programming forums, part of the General Programming Boards category; #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. #1
    Registered User
    Join Date
    Feb 2003
    Posts
    15

    why won't this code compile?

    #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);
    }

    I get the following errors

    /tmp/ccKpU3tT.o: In function `getInputData':
    /tmp/ccKpU3tT.o(.text+0x63): undefined reference to `sizeOf'
    collect2: ld returned 1 exit status

    I dont' get it...please help!
    thanks eveyone!
    from Mel

  2. #2
    Mayor of Awesometown Govtcheez's Avatar
    Join Date
    Aug 2001
    Location
    MI
    Posts
    8,825
    It's sizeof, not sizeOf

    Also, in the future please use code tags.

  3. #3
    Registered User
    Join Date
    Feb 2003
    Posts
    15
    thanks, but its still not doign what i'd like

    Please Enter in a positive number, then hit ENTER
    dg
    That is not an integer
    [root@CPE0010b53e159e-CM root]# ./a.out
    Please Enter in a positive number, then hit ENTER
    5429764297625321
    Good, you've entered an integer!The number you entered is: 2147483647
    [root@CPE0010b53e159e-CM root]#

    see?
    how come its doing that weird thing, I just want it to see that its too big, then say it, and re-ask


    I've fixed the re-ask part, with a for loop (I think)
    Code:
    #include <stdio.h>
    
    
    
    
    unsigned int getInputData(){
     unsigned int digit;
     int i;
     for(i=0;i<20;i++){
      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){
        return(-1);}
       else 
        printf("The number you entered is: %d\n", digit);
        return(digit);}
    }
    
    int main(void){
    
    getInputData();
    return(0);
    }
    how do I use the tags?
    Last edited by vanilly; 02-13-2003 at 01:47 PM.
    thanks eveyone!
    from Mel

  4. #4
    Mayor of Awesometown Govtcheez's Avatar
    Join Date
    Aug 2001
    Location
    MI
    Posts
    8,825
    Oh, next time I'll look through the code more thoroughly.

    You don't need "sizeof(digit)" to get digit's value. Just use "digit" instead.

    As for the code tags, there's an announcement in this forum about it, as well as being in the board faq.

  5. #5
    Registered User
    Join Date
    Feb 2003
    Posts
    15
    I do need the sizeof, because, i have to compare its size in bytes, so that if its over the max bytes in an int, it will give the error msg...
    thanks eveyone!
    from Mel

  6. #6
    Mayor of Awesometown Govtcheez's Avatar
    Join Date
    Aug 2001
    Location
    MI
    Posts
    8,825
    But you're still going about it wrong. sizeof(int) will always return the same number, whether the number itself is too big or not.

  7. #7
    Registered User Cela's Avatar
    Join Date
    Jan 2003
    Posts
    362
    This sounds like an overly difficult assignment to me, the best approximation I can come up with while maintaining the restrictions you have is this
    Code:
    #include <stdio.h>
    
    int get_int(void)
    {
      unsigned long digit = 0;
    
      printf("Enter an integer: ");
    
      while (1)
      {
        if (
           scanf("%ld", &digit) == 1 &&
           digit < (2 << (sizeof(int) * 8-2)) /* Assume octets */
           )
        {
          break;
        }
    
        printf("Invalid integer, try again: ");
        while (getchar() != '\n')
        {}
      }
    
      return (int)digit;
    }
    
    int main(void)
    {
      printf("%d\n", get_int());
    
      return 0;
    }
    *Cela*

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

    thanks

    thank you very much, however, the only thing that still doesn't work with that code, is that if you enter in a really large number, it changes it

    Please Enter in a positive number, then hit ENTER
    dsc
    invalid integer, try again: fd543fe
    invalid integer, try again: 4325245235354325
    2147483647 is the number you have entered


    would u know how to fix this problem?
    thanks eveyone!
    from Mel

  9. #9
    Registered User Cela's Avatar
    Join Date
    Jan 2003
    Posts
    362
    >>would u know how to fix this problem?
    With your restrictions you can't. If you enter a really large value then it'll overflow the unsigned variable and it'll probably be in range.
    *Cela*

  10. #10
    Mayor of Awesometown Govtcheez's Avatar
    Join Date
    Aug 2001
    Location
    MI
    Posts
    8,825
    That number is the largest int your system can handle. Since that's all the space allocated for the number, it's rounded down to that. You could always input the number as a string, use atoi to change it to an integer for testing, and then reprint as a string, I guess.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Code won't compile
    By monkles in forum C Programming
    Replies: 3
    Last Post: 05-28-2009, 02:45 PM
  2. Compile Errors in my Code. Can anyone help?
    By DGLaurynP in forum C Programming
    Replies: 1
    Last Post: 10-06-2008, 10:36 AM
  3. This code won't compile in Red Hat Linux 9
    By array in forum Linux Programming
    Replies: 7
    Last Post: 04-27-2004, 07:30 AM
  4. << !! Posting Code? Read this First !! >>
    By kermi3 in forum Linux Programming
    Replies: 0
    Last Post: 10-14-2002, 02:30 PM
  5. How do they compile code for an OS ?
    By Nutshell in forum A Brief History of Cprogramming.com
    Replies: 49
    Last Post: 03-28-2002, 12:16 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21