Is there a code that means an integer is empty?
eg.
int a;
cin>>a;
//if a is empty
cout<<"Nothing is input.";
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.
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.
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.
So is it impossible to write code like this?
int a;
cin>>a;
//if a is empty
cout<<"Nothing is input.";
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Ž
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.
Originally Posted by SlyMaelstromI 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).Code:int n; cin >> n; cout << n;
Basically it's a check for valid (integer) input.
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
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;
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.Originally Posted by Dweia
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.Originally Posted by Dweia
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.Originally Posted by Roy01
...and incase you missed my signature the first time. USE CODE TAGS!!!!!
Sent from my iPadŽ
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.
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).
Sorry, I understood. I just had problem with expressing myself. Now I got it.Originally Posted by SlyMaelstrom
Thank you all of you.