This seems to be weird:

int main(int argc, char* argv[]) {  

        cout << "function main() .." << '\n';  

        char ch = 0;  
        double number_value=1.1;  

        cin >> ch;  
        cin.putback(ch);  

        cin >> number_value;  
        cout << "1 .. " << " " << cin.good() << " " << number_value << '\n';  
        cin >> number_value;  
        cout << "2 .. " << " " << cin.good() << " " << number_value << '\n';  

        return 0;  

}

if i input the following:
7a 1

i get the following:

function main() ..
7a 1
1 .. 1 7
2 .. 0 0

i understand the:
1 .. 1 7
but why the variable number_value is 0.
The 2nd cin.good() shows failure (0) so nothing would have read and the value in number_value from the previous assignment would remain. I expect the value of 7.

Recommended Answers

All 5 Replies

In VC++ 2008 on XP, I get the result you expect; the 7 value persists in number_value.

[added] And just now in g++ in Fedora 10 I replicated your result.

>but why the variable number_value is 0.
>I expect the value of 7.

Deep under the hood (for g++), number_value is getting set to 0.0 because the "a" is read as a floating-point value and fails to be converted by strtod, which returns 0 if no conversion can be made. Essentially g++ uses a reference to number_value as the working result throughout the process.

On the other hand (to explain vmanes' results), Visual Studio uses a temporary local variable to perform the conversion and only sets the reference to number_value on a successful conversion.

In this case, Visual Studio is correct and g++ is not because these conversions are performed through the num_get facet, which is required to leave the value reference unchanged on failure. g++ doesn't meet that requirement.

yapkm01, it would help to know what compiler you're using. For that lack, my answers are based on the two compilers vmanes tested with.

commented: Nice explanation of the behind the scenes actions +5

>but why the variable number_value is 0.
>I expect the value of 7.

Deep under the hood (for g++), number_value is getting set to 0.0 because the "a" is read as a floating-point value and fails to be converted by strtod, which returns 0 if no conversion can be made. Essentially g++ uses a reference to number_value as the working result throughout the process.

On the other hand (to explain vmanes' results), Visual Studio uses a temporary local variable to perform the conversion and only sets the reference to number_value on a successful conversion.

In this case, Visual Studio is correct and g++ is not because these conversions are performed through the num_get facet, which is required to leave the value reference unchanged on failure. g++ doesn't meet that requirement.

yapkm01, it would help to know what compiler you're using. For that lack, my answers are based on the two compilers vmanes tested with.

------------------------------------------------------

I am using g++ compiler on ubuntu.
If 7a1 failed due to "a" not being converted and hence 0 being displayed..
Look at the following input i use below:

function main() ..
7a1
1 .. 1 7
2 .. 0 0
yapkm01@yapkm01-desktop:~/C++_BjarneStroustrup/exercises$ ./DeskCalculator
function main() ..
7*1
1 .. 1 7
2 .. 0 0
yapkm01@yapkm01-desktop:~/C++_BjarneStroustrup/exercises$ ./DeskCalculator
function main() ..
7+1
1 .. 1 7
2 .. 1 1

Why "*" or "+" gives different results? Weird? Maybe that compiler has lots of bugs in it?

7+1

You can have a leading + or - to indicate positive or negative value. The + is always implied when no sign is present, but it's considered part of the 1.

I can't understand why the variable value is Zero.
It is not required.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.