I am trying to write a program that shows an output.dat file showing the following format:
John J. Schmidt
---------------
First name: John (4)
Middle name: Jacob (5)
Last name: Schmidt (7)
Jane M. Roe
-----------
First name: Jane (4)
Middle name: Marry (5)
Last name: Roe (3)
etc. I have an input file of 10 names but all that the program is outputting in the file is John J. Schmidt. How do I correct this???
#include <fstream>
#include <string>
using namespace std;
int main ()
{
ifstream inData;
ofstream outData;
inData.open ("namein.dat");
outData.open ("nameout.dat");
string firstName;
string lastName;
string middleName;
char initial;
inData >> firstName >> middleName >> lastName;
initial = middleName.at(0);
outData << firstName << " " << initial << ". " << lastName << endl;
outData << "---------------" << endl;
outData << "First name: " << firstName << " (" << firstName.length() << ")" << endl;
outData << "Middle name: " << middleName << " (" << middleName.length() << ")" << endl;
outData << "Last name: " << lastName << " (" << lastName.length() << ")" << endl;
inData.close();
outData.close();
return 0;
}
because your code reads only one name ,
you shall use a loop for reading all of names .
I created an EOF loop. Thanks!