Write a program that asks the user for the name of a file. The program should di
ID: 3772544 • 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 files contents wont 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.
Note: Using an editor, you should create a simple text file that can be used to test this program.
This is a programming challenge in a C++ textbook, so needless to say I need to write it in C++. Thanks in advance for the help guys and gals.
EDIT: I have my code here, but I'm trying to get Visual Studio to pick up the txt file i created and its not working. Help appreciated!
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
int main(void){
string fileName;
cout << "Input file Name: ";
cin >> fileName;
cout << endl;
ifstream file((char*)fileName.c_str());
string stream;
while (!file.eof()){
int lines = 0;
while (!file.eof() && lines < 24){
getline(file, stream);
cout << stream << endl;
lines++;
}
if (!file.eof())
system("PAUSE");
}
}
Explanation / Answer
I have modified your program and run it. It is working perfectly.
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
int main(void){
string fileName;
int lines=0;
cout << "Input file Name: ";
cin >> fileName;
cout << endl;
ifstream file((char*)fileName.c_str());
string stream;
while (!file.eof()){
getline(file, stream);
lines++;
if(lines <= 24)
{
cout << stream << endl;
lines++;
}
else
{
lines=0;
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.