My assignment is to use an input file of names, and have a program break the names down in first-last, and last-first-initial format. And to use an End Of File loop to terminate. My proglem is that Sara-Jessica Parker doesn't have a middle name and so ruins the outputs for the final 2 names. In fact the last name in my file doesn't output. Is there a way that I can use the "/n" to input data? Will that solve the problem?
Code:
//******************************************************************
// PrintName program
// This program prints a name in two different formats
//******************************************************************
#include <iostream>
#include <string>
#include <fstream>
using namespace std;
string FIRST; // Person's first name
string LAST; // Person's last name
char MIDDLE; // Person's middle initial
ifstream inPut; // Data File
ofstream outPut;
int main()
{
inPut.open("names.dat");
outPut.open ("output.dat");
if ( !inPut )
{
cout << "Can't open the input file.";
return 1;
}
inPut >> FIRST >> LAST >> MIDDLE;
while (inPut)
{
string firstLast; // Name in first-last format
string lastFirst; // Name in last-first format
firstLast = FIRST + " " + LAST;
outPut << "Name in first-last format is " << firstLast << endl;
lastFirst = LAST + ", " + FIRST + ", ";
outPut << "Name in last-first-initial format is ";
outPut << lastFirst << MIDDLE << '.' << endl;
inPut >> FIRST >> LAST >> MIDDLE;
}
inPut.close();
return 0;
}
Names.dat file:
George Bush W
Madhavi Gandhi D
Dan Berk G
Ameka Johnson P
Rita Hayward C
Clint Eastwood B
Sara-Jessica Parker
Larry Kind L
Rani Mukharji