Thread: New to Programming and looking for some help.

  1. #1
    Registered User
    Join Date
    Apr 2012
    Posts
    13

    New to Programming and looking for some help.

    Hey all,

    I have started reading the "jump to C++" book that I purchased from the parent website.

    My question is about strings,

    In chapter 3
    I started with using "cin" to write to a string that i created. That made sense. Then he had me concatenate two strings together , that made sense. Where i got confused was were the author states(quote from the book)


    "When you read in strings, sometimes you want to read a whole line at a time. There is a special function getline
    , which can be used to read in the whole line. It will even automatically discard the newline
    character at the end.

    I dont understand what he means by read in a whole line... what is the differnece between a line and a string? how does

    getline( cin, user_first_name, '\n' );
    differ from

    cin >> user_firstname;

    I feel some very basic principles are being glazed over.

    Another issue involing the getline function, the author mentions you can set the "getline" function to only get a string up to a particualr character... and then say you would need to call another input function to get the rest from the input buffer but then mention how to do this...

    I thank you all for your time and expertise and i do appogize if these are stupid questions... I am just trying to make sense of programming(somthing I have done very very little)

    Thanks again,

    Jonathan

  2. #2
    [](){}(); manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    *nullptr
    Posts
    2,657
    When you use the >> operator to read in something from a stream (here,standard input stream), it gets the characters until it encounters a delimiter.
    As you are reading into a std::string, a space, tab or newline can act as delimiters, which means, you can get a single word at a time.
    But getline, gets everything until the delimiter specified by you (here , a newline).
    Note that you can give other arguments to getline, for ..say.. reading until a semicolon.


    These aren't stupid questions at all. I remember being confused by these sorts of stuff when I started learning C++.

  3. #3
    - - - - - - - - oogabooga's Avatar
    Join Date
    Jan 2008
    Posts
    2,808
    Using getline you can read multiple words with spaces in between. Using >> stops at the first space.
    Try this program:
    Code:
    #include <iostream>
    #include <string>
    using namespace std;
    
    int main() {
        string s;
        cout << "Enter a few words: ";
        cin >> s;
        cout << "All I read was [" << s << "]\n";
        cout << "Now I'll read the rest...\n";
        getline(cin, s);
        cout << "The rest is: [" << s << "]\n";
        return 0;
    }
    Here's a sample run:
    Code:
    Enter a few words: this is a few words
    All I read was [this]
    Now I'll read the rest...
    The rest is: [ is a few words]
    The cost of software maintenance increases with the square of the programmer's creativity. - Robert D. Bliss

  4. #4
    Registered User
    Join Date
    Apr 2012
    Posts
    13
    wow thanks guys! That makes total sense!

    Thank you again guys, your time is really apperciated!

    Jonathan
    Last edited by Mathieas; 04-04-2012 at 11:37 PM.

  5. #5
    - - - - - - - - oogabooga's Avatar
    Join Date
    Jan 2008
    Posts
    2,808
    The input is line-buffered and so is not actually sent to the program until the user presses enter to finish the line. The cin<<s only reads the first word (up to but not including the first space). The getline(cin,s) then reads everything up to the newline (it actually reads the newline character too, but throws it away so that it doesn't appear in the string).
    The cost of software maintenance increases with the square of the programmer's creativity. - Robert D. Bliss

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 4
    Last Post: 12-11-2011, 04:25 PM
  2. Replies: 1
    Last Post: 08-19-2007, 03:55 AM
  3. small programming job VCPP / Object Oriented Programming
    By calgonite in forum Projects and Job Recruitment
    Replies: 10
    Last Post: 01-04-2006, 11:48 PM
  4. Total newb to programming here... Question about the many programming languages. Ty!
    By tsubotakid1 in forum A Brief History of Cprogramming.com
    Replies: 11
    Last Post: 10-05-2003, 10:32 AM