Does “cin” reset variable to some default value if input type differs from destination type? [duplicate]

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP











up vote
10
down vote

favorite













This question already has an answer here:



  • Why does stringstream >> change value of target on failure?

    2 answers



I have an issue with behavior of "cin" (I do not understand). My IDE is Netbeans under Windows OS (with Cygwin).



Here's a code example:



int main()

int temp = -1;
std::cin >> temp; // here user enters string of characters (string) or a single character

if (temp == 0)
std::cout << "temp = " << temp << ".n";
if (temp == -1)
std::cout << "temp = " << temp << ".n";

return 0;



This code shows message temp = 0 if I enter some sort of a character/string of characters. It's like there is conversion of char to int and conversion always ending by value 0.



Thank you if you can explain this behavior.







share|improve this question














marked as duplicate by NathanOliver c++
Users with the  c++ badge can single-handedly close c++ questions as duplicates and reopen them as needed.

StackExchange.ready(function()
if (StackExchange.options.isMobile) return;

$('.dupe-hammer-message-hover:not(.hover-bound)').each(function()
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');

$hover.hover(
function()
$hover.showInfoMessage('',
messageElement: $msg.clone().show(),
transient: false,
position: my: 'bottom left', at: 'top center', offsetTop: -7 ,
dismissable: false,
relativeToBody: true
);
,
function()
StackExchange.helpers.removeMessages();

);
);
);
Aug 16 at 13:41


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.














  • The input that std::cin reads is a stream of text. Beyond that, it does not have a type. The extractor (operator>>) converts that text into a value of the appropriate type if it can.
    – Pete Becker
    Aug 16 at 12:53















up vote
10
down vote

favorite













This question already has an answer here:



  • Why does stringstream >> change value of target on failure?

    2 answers



I have an issue with behavior of "cin" (I do not understand). My IDE is Netbeans under Windows OS (with Cygwin).



Here's a code example:



int main()

int temp = -1;
std::cin >> temp; // here user enters string of characters (string) or a single character

if (temp == 0)
std::cout << "temp = " << temp << ".n";
if (temp == -1)
std::cout << "temp = " << temp << ".n";

return 0;



This code shows message temp = 0 if I enter some sort of a character/string of characters. It's like there is conversion of char to int and conversion always ending by value 0.



Thank you if you can explain this behavior.







share|improve this question














marked as duplicate by NathanOliver c++
Users with the  c++ badge can single-handedly close c++ questions as duplicates and reopen them as needed.

StackExchange.ready(function()
if (StackExchange.options.isMobile) return;

$('.dupe-hammer-message-hover:not(.hover-bound)').each(function()
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');

$hover.hover(
function()
$hover.showInfoMessage('',
messageElement: $msg.clone().show(),
transient: false,
position: my: 'bottom left', at: 'top center', offsetTop: -7 ,
dismissable: false,
relativeToBody: true
);
,
function()
StackExchange.helpers.removeMessages();

);
);
);
Aug 16 at 13:41


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.














  • The input that std::cin reads is a stream of text. Beyond that, it does not have a type. The extractor (operator>>) converts that text into a value of the appropriate type if it can.
    – Pete Becker
    Aug 16 at 12:53













up vote
10
down vote

favorite









up vote
10
down vote

favorite












This question already has an answer here:



  • Why does stringstream >> change value of target on failure?

    2 answers



I have an issue with behavior of "cin" (I do not understand). My IDE is Netbeans under Windows OS (with Cygwin).



Here's a code example:



int main()

int temp = -1;
std::cin >> temp; // here user enters string of characters (string) or a single character

if (temp == 0)
std::cout << "temp = " << temp << ".n";
if (temp == -1)
std::cout << "temp = " << temp << ".n";

return 0;



This code shows message temp = 0 if I enter some sort of a character/string of characters. It's like there is conversion of char to int and conversion always ending by value 0.



Thank you if you can explain this behavior.







share|improve this question















This question already has an answer here:



  • Why does stringstream >> change value of target on failure?

    2 answers



I have an issue with behavior of "cin" (I do not understand). My IDE is Netbeans under Windows OS (with Cygwin).



Here's a code example:



int main()

int temp = -1;
std::cin >> temp; // here user enters string of characters (string) or a single character

if (temp == 0)
std::cout << "temp = " << temp << ".n";
if (temp == -1)
std::cout << "temp = " << temp << ".n";

return 0;



