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; 
}

Dani AI

Generated

Short answer: read the whole line and pick the last meaningful token. was right to recommend getline() and scanning from the right; is right to call it tokenise. The original cin >> first_name >> last_name approach breaks when a middle name is present because operator>> stops at whitespace.

A robust approach:

  • Read the full line with getline.
  • Trim trailing/leading whitespace.
  • Split on whitespace into tokens (use istringstream or scan from the right).
  • Strip trailing punctuation (commas, periods) from the candidate token.
  • If the candidate is a known suffix (Jr, Sr, II, III, ...), drop it and use the previous token.
    That handles "Barack Hussein Obama" -> "Obama" and common "Last, First" inputs where a comma appears.

Example tokenisation-based routine (concise, ready to adapt):

#include <iostream>
#include <sstream>
#include <vector>
#include <string>
#include <algorithm>
#include <cctype>

std::string strip_punct(std::string s){
    while(!s.empty() && ispunct(static_cast<unsigned char>(s.back()))) s.pop_back();
    return s;
}
std::string to_lower(std::string s){
    std::transform(s.begin(), s.end(), s.begin(), [](unsigned char c){ return std::tolower(c); });
    return s;
}

// read a full line into 'fullname', tokenize, remove suffixes, print last name
// (adapt this snippet into your loop/IO flow)
std::string extract_last(const std::string &fullname){
    std::istringstream ss(fullname);
    std::vector<std::string> tokens; std::string t;
    while(ss >> t) tokens.push_back(t);
    if(tokens.empty()) return "";
    std::string last = strip_punct(tokens.back());
    static const std::vector<std::string> suffixes = {"jr","sr","ii","iii","iv"};
    if(std::find(suffixes.begin(), suffixes.end(), to_lower(last)) != suffixes.end() && tokens.size() >= 2)
        last = strip_punct(tokens[tokens.size()-2]);
    return last;
}

Notes and caveats: multi-word last names ("de la Cruz", "Van der Waals") and cultural name orders are hard to detect reliably by heuristics. If correctness matters, ask for separate fields (first / last) or provide a small UI question. Also validate empty input and trim carefully before tokenising.

Recommended Answers

All 3 Replies

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.