I did it like this and it somewhat works but I have problem with the middle name such as Barrack Hussein Obama. How do I make it so it ignores the middle name and just gives me the last name???

#include <iostream> 
#include <iomanip> 
#include <string>

using namespace std;

int main()
{
    string name;
    string first_name, last_name;
    char response;

do{
    cout << "Enter your full name: ";
    cin >> first_name; cin >> last_name; // get their full name, including spaces.
    // display last name
    cout << "\t" <<first_name << " "<< last_name << ", your last name is " << last_name << endl; 

    cout << "CONTINUE(y/n)? ";
    cin >> response;
    cin.ignore(50, '\n');
    } while(response == 'Y' || response=='y') ;


    system ("pause");
    return 0; 
}

My advice is to read the entire line - not just one word - and parse that. You can easily scan the string from right-to-left to get the last name, and left-to-right to get the first name. See getline().

I did this but I don't get how to go from right to left to get last name?

#include <iostream> 
#include <iomanip> 
#include <string>

using namespace std;

int main()
{
    string name, last, first_name;
    char response;

do{
    cout << "Enter your full name: ";

    getline(cin, name);// get their full name, including spaces.

    // display last name
    cout << "\t" << name << ", your last name is " << last << endl; 

    cout << "CONTINUE(y/n)? ";
    cin >> response;
    cin.ignore(50, '\n');
    } while(response == 'Y' || response=='y') ;


    system ("pause");
    return 0; 
}

You're asking how to tokenise a string. Now that you know what it's called, you'll be able to find all the resources you need.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.