Write a program that asks the user for the name of a file . The program should d
ID: 3820947 • Letter: W
Question
Write a program that asks the user for the name of a file.
The program should display the contents of the file on the screen.
If the file's contents won't fit on a single screen, the program should display 24 lines of output at a time, and then pause.
Each time the program pauses it should wait for the user to strike a key before the next 24 lines are displayed.
--------------------------------------------------------------------------------------------------
//There have so many errors, please change whatever you want to be correct answer -thank you so much for helping!
//.Display Program
#include
#include
#include
using namespace std;
int main (int argc, char *argv[])
{
ifstream in_stream;
//open the file
in_stream.open("Display_Input.txt");
string data;
//count the no of lines read and printed
int count=0;
//loop till end of the file
while(!in_stream.eof())
{
//read the file data
count++;
getline(in_stream,data);
//if no of lines printed are less than 10 then print
if(count <=24){
cout< }
else{
cout << "Press ENTER to continue to print the next records from file " << endl;
int ch = cin.get();
while (ch != ' ')
{
ch = cin.get();
}
//reset the count
count=0;
cout< }
}
//close the stream
in_stream.close();
}
Explanation / Answer
#include <iostream>
#include <fstream>
#include <stdio.h>
using namespace std;
int main (int argc, char *argv[])
{
ifstream in_stream;
string Filename;
cout << "Enter the name of the file: ";
cin >> Filename;
//open the file
in_stream.open(Filename.c_str());
string data;
//count the no of lines read and printed
int count=0;
//loop till end of the file
while(!in_stream.eof())
{
//read the file data
count++;
getline(in_stream,data);
//if no of lines printed are more than 24 then ask for a key
if(count > 24)
{
cout << "Press any key to continue displaying the rest of the lines: ";
fflush(stdin);
getchar();
count=0;
cout << data << endl;
}
else
{
cout << data << endl;
}
}
//close the stream
in_stream.close();
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.