Write a C++ program that reads in from a file that contains full names of employees of ABC
Company. The file is formatted in the following way:
John Jacob Schmidt
Jane Marry Roe

The program should take each name from the file into a string and display each part of the name
together with its length separately to screen in the following format:
Schmidt, John J.
First name: John (4)
Middle name: Jacob (5)
Last name: Schmidt (7)
Roe, Jane M.

First name: Jane (4)
Middle name: Marry (5)
Last name: Roe (3)

For the first line of the file, you should read John, Jacob, and Schmidt into three string
variables. His full name, Schmidt, John J., should be stored in another string variable for output.
As there are unknown numbers of names/lines, you need to choose appreciated loop statement in this
program. Please be noted that the middle is the initial with a dot in output.

Recommended Answers

This reads as if you want others to write this code for you. It looks far too similar to a post 7 years ago at http://www.cplusplus.com/forum/beginner/20873/ That's pretty strange.

Try again but share your code so far and what is eluding you.

Jump to Post

All 4 Replies

This reads as if you want others to write this code for you. It looks far too similar to a post 7 years ago at http://www.cplusplus.com/forum/beginner/20873/ That's pretty strange.

Try again but share your code so far and what is eluding you.

What rproffitt is politely trying to say is that we do not do your homework for you. Make an effort. Show your code and where you are having problems. At that point, we may help you. Also, do read the terms of use of this site. It covers this.

What i have so far, and it only breaks up the second line into each string and not the first line. How can i display the full name and break it up into strings?

#include <iostream>
#include <string>
#include <fstream>

using namespace std;

int main()
{
    ifstream names_("Names.txt");
    string fullName;
    string firstName;
    string middleName;
    string lastName;
    int count = 0;

    if (names_.is_open())
    {
        while (getline(names_, fullName))
        {
            cout << fullName;
            cout << "\n";
            cout << "----------------";
            ++count;
            while (names_ >> firstName >> middleName >> lastName)
            {

                cout << "\n";
                cout << "First name: " << firstName;
                cout << "\n";
                cout << "Middle name: " << middleName;
                cout << "\n";
                cout << "Last name: " << lastName;
                cout << "\n";
                cout << "----------------";
                cout << "\n";
            }

            cout << endl;
        }
    }
    else
    {
        cout << "file does not open" << endl;
    }
    return 0;
}

You're reading the first line using getline, then the subsequent reads are on the next line. Get rid of that and build the full name inside the while loop:

if (names_.is_open())
{
    while (names_ >> firstName >> middleName >> lastName)
    {
        cout << lastName << ", " << firstName << " " << middleName[0] << ".\n";
        cout << "----------------\n";
        cout << "First name: " << firstName << " (" << firstName.size() << ")\n";
        cout << "Middle name: " << middleName << " (" << middleName.size() << ")\n";
        cout << "Last name: " << lastName << " (" << lastName.size() << ")\n";
        cout << "----------------\n";
    }
}
Be a part of the DaniWeb community

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