This code shows message temp = 0 if I enter some sort of a character/string of characters. It's like there is conversion of char to int and conversion always ending by value 0.



Thank you if you can explain this behavior.





This question already has an answer here:



  • Why does stringstream >> change value of target on failure?

    2 answers









share|improve this question













share|improve this question




share|improve this question








edited Aug 16 at 8:52









Toby Speight

14.7k133660




14.7k133660










asked Aug 16 at 6:59









RickSanch3z

676




676




marked as duplicate by NathanOliver c++
Users with the  c++ badge can single-handedly close c++ questions as duplicates and reopen them as needed.

StackExchange.ready(function()
if (StackExchange.options.isMobile) return;

$('.dupe-hammer-message-hover:not(.hover-bound)').each(function()
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');

$hover.hover(
function()
$hover.showInfoMessage('',
messageElement: $msg.clone().show(),
transient: false,
position: my: 'bottom left', at: 'top center', offsetTop: -7 ,
dismissable: false,
relativeToBody: true
);
,
function()
StackExchange.helpers.removeMessages();

);
);
);
Aug 16 at 13:41


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.






marked as duplicate by NathanOliver c++
Users with the  c++ badge can single-handedly close c++ questions as duplicates and reopen them as needed.

StackExchange.ready(function()
if (StackExchange.options.isMobile) return;

$('.dupe-hammer-message-hover:not(.hover-bound)').each(function()
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');

$hover.hover(
function()
$hover.showInfoMessage('',
messageElement: $msg.clone().show(),
transient: false,
position: my: 'bottom left', at: 'top center', offsetTop: -7 ,
dismissable: false,
relativeToBody: true
);
,
function()
StackExchange.helpers.removeMessages();

);
);
);
Aug 16 at 13:41


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.













  • The input that std::cin reads is a stream of text. Beyond that, it does not have a type. The extractor (operator>>) converts that text into a value of the appropriate type if it can.
    – Pete Becker
    Aug 16 at 12:53

















  • The input that std::cin reads is a stream of text. Beyond that, it does not have a type. The extractor (operator>>) converts that text into a value of the appropriate type if it can.
    – Pete Becker
    Aug 16 at 12:53
















The input that std::cin reads is a stream of text. Beyond that, it does not have a type. The extractor (operator>>) converts that text into a value of the appropriate type if it can.
– Pete Becker
Aug 16 at 12:53





The input that std::cin reads is a stream of text. Beyond that, it does not have a type. The extractor (operator>>) converts that text into a value of the appropriate type if it can.
– Pete Becker
Aug 16 at 12:53













2 Answers
2






active

oldest

votes

















up vote
18
down vote



accepted










This is expected behavior of std::basic_istream::operator>>; since C++11 if extraction fails, the variable will be set to 0. Before C++11, the variable won't be modified then its original value remains.




If extraction fails (e.g. if a letter was entered where a digit is
expected), value is left unmodified and failbit is set. (until C++11)



If extraction fails, zero is written to value and failbit is set. If
extraction results in the value too large or too small to fit in
value, std::numeric_limits::max() or std::numeric_limits::min()
is written and failbit flag is set. (since C++11)







share|improve this answer


















  • 1




    If no characters could be read then the value is unchanged; the setting 0 only happens on conversion failure
    – M.M
    Aug 16 at 9:12






  • 2




    Just for information: I've used std::cin.fail() to check if there a failbit set.
    – RickSanch3z
    Aug 16 at 9:39

















up vote
4
down vote













If the read fails operator>> will set the value to zero (cppreference):




If extraction fails, zero is written to value and failbit is set.







