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...
please complete the following program in c++: In this homework assignment you will modify the program...
please complete the following program in c++: In this homework assignment you will modify the program you have developed in Homework Assignment 1 by implementing the following: 1- Your program will ask the user to choose to either Compute and Display Surface Areas of Hemispheres and their Average (Choice 1) or to Compute and Display Volumes of Hemispheres and their Average (Choice 2). The program will only perform the chosen operation. (Hint: use a character variable to read the character...
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. Write a program that reads the contents of two text files and compares...
In c++ please. Write a program that reads the contents of two text files and compares them in the following ways: It should display a list of all the unique words contained in both files. It should display a list of the words that appears in both files. It should display a list of the words that appears in the first file, but not the second. It should display a list of the words that appears in the second file,...
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...
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...
****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...
C-Language Modify the existing vector's contents, by erasing 200, then inserting 100 and 102 in the...
C-Language Modify the existing vector's contents, by erasing 200, then inserting 100 and 102 in the shown locations. Use Vector ADT's erase() and insert() only. Sample output of below program: 100 101 102 103 ---------------------------------------------------------------------------------- #include <stdio.h> #include <stdlib.h> // struct and typedef declaration for Vector ADT typedef struct vector_struct {    int* elements;    unsigned int size; } vector; // Initialize vector with specified size void vector_create(vector* v, unsigned int vectorSize) {    int i;    if (v ==...
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...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT