Thread: Checking for negatives in an unsigned variable

  1. #1
    Registered User
    Join Date
    Jan 2008
    Posts
    182

    Checking for negatives in an unsigned variable

    I wrote a program that uses an unsigned long long as storage for the user input. Even though my program tells the user NOT to input negative numbers, I am extremely sure that it would be the first thing he/she will try. Also, I can't accept floating point numbers (duh). So, I need to show an error message somehow if the user inputs a negative number, instead of using the undefined number that will appear. My question is, how can I check for negative input or floating point input if I want to store the whole value to an unsigned long long? I read the user input like this:

    Code:
    scanf("%I64u",&var);
    And after that, well, I can't check if the number is negative or floating point because what the variable contains is a positive piece of junk. I thought I could also use atoi() or atof(), but that is only for integers or floats and I need full unsigned long long!

    Thanks

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    If you need to do input verification, you can't read into the variable itself. Get a string (with fgets or scanf or whatever) and verify.

  3. #3
    Registered User
    Join Date
    Jan 2008
    Posts
    182
    And after that, how do I pass the number to an unsigned long long?

  4. #4
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    You would do that as part of the verification process. Look at strtoull.

  5. #5
    Registered User
    Join Date
    Jan 2008
    Posts
    182
    Thanks very much. I didn't understand the 2nd argument of strtoull immediately, but I think I got it. I have to declare a char *pointer and pass the address of the pointer like this: &pointer, right?

    So something like this should work:
    Code:
    unsigned long long number;
    char input[32];
    char *endptr;
    
    scanf("%s", input);
    // here goes error checking
    
    number = strtoull(input, &endptr, 0);
    
    // do what I want with the number
    Thanks.

  6. #6
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Quote Originally Posted by samus250 View Post
    Thanks very much. I didn't understand the 2nd argument of strtoull immediately, but I think I got it. I have to declare a char *pointer and pass the address of the pointer like this: &pointer, right?

    Thanks.
    Right. And endptr will point at the "rest" of the number, so if endptr doesn't point to just "\n" or the like, that means there was extra stuff typed in at the end.

  7. #7
    Registered User
    Join Date
    Jan 2008
    Posts
    182
    Ok thanks, I understand completely now.

    So then I could check everything endptr has by making a loop that uses pointer notation like (*(enptr+x)) until NULL is met.

    Thanks a lot, I solved my problem! A very handy function strtoull is!

  8. #8
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Have a look at
    http://cpwiki.sourceforge.net/Buffer_overrun
    You're reading strings with scanf the wrong way.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  9. #9
    Registered User
    Join Date
    Jan 2008
    Posts
    182
    Thanks a lot Elysia. I read that wiki and all of it on your signature I'll change that now.

    Which one would you prefer? Using scanf("%63s",&var); or fgets(var, 64, stdin);? P.S., I noticed that you do not use the & in front of the variable in your scanf (the one on the single post). Wouldn't that cause compiler error?

  10. #10
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Arrays are passed as pointers by default, so using & on them would, in effect, take the address of the pointer, thus giving wrong results. So no, it's correct.
    I recommend the fgets method, though, seeing as you can just pass sizeof(buffer) instead of hard-coding numbers and it doesn't leave anything in the input buffer.
    Remember that fgets reads the newline, so you may have to get rid of it yourself.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 16
    Last Post: 10-29-2006, 05:04 AM
  2. checking variable for no value
    By DarkViper in forum C++ Programming
    Replies: 4
    Last Post: 01-08-2004, 11:37 PM
  3. Edit code please
    By bluehead in forum C++ Programming
    Replies: 3
    Last Post: 02-22-2002, 09:42 PM
  4. can someone check this out and let me know ?
    By javaz in forum C Programming
    Replies: 5
    Last Post: 01-21-2002, 02:13 PM
  5. ANY BODY WILLING TO HELP ME WITH Microsoft Visual C++
    By BiG pImPiN fOoL in forum C++ Programming
    Replies: 12
    Last Post: 11-04-2001, 06:03 PM