Thread: Is there a code that means a integer is empty?

  1. #1
    Registered User
    Join Date
    Oct 2006
    Posts
    50

    Is there a code that means a integer is empty?

    Is there a code that means an integer is empty?

    eg.

    int a;
    cin>>a;

    //if a is empty
    cout<<"Nothing is input.";
    Last edited by Roy01; 10-19-2006 at 12:02 AM.

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    There is no integer equivalent to say a pointer being NULL
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    Can you define empty?

    int foo;

    That's a variable that doesn't hold a value, but lint will complain that it's a variable undefined. You will need the integer to contain something before you attempt to use it in an expression.

  4. #4
    Registered User
    Join Date
    Oct 2006
    Posts
    50
    So is it impossible to write code like this?

    int a;
    cin>>a;

    //if a is empty
    cout<<"Nothing is input.";

  5. #5
    Devil's Advocate SlyMaelstrom's Avatar
    Join Date
    May 2004
    Location
    Out of scope
    Posts
    4,079
    Well... in reality, that >> operator isn't going to resolve until a value is read into a or the stream fails, so to check if a is empty at that point is very much pointless. As others said, however... an integer is never really empty. Even at the point of declaration, it still has some garbage value in it, so no, you can't check if an integer is empty.
    Sent from my iPadŽ

  6. #6
    Cat Lover
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    109
    cin into a string instead, which you can check for emptiness, then convert the string to an integer.

    citizen: When you declare a variable without initialising it e.g. int n; it basically contains a random value.
    Your compiler will warn or give you an error because it's a bad idea to use a variable without defining it (foo = bar), because you don't know what you'll get. It does that through looking at your code and seeing you have x = n before n = a, not through checking the contents of variable a for a == undefined, which would be a run-time check anyway.

  7. #7
    Cat Lover
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    109
    Quote Originally Posted by SlyMaelstrom
    Well... in reality, that >> operator isn't going to resolve until a value is read into a or the stream fails, so to check if a is empty at that point is very much pointless. As others said, however... an integer is never really empty. Even at the point of declaration, it still has some garbage value in it, so no, you can't check if an integer is empty.
    Code:
    int n;
    cin >> n;
    cout << n;
    I enter 'foo' when it asks for input, the cin fails, nothing is written to n, it is 'empty' (aside from the garbage value from not initialising it).
    Basically it's a check for valid (integer) input.

  8. #8
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    Anyone and everyone who mentions my post:
    I've read all that, and even disclaimed its use. Leave me alone.

    This thread, it turns out, needed one answer. Grab a string from the user and convert that, and keep trying ntil you succeed.
    http://faq.cprogramming.com/cgi-bin/...&id=1043284385

  9. #9
    Registered User
    Join Date
    Oct 2006
    Posts
    50
    How about this?

    int a,b,c;
    cout<<"Input 3 value:";
    cin>>a>>b>>c;

    //if a,b,c are valid
    cout<<a+b+c;

    //if c is empty
    cout<<a+b;

  10. #10
    The larch
    Join Date
    May 2006
    Posts
    3,573
    Quote Originally Posted by Dweia
    Code:
    int n;
    cin >> n;
    cout << n;
    I enter 'foo' when it asks for input, the cin fails, nothing is written to n, it is 'empty' (aside from the garbage value from not initialising it).
    Basically it's a check for valid (integer) input.
    if (cin >> n) and while (cin >> n) are checks for valid integer input. If you continue using n = "foo" you have a variable with garbage value and an input stream that's in a failed state and not usable any more.

  11. #11
    Devil's Advocate SlyMaelstrom's Avatar
    Join Date
    May 2004
    Location
    Out of scope
    Posts
    4,079
    Quote Originally Posted by Dweia
    I enter 'foo' when it asks for input, the cin fails, nothing is written to n, it is 'empty' (aside from the garbage value from not initialising it).
    Basically it's a check for valid (integer) input.
    Yes, I said that. However, what's the point of checking if an integer is "empty" if the stream failed? Just going to clear it and keep looping until it doesn't fail? Anyway, the point is, you can't have an "empty" integer, so this whole concept is pointless.

    Quote Originally Posted by Roy01
    //if c is empty
    cout<<a+b;
    What part of "No, you can't do it" don't you understand? As Dweia said in his/her first post. Read in a string and parse it into however many integers you have.

    ...and incase you missed my signature the first time. USE CODE TAGS!!!!!
    Sent from my iPadŽ

  12. #12
    Registered User
    Join Date
    May 2003
    Posts
    1,619
    Well, you can accomplish the effect you want by checking if the input stream is in a failed state or not; if it failed, then no integer was read, but the variable is not "empty", it has some value, albeit a random and unpredictable one.

    An int is just a value in memory; it always has SOME value because each bit of memory holds either 1 or 0.

    What you really want to know is not "can you tell if an int is empty" which makes no sense, but rather "can you tell if reading an int succeeded or failed" which makes sense and is easy to do.
    You ever try a pink golf ball, Wally? Why, the wind shear on a pink ball alone can take the head clean off a 90 pound midget at 300 yards.

  13. #13
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    The problem with checking for a failed input stream is that (as has already been said) cin will wait until something has been input. If the user hits enter without typing anything else, cin will not fail but will still sit there waiting for input. It is possible that using operator>> with noskipws might work in causing the stream to fail on enter.

    Reading string input with getline will allow the user to hit enter and have cin read it immediately with no fail state. That string can be put into a stringstream for conversion to int(s).

  14. #14
    Registered User
    Join Date
    Oct 2006
    Posts
    50
    Quote Originally Posted by SlyMaelstrom
    What part of "No, you can't do it" don't you understand? As Dweia said in his/her first post. Read in a string and parse it into however many integers you have.

    ...and incase you missed my signature the first time. USE CODE TAGS!!!!!
    Sorry, I understood. I just had problem with expressing myself. Now I got it.

    Thank you all of you.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Obfuscated Code Contest
    By Stack Overflow in forum Contests Board
    Replies: 51
    Last Post: 01-21-2005, 04:17 PM
  2. Updated sound engine code
    By VirtualAce in forum Game Programming
    Replies: 8
    Last Post: 11-18-2004, 12:38 PM
  3. Help with homework please
    By vleonte in forum C Programming
    Replies: 20
    Last Post: 10-13-2003, 11:16 AM
  4. Can someone explain to me what this code means
    By Shadow12345 in forum C++ Programming
    Replies: 3
    Last Post: 12-22-2002, 12:36 PM
  5. Interface Question
    By smog890 in forum C Programming
    Replies: 11
    Last Post: 06-03-2002, 05:06 PM