Thread: split string using getLine function

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Registered User
    Join Date
    May 2009
    Posts
    242
    so the program is supposed to split up the string "Sam Harris" into "Sam" and "Harris" and print out accordingly?

    if so, while i haven't really worked with istringstream objects, getline() should work to get you there: getline - C++ Reference

  2. #2
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    Uh, istringstreams are variations on istreams so just things like std::cin >> sub; will work.

  3. #3
    Registered User
    Join Date
    Sep 2010
    Posts
    90
    No Aisthesis . It should not be an embeded name. The first example I posted was just a piece of code I came up with while trying. Working code should display on screen from anyone who type there name full name at the keyboard. I don't know how to make it work with getLine of set-up the std::cin >> sub thing. I am brand new learning C++. I love the way C++ people talk code, but I got to see code examples to get an understanding of how the code work. This is my 3rd week.

    (Please notice the user type his full name on one line, but the program output First and Last name on separate lines. Number 2 is the one I am trying to do)

    1) Everybody knows how to output a single line like this:

    Please type your first and last name:
    John Doe
    Thankyou

    2) But it seem no one knows how to output to seperate lines like this:

    Please type your first and last name:
    John Doe
    Your First Name is:
    John
    Your Last Name is:
    Doe


    This code is a little mix-up but I'm sure it's only a hair away from working.
    I just don't know how to get it to work:


    Code:
    #include <iostream>
    #include <sstream>
    #include <string>
    using namespace std;
    
    int main()
    {
    string s("Sam Harris");
    istringstream iss(s);
    
    string sub;
    iss >> sub;
    cout << "First Name: " << sub << endl;
    
    iss >> sub;
    cout << "Last Name: " << sub << endl;
    
    string str;
    cout << "Please enter full name: ";
    getline (cin,str);
    cout << "Thank you, " << str << ".\n";
    return 0;
    }
    Last edited by sharris; 09-25-2010 at 03:55 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. OOP Question DB Access Wrapper Classes
    By digioz in forum C# Programming
    Replies: 2
    Last Post: 09-07-2008, 04:30 PM
  2. Inheritance Hierarchy for a Package class
    By twickre in forum C++ Programming
    Replies: 7
    Last Post: 12-08-2007, 04:13 PM
  3. Screwy Linker Error - VC2005
    By Tonto in forum C++ Programming
    Replies: 5
    Last Post: 06-19-2007, 02:39 PM
  4. Linked List Help
    By CJ7Mudrover in forum C Programming
    Replies: 9
    Last Post: 03-10-2004, 10:33 PM
  5. Something is wrong with this menu...
    By DarkViper in forum Windows Programming
    Replies: 2
    Last Post: 12-14-2002, 11:06 PM