share|improve this answer



























    2 Answers
    2






    active

    oldest

    votes








    2 Answers
    2






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes








    up vote
    18
    down vote



    accepted










    This is expected behavior of std::basic_istream::operator>>; since C++11 if extraction fails, the variable will be set to 0. Before C++11, the variable won't be modified then its original value remains.




    If extraction fails (e.g. if a letter was entered where a digit is
    expected), value is left unmodified and failbit is set. (until C++11)



    If extraction fails, zero is written to value and failbit is set. If
    extraction results in the value too large or too small to fit in
    value, std::numeric_limits::max() or std::numeric_limits::min()
    is written and failbit flag is set. (since C++11)







    share|improve this answer


















    • 1




      If no characters could be read then the value is unchanged; the setting 0 only happens on conversion failure
      – M.M
      Aug 16 at 9:12






    • 2




      Just for information: I've used std::cin.fail() to check if there a failbit set.
      – RickSanch3z
      Aug 16 at 9:39














    up vote
    18
    down vote



    accepted










    This is expected behavior of std::basic_istream::operator>>; since C++11 if extraction fails, the variable will be set to 0. Before C++11, the variable won't be modified then its original value remains.




    If extraction fails (e.g. if a letter was entered where a digit is
    expected), value is left unmodified and failbit is set. (until C++11)



    If extraction fails, zero is written to value and failbit is set. If
    extraction results in the value too large or too small to fit in
    value, std::numeric_limits::max() or std::numeric_limits::min()
    is written and failbit flag is set. (since C++11)







    share|improve this answer


















    • 1




      If no characters could be read then the value is unchanged; the setting 0 only happens on conversion failure
      – M.M
      Aug 16 at 9:12






    • 2




      Just for information: I've used std::cin.fail() to check if there a failbit set.
      – RickSanch3z
      Aug 16 at 9:39












    up vote
    18
    down vote



    accepted







    up vote
    18
    down vote



    accepted






    This is expected behavior of std::basic_istream::operator>>; since C++11 if extraction fails, the variable will be set to 0. Before C++11, the variable won't be modified then its original value remains.




    If extraction fails (e.g. if a letter was entered where a digit is
    expected), value is left unmodified and failbit is set. (until C++11)



    If extraction fails, zero is written to value and failbit is set. If
    extraction results in the value too large or too small to fit in
    value, std::numeric_limits::max() or std::numeric_limits::min()
    is written and failbit flag is set. (since C++11)







    share|improve this answer














    This is expected behavior of std::basic_istream::operator>>; since C++11 if extraction fails, the variable will be set to 0. Before C++11, the variable won't be modified then its original value remains.




    If extraction fails (e.g. if a letter was entered where a digit is
    expected), value is left unmodified and failbit is set. (until C++11)



    If extraction fails, zero is written to value and failbit is set. If
    extraction results in the value too large or too small to fit in
    value, std::numeric_limits::max() or std::numeric_limits::min()
    is written and failbit flag is set. (since C++11)








    share|improve this answer














    share|improve this answer



    share|improve this answer








    edited Aug 16 at 7:16

























    answered Aug 16 at 7:02









    songyuanyao

    83.6k9161219




    83.6k9161219







    • 1




      If no characters could be read then the value is unchanged; the setting 0 only happens on conversion failure
      – M.M
      Aug 16 at 9:12






    • 2




      Just for information: I've used std::cin.fail() to check if there a failbit set.
      – RickSanch3z
      Aug 16 at 9:39












    • 1




      If no characters could be read then the value is unchanged; the setting 0 only happens on conversion failure
      – M.M
      Aug 16 at 9:12






    • 2




      Just for information: I've used std::cin.fail() to check if there a failbit set.
      – RickSanch3z
      Aug 16 at 9:39







    1




    1




    If no characters could be read then the value is unchanged; the setting 0 only happens on conversion failure
    – M.M
    Aug 16 at 9:12




    If no characters could be read then the value is unchanged; the setting 0 only happens on conversion failure
    – M.M
    Aug 16 at 9:12




    2




    2




    Just for information: I've used std::cin.fail() to check if there a failbit set.
    – RickSanch3z
    Aug 16 at 9:39




    Just for information: I've used std::cin.fail() to check if there a failbit set.
    – RickSanch3z
    Aug 16 at 9:39












    up vote
    4
    down vote













    If the read fails operator>> will set the value to zero (cppreference):




    If extraction fails, zero is written to value and failbit is set.







    share|improve this answer
























      up vote
      4
      down vote













      If the read fails operator>> will set the value to zero (cppreference):




      If extraction fails, zero is written to value and failbit is set.







      share|improve this answer






















        up vote
        4
        down vote










        up vote
        4
        down vote









        If the read fails operator>> will set the value to zero (cppreference):




        If extraction fails, zero is written to value and failbit is set.







        share|improve this answer












        If the read fails operator>> will set the value to zero (cppreference):




        If extraction fails, zero is written to value and failbit is set.








        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Aug 16 at 7:02









        vitaut

        21.6k1187148




        21.6k1187148












            這個網誌中的熱門文章

            Is there any way to eliminate the singular point to solve this integral by hand or by approximations?

            Why am i infinitely getting the same tweet with the Twitter Search API?

            Carbon dioxide