Thread: c++ input stream and cin

  1. #1
    Registered User
    Join Date
    Jul 2009
    Location
    Croatia
    Posts
    272

    c++ input stream and cin

    Ok, well, some things are unclear to me about how cin works.

    If we for example do this:

    Code:
    #include <cstdlib>
    #include <iostream>
    
    using namespace std;
    
    int main()
    {
         int answer = 0;
         string s;
         
         cin >> answer;
         cin >> s;
         
         cout << s << endl;
         
         system("PAUSE");
         return EXIT_SUCCESS;
    }
    And if the input looks like

    not_integer

    Why wont cin >> s pick it up?

    if the first cin >> answer fails, it pushes the first readed character back to input. so the next cin should pick it up.

    but this isnt the case. why?

    edit: i also came accros information that cin's bad and easily error prone. Should i avoid using it?

    I want my programs to be safe ofcourse.

    I come from a C background, thats why this is strange to me.
    Last edited by Tool; 06-11-2010 at 08:37 AM.

  2. #2
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    After attempting to read "answer", the stream will have a flag set that indicates an error condition. That flag needs to be cleared before your code can successfully read s.

    If you want safe code, a rule of thumb is: after any operation that may fail, check to see if it has, and recover as needed. You are not doing that.

    I'll leave it as an exercise to work out who to check the state of the stream, and to correct it. Reading the documentation to learn effective usage of iostreams won't hurt you.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

  3. #3
    Registered User
    Join Date
    Jul 2009
    Location
    Croatia
    Posts
    272
    Code:
    #include <cstdlib>
    #include <iostream>
    
    using namespace std;
    
    int main()
    {
         int answer;
         string s;
         
         here:
         cin >> answer;
         cin >> s;
         if(cin.failbit) {
            printf("omfg\n");
            cin.failbit = 0;
         } else
               printf("cool!\n");
    
         system("PAUSE");
         return EXIT_SUCCESS;
    }
    This doesnt seems to be working either - seting a non read variable ?

  4. #4
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    cin.failbit is a constant. It is the constant you set or clear to indicate or remove the fail flag from the stream. You're not using the correct function.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  5. #5
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by grumpy View Post
    I'll leave it as an exercise to work out who to check the state of the stream, and to correct it. Reading the documentation to learn effective usage of iostreams won't hurt you.
    This is great:

    C++ Reference

    If you click "iostream", scroll down, click "cin", scroll down, click "istream" (sort of circuitous, but it follows the inheritance structure), you'll notice the method you want is just called "fail()". There's a badbit and a failbit, presumably these are bitflags (get it?).
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  6. #6
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    I like an alternative to MSDN, but where is std::array and std::shared_ptr!?!
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  7. #7
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by Elysia View Post
    I like an alternative to MSDN, but where is std::array and std::shared_ptr!?!
    Good question, altho apparently these are not part of the current standard. Eg. Blarney Strnostr's book from last year ("Programming C++") does not mention them either.

    But thanks for letting me in on std::array, sounds like a better vector
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  8. #8
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    They're part of TR1, and should be part of C++0x.
    std::array is better than native C arrays. They are static arrays, and meant as a replacement for the C arrays (integrates better with the standard library and so on). Not a replacement for std::vector.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. cin stream
    By RaisinToe in forum C++ Programming
    Replies: 3
    Last Post: 02-27-2009, 01:20 PM
  2. Some bugs which i can't find
    By Lincoln_Poh in forum C++ Programming
    Replies: 7
    Last Post: 09-18-2008, 08:11 PM
  3. Help loading a vector with input from input stream
    By Bnchs in forum C++ Programming
    Replies: 9
    Last Post: 11-07-2006, 03:53 PM
  4. EOF messing up my input stream?
    By Decrypt in forum C++ Programming
    Replies: 4
    Last Post: 09-30-2005, 03:00 PM
  5. eof member function on the stream cin
    By stimpyzu in forum C++ Programming
    Replies: 3
    Last Post: 10-30-2002, 05:00 PM