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; I'm trying to recall all of the versions of std::cin and how they deal with string input. Please correct me ...

  1. #1
    C/C++ homeyg's Avatar
    Join Date
    Nov 2004
    Location
    Louisiana, USA
    Posts
    209

    Tell me if I'm right-

    I'm trying to recall all of the versions of std::cin and how they deal with string input. Please correct me if I'm wrong or if I'm missing any (I'm typing all of this from memory).

    cin>> reads chars into the variable or array. Terminates upon blank characters and throws away \n characters produced by the enter key. If it's an array, it adds the null character at the end.

    cin.get() reads a char from the input stream and returns it. Leaves the \n character in the input buffer.

    cin.get(ch) reads a char from the input stream and stores it in ch. Leaves the \n character in the input buffer.

    cin.get(chArray, 50) reads up to 50 characters from the input stream and stores them in chArray. Leaves the \n character in the input buffer. Puts the null character where the \n was.

    cin.getline(chArray, 50) reads up to 50 characters from the input stream and stores them in chArray. Reads and throws away the newline character. Puts the null character where the \n was.

    Please make any corrections necessary. I'm tired of not being able to correctly remember what all these do.
    Last edited by homeyg; 02-07-2005 at 08:54 PM.

  2. #2
    Registered User axon's Avatar
    Join Date
    Feb 2003
    Location
    Mt. Prospect, IL
    Posts
    2,572

    some entropy with that sink? entropysink.com

    there are two cardinal sins from which all others spring: Impatience and Laziness. - franz kafka

  3. #3
    Registered User
    Join Date
    Jan 2005
    Posts
    7,317
    Quote Originally Posted by homeyg
    cin>> reads chars into the variable or array. Terminates upon blank characters and throws away \n characters produced by the enter key. If it's an array, it adds the null character at the end.
    Wrong about throwing away '\n' characters. By default, using cin >> leaves the '\n' character and any other whitespace around that comes after the data it reads in. It skips whitespace that comes before the word it reads.
    Quote Originally Posted by homeyg
    cin.get() reads a char from the input stream and returns it. Leaves the \n character in the input buffer.
    It reads in one character. It leaves the next character in the input buffer. No need to specify '\n', since it behaves the same as any other character (or whitespace). If the current character happens to be '\n', then it will be read, not left in the input buffer.
    Quote Originally Posted by homeyg
    cin.get(ch) reads a char from the input stream and stores it in ch. Leaves the \n character in the input buffer.
    Same as above. If the current character happens to be '\n', then it will be read, not left in the input buffer.
    Quote Originally Posted by homeyg
    cin.get(chArray, 50) reads up to 50 characters from the input stream and stores them in chArray. Leaves the \n character in the input buffer. Puts the null character where the \n was.
    Wrong about the 50 characters. It reads up to 49 characters to leave room for the null character at the end.
    Quote Originally Posted by homeyg
    cin.getline(chArray, 50) reads up to 50 characters from the input stream and stores them in chArray. Reads and throws away the newline character. Puts the null character where the \n was.
    Same as above. It reads up to 49 characters to leave room for the null character at the end.

    For all of the above, change the word 'newline' or '\n' to 'delimiter' so that you remember that you can change the delimiter even though the default is '\n'.

  4. #4
    Registered User
    Join Date
    Apr 2003
    Posts
    2,662
    That's a good list to compile. I was just reviewing some of those tonight. In a list like that, I would include the

    cin>>number;

    followed by

    cin.getline(text, maxlength)

    problem where the program mysteriously skips over cin.getline(), as well as the cin.ignore() fix. You might also want to include the parameters for each function. Several of them take a third parameter which allows you to specify the delimiter, and enables you to skip over the \n and read in multiple lines

    I've got a question. I know that on different systems the carriage return character is different:

    Windows: \r\n
    Mac: \r
    Unix: \n

    so why doesn't the \r muck things up in C++ programs?
    Last edited by 7stud; 02-08-2005 at 02:05 AM.

  5. #5
    Registered User major_small's Avatar
    Join Date
    May 2003
    Posts
    2,787
    a \n can be related to \r\n, which is (carriage return)(feed). in the olden days, when you typed a line, you would have to send a signal to the printer to bring the print head (the carriage) back to the first column on the page, and then feed one line of the page. then when you started printing, you were on the next line.

    as for the "skipping" thing you mentioned: http://www.cprogramming.com/tips/sho...ount=30&page=0
    Last edited by major_small; 02-08-2005 at 12:58 AM.
    Join is in our Unofficial Cprog IRC channel
    Server: irc.phoenixradio.org
    Channel: #Tech


    Team Cprog Folding@Home: Team #43476
    Download it Here
    Detailed Stats Here
    More Detailed Stats
    52 Members so far, are YOU a member?
    Current team score: 1223226 (ranked 374 of 45152)

    The CBoard team is doing better than 99.16% of the other teams
    Top 5 Members: Xterria(518175), pianorain(118517), Bennet(64957), JaWiB(55610), alphaoide(44374)

    Last Updated on: Wed, 30 Aug, 2006 @ 2:30 PM EDT

  6. #6
    Registered User
    Join Date
    Apr 2003
    Posts
    2,662
    Quote Originally Posted by major_small
    I think it's more like:
    Code:
    Windows: \r\f
    Mac: \r
    Unix: \f
    Wait a minute now I'm confused. In C++, we talk about \n being entered with the return key, and how it sits in the buffer and because \n is the defaul delimiter for cin.getline(), etc., it can cause problems. Now you're saying \n doesn't even exist?

    In any case, my point is if one of the two characters in Windows is the default delimiter for cin.getline(), etc. where does the other character go?
    Last edited by 7stud; 02-08-2005 at 12:53 AM.

  7. #7
    Registered User major_small's Avatar
    Join Date
    May 2003
    Posts
    2,787
    http://www.csee.umbc.edu/help/C++/summary.shtml#Lexical

    it's a little hard to explain... '\n' does exist, but in effect, it's the same as sending "\r\f" to the system... the default delimiter in any case for cin.getline is '\n', but yes, if you were using this:
    Code:
    std::cin.getline(line,x,'\r');
    you would end up with an extra \f in the stream.

    edit: you were right, windows uses \r\n, and unix uses \n

    edit2: from the mozilla website: http://www.mozilla.org/hacking/porta...#no_cr_in_code
    Last edited by major_small; 02-08-2005 at 01:08 AM.
    Join is in our Unofficial Cprog IRC channel
    Server: irc.phoenixradio.org
    Channel: #Tech


    Team Cprog Folding@Home: Team #43476
    Download it Here
    Detailed Stats Here
    More Detailed Stats
    52 Members so far, are YOU a member?
    Current team score: 1223226 (ranked 374 of 45152)

    The CBoard team is doing better than 99.16% of the other teams
    Top 5 Members: Xterria(518175), pianorain(118517), Bennet(64957), JaWiB(55610), alphaoide(44374)

    Last Updated on: Wed, 30 Aug, 2006 @ 2:30 PM EDT

  8. #8
    Registered User
    Join Date
    Apr 2003
    Posts
    2,662
    So, what happens when a user on Windows enters the character 'H' into the following program:
    Code:
    #include <iostream>
    using namespace std;
    
    int main()
    {
        char text[5];
        cin.getline(text, 5);
    
        cout<<text<<endl;
        return 0;
    }
    Does text look like this:

    H\r\0\0\0

    but the \r won't display when you use cout<<? Or, does C++ convert \r\n into \n somehow? Or, are all escape sequences in the stream cin discarded?

  9. #9
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,893
    I/O is required to convert between platform newlines and \n on in- and output.
    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

  10. #10
    Registered User
    Join Date
    Apr 2003
    Posts
    2,662
    I/O is required to convert between platform newlines and \n on in- and output.
    I need a translation.

  11. #11
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,893
    Both the C and the C++ standards say that in the program, a newline is a single \n character. If the platform uses a different sequence, like \r\n or \r, then the I/O routines (scanf, printf, istream, ostream, ...) are required to convert between them if the source (e.g. a file) is opened in text mode.
    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

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

  13. #13
    C/C++ homeyg's Avatar
    Join Date
    Nov 2004
    Location
    Louisiana, USA
    Posts
    209
    I was doing some experimenting with cin.get() and cin.getline() and noticed a few things. Here is the code I was using to do the test:

    Code:
    #include <iostream>
    using namespace std;
    int main()
    {
        char ch[10];
        char left[10];
        
        cout<<"Enter characters."<<endl;
        
        cin.getline(ch,10);
        cin.getline(left, 10);
        
        cout<<ch<<endl;
        cout<<left<<endl;
        
        
        system("PAUSE");
        return 0;
    }
    With the code as it is, when I would enter more than 9 characters for the first input, it seems that any characters after 9 are simply thrown away. I'm assuming this is how that function functions. However, there is no chance for any second input. Can someone explain what is happening here?

  14. #14
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,893
    Are you sure that anything beyond the first 9 characters doesn't end up in left?
    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

  15. #15
    C/C++ homeyg's Avatar
    Join Date
    Nov 2004
    Location
    Louisiana, USA
    Posts
    209
    No nothing is there when the second array is outputted and there is no chance for input. Run the program for yourself.

Page 1 of 2 12 LastLast
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