Question: Please complete the C++ code as instructed below: create a program that reads a text file and tersely summarizes it. Read over main.cpp to try
Please complete the C++ code as instructed below:
create a program that reads a text file and tersely summarizes it.
Read over main.cpp to try to understand what it is doing, step-by-step.
Your job is to write the code that will save the first and last line of the text file so that the cout statement at the end displays them out. The program should disregard any other line in the file, but it should print the first line, followed by "...yada, yada, yada... ", followed by the last line.
When there are fewer than 2 lines (therefore not having a unique "last" line) in the file, it should print "The End." in place of a last line.
Hint
To get a full line of text (rather than just one word at a time), you need to use the function getline().
If we were reading a full line of text from (typed) user input, we would use:
string line;
getline(cin,line);
and that would read from regular cin input (everything until the user hits Return) and store it in the variable called line. However, since we are reading from a file instead of command line input, we want to use something closer to:
string line; //declares a variable called line that is a string
ifstream file; //declares a variable called file that is an Input File stream
file.open("myfile.txt"); //loads a file called "myfile.txt" to be read by the stream
getline(file,line); //reads a line (until a Return) of the file and saves it in the variable line.
Main.cpp Code
#include
#include
#include
using namespace std;
int main()
{
ifstream file;
string filename;
string line;
string first;
string last;
bool success;
do
{
cout<<"What file do you want paraphrased? ";
cin>>filename;
cin.ignore();
file.open(filename);
success = file.is_open();
if( success == false )
{
cout<<"Cannot open "< } }while( success == false ); //Do stuff here that will get the first //and last line of the text file cout< file.close(); return 0; }
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
