In: Computer Science
C++ please
Instructions
Download and modify the Lab5.cpp program.
Currently the program will read the contents of a file and display each line in the file to the screen. It will also display the line number followed by a colon. You will need to change the program so that it only display 24 lines from the file and waits for the user to press the enter key to continue.
Do not use the system(“pause”) statement.
Download Source Lab 5 File:
#include <iostream>
#include <string>
#include <iomanip>
#include <fstream>
using namespace std;
int main()
{
ifstream file; // File stream object
string name; // To hold the file name
string inputLine; // To hold a line of input
int lines = 0; // Line counter
int lineNum = 1; // Line number to display
// Get the file name.
cout << "Enter the file name: ";
getline(cin, name);
// Open the file.
file.open(name);
// Test for errors.
if (!file)
{
// There was an error so display an error
// message and end the program.
cout << "Error opening " << name << endl;
}
else
{
// Read the contents of the file and display
// each line with a line number.
while (!file.eof())
{
// Get a line from the file.
getline(file, inputLine, '\n');
// Display the line.
cout << setw(3) << right << lineNum
<< ":" << inputLine << endl;
// Update the line display counter for the
// next line.
lineNum++;
// Update the total line counter.
lines++;
}
// Close the file.
file.close();
}
return 0;
}
#include <iostream> #include <string> #include <iomanip> #include <fstream> using namespace std; int main() { ifstream file; // File stream object string name; // To hold the file name string inputLine; // To hold a line of input int lines = 0; // Line counter int lineNum = 1; // Line number to display // Get the file name. cout << "Enter the file name: "; getline(cin, name); // Open the file. file.open(name); // Test for errors. if (!file) { // There was an error so display an error // message and end the program. cout << "Error opening " << name << endl; } else { // Read the contents of the file and display // each line with a line number. while (!file.eof()) { // Get a line from the file. getline(file, inputLine, '\n'); // Display the line. cout << setw(3) << right << lineNum << ":" << inputLine << endl; // Update the line display counter for the // next line. lineNum++; // Update the total line counter. lines++; if (lines == 24) { lines = 0; cout << "Press the enter key to continue: "; getline(cin, inputLine, '\n'); } } // Close the file. file.close(); } return 0; }