Question

In: Computer Science

C++ please Instructions Download and modify the Lab5.cpp program. Currently the program will read the contents...

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;

}

Solutions

Expert Solution

#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;
}

Related Solutions

PLEASE write the code in C++. employee.h, employee.cpp, and hw09q1.cpp is given. Do not modify employee.h...
PLEASE write the code in C++. employee.h, employee.cpp, and hw09q1.cpp is given. Do not modify employee.h and answer the questions in cpp files. Array is used, not linked list. It would be nice if you could comment on the code so I can understand how you wrote it employee.h file #include <string> using namespace std; class Employee { private:    string name;    int ID, roomNumber;    string supervisorName; public:    Employee();       // constructor    void setName(string name_input);   ...
Complete the given C++ program (prob1.cpp) to read an array of integers, print the array, and...
Complete the given C++ program (prob1.cpp) to read an array of integers, print the array, and then find the index of the largest element in the array. You are to write two functions, printArray() and getIndexLargest(), which are called from the main function. printArray() outputs integers to std::cout with a space in between numbers and a newline at the end. getIndexLargest () returns the index of the largest element in the array. Recall that indexes start at 0. If there...
C++ Download Lab10.cpp . In this file, the definition of the class personType has given. Think...
C++ Download Lab10.cpp . In this file, the definition of the class personType has given. Think of the personType as the base class. Lab10.cpp is provided below: #include <iostream> #include <string> using namespace std; // Base class personType class personType { public: void print()const; //Function to output the first name and last name //in the form firstName lastName. void setName(string first, string last); string getFirstName()const; string getLastName()const; personType(string first = "", string last = ""); //Constructor //Sets firstName and lastName...
In C++ please modify the following program and add characters (char) and names (strings) to be...
In C++ please modify the following program and add characters (char) and names (strings) to be added to the linked list along with integers. The current demo program accepts only integer data, so you would ask the user to select the data type to be added to the linked list. The user should be given the following three choices: (a) whole numbers (b) single characters (c) strings Once the user makes a selection from the list above then your program...
****user comments: PLEASE READ INSTRUCTIONS THOROUGHLY AND KEEP THE STRUCTURE OF THE PROGRAM. JUST ADD THE...
****user comments: PLEASE READ INSTRUCTIONS THOROUGHLY AND KEEP THE STRUCTURE OF THE PROGRAM. JUST ADD THE ADDITIVES NEEDED IN ORDER TO SUFFICE THE PROGRAM. PLEASE MAKE SURE IT IS IN C++ AND WORKS! THANK YOU!**** Write a program that uses a structure to store the following information for a particular month at the local airport: Total number of planes that landed Total number of planes that departed Greatest number of planes that landed in a given day that month Least...
Please read the instructions and  find attached for the first wiki . Instructions for students: Read carefully...
Please read the instructions and  find attached for the first wiki . Instructions for students: Read carefully the attached document and then post your comments bearing in mind the following questions: 1- What are the pros and cons of rent controls? 2- Why economists disagree on the usefulness of rent control? 3- Do you believe rent control can help the poor? Edit Wiki Content rent control Rent regulation can take various forms, including rent control (the placing of a cap on...
Exercise 5.1 Please provide code for both parts A and B. PART A Modify the P5_0.cpp...
Exercise 5.1 Please provide code for both parts A and B. PART A Modify the P5_0.cpp program (located below) and use the predefined function pow to compute the ab power. Here is the definition of the pow function: double pow (double base, double exponent); i.e. pow takes two parameters of type double, a and b and its value returned is of type double. You need to use pow in a statement like this: p = pow(a,b) Please note that in...
You are required to modify the attached simulation program. This program currently uses an array to...
You are required to modify the attached simulation program. This program currently uses an array to implement the queue. You will modify the program so that it uses the STL queue instead. /* This is for CSC 611 class at NSU Computer Science Department This program is a modified C++ version of the C program From the Second Edition Simulation Modeling & Analysis Averill M. Law W. David Kelton */ #include using namespace std; #include #include double time = 0;...
You are required to modify the attached simulation program. This program currently uses an array to...
You are required to modify the attached simulation program. This program currently uses an array to implement the queue. You will modify the program so that it uses the STL queue instead. #include <iostream> using namespace std; #include <math.h> #include<fstream> double time = 0; int seed = 1; /* External definitions for single-server queeing system. */ const int Q_LIMIT = 100000; /* Limit on queue length. */ const int BUSY = 1; /* Mnemonics for server's being busy */ const...
This assignment uses a combination of classes and arrays. Instructions: 1) download the 3 program files...
This assignment uses a combination of classes and arrays. Instructions: 1) download the 3 program files into a new C++ project.    NOTE: if your IDE does not allow you to create projects - or you are not sure how to do it - then you may combine the two cpp files into a single file. 2) Complete the program by adding code the the class methods. You may want to study the existing code first to get a feel...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT