Tell me if I'm right-

This is a discussion on Tell me if I'm right- within the C++ Programming forums, part of the General Programming Boards category; Will do, but I first need to grab a compiler. Don't have one on this computer....

  1. #16
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,893
    Will do, but I first need to grab a compiler. Don't have one on this computer.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  2. #17
    Registered User
    Join Date
    Jan 2005
    Posts
    7,328
    Check the return values - cin is put into a fail state after the first getline, so until you clear it all subsequent reads will fail. See what happens when you change the first getline to:
    Code:
    if (!cin.getline(ch, 10))
      cin.clear();

  3. #18
    C/C++ homeyg's Avatar
    Join Date
    Nov 2004
    Location
    Louisiana, USA
    Posts
    209
    Yes, it solves the problem. I never really knew what cin.clear() did before this.

  4. #19
    Registered User
    Join Date
    Apr 2003
    Posts
    2,662
    Quote Originally Posted by Daved
    Check the return values - cin is put into a fail state after the first getline, so until you clear it all subsequent reads will fail. See what happens when you change the first getline to:
    Code:
    if (!cin.getline(ch, 10))
      cin.clear();
    Why is cin put in fail safe mode? I can't find any information on getline() that describes that behavior?

  5. #20
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,893
    MSDN:
    The second function extracts up to _Count - 1 elements and stores them in the array beginning at _Str. It always stores char_type after any extracted elements it stores. In order of testing, extraction stops:

    * At end of file.
    * After the function extracts an element that compares equal to _Delim, in which case the element is neither put back nor appended to the controlled sequence.
    * After the function extracts _Count - 1 elements.

    If the function extracts no elements or _Count - 1 elements, it calls setstate(failbit). In any case, it returns *this.
    http://msdn.microsoft.com/library/de...rFunctions.asp
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  6. #21
    Registered User
    Join Date
    Apr 2003
    Posts
    2,662
    Thanks.

  7. #22
    Registered User
    Join Date
    Apr 2003
    Posts
    2,662
    After playing around a bit with those two successive calls to getline(), this is what I came up with to make the program work in a predictable way:
    Code:
    #include <iostream>
    using namespace std;
    
    int main()
    {
        char ch[3];
        char left[3];
        
        cout<<"Enter characters."<<endl;
        
        cin.getline(ch,3);
        
        if(cin.fail())  //then there are more characters to read
            cin.clear();
        else  //the buffer is empty
            cout<<"Enter some more characters:\n";
            
        cin.getline(left, 3);
                
        cout<<ch<<endl;
        cout<<left<<endl;
        
        return 0;
    }
    Last edited by 7stud; 02-09-2005 at 07:33 AM.

Page 2 of 2 FirstFirst 12
Popular pages Recent additions subscribe to a feed

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21