The output is blank. It should work by putting first name 1st, last name last, a
ID: 3634264 • Letter: T
Question
The output is blank. It should work by putting first name 1st, last name last, and finding and dumping the comma from a file that contains:
Kirk, James T.
Picard, Jean-Luc
Sisko, Benjamin
Janeway, Kathryn
Archer, Jonathan
Spock
7 of 9
.
open file didn't fail, don't know what I'm doing wrong!
The program should read the input file name from the user, the last line has a "."
#include <iostream>
#include <cstring>
#include <string>
#include <fstream>
using namespace std;
int main()
{
string input,FIRST,LAST,THE_NAME;
int comma;
int len;
ifstream in_stream;
in_stream.open("E: ames.txt");
if (in_stream.fail())
{
cout << "Input file opening failed. ";
}
getline(cin,input);
while (! in_stream.eof())
{
len=input.length();
comma=input.find(',');
FIRST=input.substr(comma+1,len-comma+1);
LAST=input.substr(0,comma);
THE_NAME=FIRST+" "+LAST;
getline(cin,input);
}
in_stream.close();
cout<<THE_NAME<<endl;
return 0;
}
Explanation / Answer
please rate - thanks
you're inputting from the console, not the file, so it was waiting for you to type something in
hope you don't mind, I fixed the rest of the code
including adding for when there is no comma
#include <iostream>
#include <cstring>
#include <string>
#include <fstream>
using namespace std;
int main()
{
string input,FIRST,LAST,THE_NAME;
int comma;
int len;
ifstream in_stream;
in_stream.open("E: ames.txt");
if (in_stream.fail())
{
cout << "Input file opening failed. ";
}
getline(in_stream,input);
while (! in_stream.eof())
{THE_NAME="";
len=input.length();
comma=input.find(',',0);
if(comma>0)
{FIRST=input.substr(comma+1,len-comma+1);
LAST=input.substr(0,comma);
THE_NAME=FIRST+" "+LAST;
}
else
THE_NAME=input;
cout<<THE_NAME<<endl;
getline(in_stream,input);
}
in_stream.close();
system("pause");
return 0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.