0

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.

Edited by Reverend Jim: Moved to programming forum

4
Contributors
4
Replies
40
Views
5 Months
Discussion Span
Last Post by tinstaafl
0

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.

0

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

Edited by tinstaafl

Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.