The following questions are to be completed, but NOT handed-in. 14. Write a C++
ID: 3668908 • Letter: T
Question
The following questions are to be completed, but NOT handed-in.
14. 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 output each part of the name together with its length separately in the following format:
John J. Schmidt ---------------
First name: Middle name: Last name:
Jane M. Roe ----------- First name: Middle name: Last name:
...
John (4) Jacob (5) Schmidt (7)
Please be noted that the middle is the initial with a dot in output.
Explanation / Answer
#include <iostream>
#include <string>
#include<fstream>
#include<sstream>
#include<vector>
using namespace std;
int main()
{
ifstream iFile("/home/shekhar/Desktop/chegg/CS/cpp/Records");
string line;
/* While there is still a line. */
while(getline(iFile, line))
{
string buff;
stringstream ss(line);
vector<string> tokens;
while(ss>>buff)
{
tokens.push_back(buff);
}
string firstName=tokens[0]; // ... you fill this in
string middleName=tokens[1]; // ... ditto
string lastName=tokens[2]; // ... ditto
cout<<" ";
cout <<"Full name is "<< line << endl;
cout <<"First: "<<tokens[0]<<" ("<<firstName.length() <<") " ;
cout <<"Middle: "<<tokens[1]<<" ("<<middleName.length() <<") ";
cout <<"Last: "<<tokens[2]<<" ("<<lastName.length() <<") ";
}
iFile.close();
return 0;
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.