Author |
Message |
boogerlad
|
Posted: Wed Jan 04, 2012 6:48 pm Post subject: cin error returning |
|
|
Let's just say that you have
code: | int x = 5;
cin >> x;
cout << x;
|
and say you input "derp"
Why does 0 get printed? Since it failed, shouldn't it not modify x at all? Or is it because it fails, its return code is false, which is a zero? |
|
|
|
|
|
Sponsor Sponsor
|
|
|
Insectoid
|
Posted: Wed Jan 04, 2012 7:34 pm Post subject: RE:cin error returning |
|
|
C++ won't return an error when this happens. It will just continue running. It appears to not change the RAM if this happens, in which case, it will output the last value of x or in this case, whatever the last program to use this particular bit of ram set it to. A lot of RAM is zeros, in my experience, but if you do this enough you may end up with other values. |
|
|
|
|
|
boogerlad
|
Posted: Wed Jan 04, 2012 8:07 pm Post subject: RE:cin error returning |
|
|
but wouldn't the last value of x be 5, because the cin failed? |
|
|
|
|
|
Insectoid
|
Posted: Wed Jan 04, 2012 8:29 pm Post subject: RE:cin error returning |
|
|
Oh, I didn't see that you had that. On my computer, the output is still 5. This isn't standardized behavior. The output may depend on your architecture, OS or compiler. |
|
|
|
|
|
boogerlad
|
Posted: Wed Jan 04, 2012 8:33 pm Post subject: RE:cin error returning |
|
|
I'm beginning to hate gcc for all these little quirks. I'm going to try with a different compiler, and see what happens. |
|
|
|
|
|
Insectoid
|
Posted: Wed Jan 04, 2012 8:35 pm Post subject: RE:cin error returning |
|
|
I suggest you keep using gcc. It's the industry standard for linux/mac. These aren't 'quirks'. These are things that just don't matter. Why do you care what happens if you try to store a string into an integer? That should never happen in the first place. Odds are, most other compilers will do the same thing, or something even more silly. |
|
|
|
|
|
boogerlad
|
Posted: Wed Jan 04, 2012 8:43 pm Post subject: RE:cin error returning |
|
|
I switched to tiny c compiler, and I got a zero instead of a five. Same result as gcc. What compiler/os/cpu are you using? I know it's a miniscule thing, but say that you've been doing multiple operations to a variable, and then if a read fails, you want the variable to maintain its value, not a zero? |
|
|
|
|
|
Insectoid
|
Posted: Wed Jan 04, 2012 8:49 pm Post subject: RE:cin error returning |
|
|
I'm using gcc on OSX with an x86-64 cpu. Ideally, you'll write your code so that a read cannot fail (for example, read into a char array, check if it's an integer, and if it is, assign it to your int variable. Otherwise throw an error message and ask for input again). |
|
|
|
|
|
Sponsor Sponsor
|
|
|
boogerlad
|
Posted: Wed Jan 04, 2012 8:54 pm Post subject: RE:cin error returning |
|
|
well, I do have proper error handling code, to check for ints. code: | int myNumber = 0;
while (true) {
cout << "Please enter a valid number: ";
getline(cin, input);
stringstream myStream(input);
if (myStream >> myNumber)
break;
cout << "Invalid number, please try again" << endl;
}
cout << "You entered: " << myNumber << endl << endl;
|
but I'm just curious. Even with proper error handling, the variable will still be set to zero until proper input is given. |
|
|
|
|
|
boogerlad
|
Posted: Wed Jan 04, 2012 10:43 pm Post subject: RE:cin error returning |
|
|
in msvc++ 2010, the program just crashes lol. Now that I know there is no standard, I'll stop worrying about it. =) |
|
|
|
|
|
[Gandalf]
|
Posted: Thu Jan 05, 2012 6:59 pm Post subject: RE:cin error returning |
|
|
Behaviour like this is usually called "undefined", and C++ is FULL of situations that may cause undefined behaviour. The key is to avoid those situations. |
|
|
|
|
|
OneOffDriveByPoster
|
Posted: Fri Jan 06, 2012 12:19 pm Post subject: Re: RE:cin error returning |
|
|
boogerlad @ Wed Jan 04, 2012 10:43 pm wrote: in msvc++ 2010, the program just crashes lol. Now that I know there is no standard, I'll stop worrying about it. =)
There _is_ a Standard.
C++03 subclause 22.2.2.1.2 [lib.facet.num.get.virtuals] paragraph 1:
If an error occurs, val is unchanged; otherwise it is set to the resulting value.
Which is superceded only recently by C++2011 where the expected result is indeed 0. |
|
|
|
|
|
Velocity
|
Posted: Mon Jan 23, 2012 12:19 pm Post subject: RE:cin error returning |
|
|
you cant return the same value that you recieved without checking a new pub |
|
|
|
|
|
QuantumPhysics
|
Posted: Tue May 29, 2012 11:18 am Post subject: RE:cin error returning |
|
|
Why would you need to use string handling anyways you could just use the basic iostream... there is no need to call #include <string> it is useless unless your trying to figure out some sort of cipher encryption or decryption |
|
|
|
|
|
mirhagk
|
Posted: Tue May 29, 2012 2:08 pm Post subject: RE:cin error returning |
|
|
@QuantumPhysics that statement isn't even related to what was being talked about (noone mentioned strings) and including <string> gives you a string library that many API's expect (and is usually a lot easier to work with). Plus your necro-ing a topic that is from January, so the original posters likely won't ever see what your saying (I know velocity won't, he's banned I'm pretty sure) |
|
|
|
|
|
|