split string using getLine function

This is a discussion on split string using getLine function within the C++ Programming forums, part of the General Programming Boards category; I think I need a small split string program with a getLine function to do "exactly" this: ( Please notice ...

  1. #1
    Registered User
    Join Date
    Sep 2010
    Posts
    90

    split string using getLine function

    I think I need a small split string program with a getLine function to do "exactly" this:

    (Please notice the user type his full name on one line, but the program output First and Last name on separate lines.)

    Please type your first and last name:
    Sam Harris
    Your First Name is:
    Sam
    Your Last Name is:
    Harris



    Below is some code I been playing around with, with no success. It is "TOTALLY" useless because it needs user input from the keyboard as described above. Not embedded names like I have in the code below. Can some one fix this or would you have an working example to archive what I'm after which is color-coded above.

    Code:
    #include <iostream>
    #include <sstream>
    #include <string>
    using namespace std;
    
    int main()
    {
    
    // useless. We need user keyboard input.  Not embedded
    // names and it must be in Standard C++.
     
    string s("Sam Harris");
    istringstream iss(s);
    
            string sub;
            iss >> sub;
            cout << "First Name: " << sub << endl;
    
            iss >> sub;
    cout << "Last Name: " << sub << endl;
        return 0;
    }
    Hope I made my question clear.

    Thanks in advance
    Last edited by sharris; 09-24-2010 at 09:09 PM.

  2. #2
    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

  3. #3
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,761
    Uh, istringstreams are variations on istreams so just things like std::cin >> sub; will work.

  4. #4
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Posts
    23,007
    Alternatively (if we want the user to enter their full name), read into a string with std::getline. Pass that string to your stringstream and continue as usual.
    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
    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 04:55 AM.

  6. #6
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Posts
    23,007
    Like I said:
    Code:
    #include <iostream>
    #include <sstream>
    #include <string>
    using namespace std;
    
    int main()
    {
        string str;
        cout << "Please enter full name: ";
        getline (cin,str);
    
        istringstream iss(str);
    
        string sub;
        iss >> sub;
        cout << "First Name: " << sub << endl;
    
        iss >> sub;
        cout << "Last Name: " << sub << endl;
        return 0;
    }
    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
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,761
    You apparently know how >> works for istreams. By default it ignores the whitespace in anything you type. Since we have a full name, we should have at least two variables (for first and surname) and then you can use cin to read into those variables directly.

  8. #8
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Posts
    23,007
    I believe the OP wants:

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

    Which you cannot do with simply cin >>.
    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.

  9. #9
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,761
    Code:
    string foo;
    string bar;
    
    cout << "What's your first and last name? ";
    if (cin >> foo>> bar)
    {
       cout << foo << " " << bar << " is a nice name" << endl;
    }
    I really had to pull out the runes for this spell.

    Chaining operators isn't useful if you're prone to forgetting them.

  10. #10
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Posts
    23,007
    Hmmm. True that. There are two ways of doing it.
    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.

  11. #11
    Registered User
    Join Date
    Sep 2010
    Posts
    90
    My brain almost went mush until now ... Wow Elysia! I just nearly figured it out minutes ago by realizing that:

    -string s;

    -cout << "Please enter full name: ";
    -getline (cin, s);

    Will put the full name in a variable just the same as writing it in:

    -string s("Sam Harris")

    So now all I have to do is piece the rest together and call it. But checking here first, you already did it and it looks a whole lot better on your post and you will never find anything like it any where when googling. At lease I could not for over 72 hours. .. Thank you

    And to boot we got two ways of doing it with standard C++. Thank you all. WoW!
    Last edited by sharris; 09-25-2010 at 08:25 AM. Reason: Tone my happiness down. I got sooo connie :)

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, 05: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, 03: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

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