Write a program that reads names (from an input file) in the following format (y
ID: 3632689 • Letter: W
Question
Write a program that reads names (from an input file) in the following format (you can assume names are no more than 30 characters if you are using C-string):First_name Middle_name/Middle_initial Last_name
The program then outputs (to a file) the name in the following format (a period must be place right after the Middle_initial. The first character of First/Middle/Last name must be capitalized):
Last_name, First_name Middle_initial.
The input file name must be "infile" and the output file name must be "outfile". There are unknown number of persons in input files, however, each person's name occupies a line. And each person must have First, Middle/Middle Initial, and Last in the format described above.
Example:
Suppose the input file consists of (special consideration is required for the case "Mary J. Horn"):
Mary J. Horn
John Jerry Doe
Edward David Cain
Then the output file should contain:
Horn, Mary J.
Doe, John J.
Cain, Edward D.
Explanation / Answer
please rate - thanks
be sure when you save the input file you put the name in quotes, since you want no extension
#include <iostream>
#include <fstream>
using namespace std;
int main()
{ifstream in;
ofstream out;
char input[30],first[20],middle[20],last[20];
int i=0,j=0,k=0;
bool midfound;
in.open("infile");
if(in.fail())
{cout<<"input file did not open please check it ";
system("pause");
return 1;
}
out.open("outfile");
in.getline(input,30);
while(in)
{
i=0;
while(input[i]!=' ')
{first[i]=input[i];
i++;
}
first[i++]='';
for(j=i;j<30;j++)
if(input[j]==' ')
{middle[0]=input[i];
middle[1]='.';
middle[2]='';
i=j+1;
midfound=true;
j=90;
}
else if(input[j]=='')
{midfound=false;
j=30;
}
j=0;
while(input[i]!='')
{last[j++]=input[i++];
}
last[j++]='';
out<<last<<", "<<first<<" "<<middle<<endl;
in.getline(input,30);
}
in.close();
out.close();
return 0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.