Question

In: Computer Science

Working with Files in C++. Create the following  program called payroll.cpp.  Note that the file you...

Working with Files in C++.

Create the following  program called payroll.cpp.  Note that the file you read must be created before you run this program.  The output file will be created automatically by the program.  You can save the input file in the same directory as your payroll.cpp file by using Add New Item, Text File.  

// File:  Payroll.cpp

// Purpose:  Read data from a file and write out a payroll

// Programmer:  (your name and section)

#include <cstdlib> // for the definition of EXIT_FAILURE

#include <fstream> // required for external file streams

#include <iostream> // required for cin cout

using namespace std;

int main ()

{

ifstream ins; // associates ins as an input stream

 ofstream outs; // associates outs as an output stream

 int id; // id for employee

 double hours, rate; // hours and rate worked

 double pay; // pay calculated

 double total_pay; // grand total of pay

 // Open input and output file, exit on any error

 ins.open ("em_in.txt"); // ins connects to file "em_in.txt"

 if (ins.fail ())

 {

   cout << "*** ERROR: Cannot open input file. " << endl;

   getchar(); //  hold the screen

   return EXIT_FAILURE;

 } // end if

 outs.open ("em_out.txt"); // outs connects to file "em_out.txt"

 if (outs.fail ())

 {

   cout << "*** ERROR: Cannot open output file." << endl;

   getchar();

   return EXIT_FAILURE;

 } // end if

// Set total_pay to 0

total_pay = 0;

ins >> id; // get first id from file

// Do the payroll while the id number is not the sentinel value

while (id != 0)

 {

   ins >> hours >> rate;

   pay = hours * rate;

   total_pay += pay;

   outs << "For  employee " << id << endl;

   outs << "The pay is " << pay << " for " << hours

<< " hours worked at " << rate << " rate of pay" << endl << endl;

   ins >> id;

 } // end while

 // Display a message on the screen

   cout << "Employee processing finished" << endl;

   cout << "Grand total paid out is " << total_pay << endl;

   ins.close(); // close input file stream

   outs.close(); // close output file stream

   return 0;

 }

Create the input file:

Inside C++ go to  File Add New Item and then Text to create a text file.  

Type in the data below

In the same directory as your .cpp file for Payroll.cpp click Files and Save As em_in.txt

1234

35  10.5

3456

40 20.5

0



solution should show :

-the output file  

-the input file

-the screen output

-the source program

Solutions

Expert Solution

Please find your answers below and let me know if you have any doubts or if you need anything to change. If you are satisfied with the solution, please rate the answer. Thanks

source file, properly formatted:

// File:  Payroll.cpp
// Purpose:  Read data from a file and write out a payroll
// Programmer:  (your name and section)
#include <cstdlib> // for the definition of EXIT_FAILURE
#include <fstream> // required for external file streams
#include <iostream> // required for cin cout
using namespace std;
int main()
{
    ifstream ins; // associates ins as an input stream
    ofstream outs; // associates outs as an output stream
    int id; // id for employee
    double hours, rate; // hours and rate worked
    double pay; // pay calculated
    double total_pay; // grand total of pay
    // Open input and output file, exit on any error
    ins.open("em_in.txt"); // ins connects to file "em_in.txt"
    if (ins.fail()) {
        cout << "*** ERROR: Cannot open input file. " << endl;
        getchar(); //  hold the screen
        return EXIT_FAILURE;
    } // end if
    outs.open("em_out.txt"); // outs connects to file "em_out.txt"
    if (outs.fail()) {
        cout << "*** ERROR: Cannot open output file." << endl;
        getchar();
        return EXIT_FAILURE;
    } // end if
    // Set total_pay to 0
    total_pay = 0;
    ins >> id; // get first id from file
    // Do the payroll while the id number is not the sentinel value
    while (id != 0) {
        ins >> hours >> rate;
        pay = hours * rate;
        total_pay += pay;
        outs << "For  employee " << id << endl;
        outs << "The pay is " << pay << " for " << hours
             << " hours worked at " << rate << " rate of pay" << endl
             << endl;
        ins >> id;
    } // end while
    // Display a message on the screen
    cout << "Employee processing finished" << endl;
    cout << "Grand total paid out is " << total_pay << endl;
    ins.close(); // close input file stream
    outs.close(); // close output file stream
    return 0;
}

em_in.txt file (should be placed in the same directory as your cpp file)

1234
35  10.5
3456
40 20.5
0

em_out.txt file (generated after running the program)

For  employee 1234
The pay is 367.5 for 35 hours worked at 10.5 rate of pay

For  employee 3456
The pay is 820 for 40 hours worked at 20.5 rate of pay

console output generated after running the program

Employee processing finished
Grand total paid out is 1187.5

screenshots of everything


Related Solutions

In C++ You will create 3 files: The .h (specification file), .cpp (implementation file) and main...
In C++ You will create 3 files: The .h (specification file), .cpp (implementation file) and main file. You will practice writing class constants, using data files. You will add methods public and private to your BankAccount class, as well as helper methods in your main class. You will create an arrayy of objects and practice passing objects to and return objects from functions. String functions practice has also been included. You have been given a template in Zylabs to help...
(Use C++ language) CREATE A PROGRAM CALLED and or not.cpp THAT: ASKS USER FOR AGE Note:...
(Use C++ language) CREATE A PROGRAM CALLED and or not.cpp THAT: ASKS USER FOR AGE Note: you must be 18 or older to vote && is the symbol for And | | is the symbol for Or ! is the symbol for Not USE AND, OR, OR NOT TO SEE IF THEY'RE OLD ENOUGH TO VOTE and display appropriate message USE AND, OR, OR NOT TO SEE IF THEY ENTERED A NUMBER LESS THAN 0 and display appropriate message USE...
Need to create a program in C++ that can display/write into a file called marks.txt. I'm...
Need to create a program in C++ that can display/write into a file called marks.txt. I'm not too worried about the functions, but I don't know how to store the different marks into a arrays. Any help would be appreaciated. Here's some details about the assignment. Student marks are kept in a text file as a single column. Each student may have a different number of assessments and therefore scores. The data recorded in the file for each student start...
Write a C++ program in a file called pop.cpp that opens a file called pop.dat which...
Write a C++ program in a file called pop.cpp that opens a file called pop.dat which has the following data (you’ll have to create pop.dat) AX013 1.0 BX123456 1234.56 ABNB9876152345 99999.99 The data are account numbers and balances. Account numbers are, at most 14 characters. Balances are, at most, 8 characters (no more than 99999.99). Using a loop until the end of file, read an account number and balance then write these to standard output in the following format shown...
c# Create a console application that protects an XML file, such as the following example. Note...
c# Create a console application that protects an XML file, such as the following example. Note that the customer's credit card number and password are currently stored in clear text. The credit card must be encrypted so that it can be decrypted and used later, and the password must be salted and hashed: <?xml version="1.0" encoding="utf-8" ?> <customers> <customer> <name>Bob Smith</name> <creditcard>1234-5678-9012-3456</creditcard> <password>Pa$$w0rd</password> </customer> </customers>
In C++ Create a class called Rational (separate the files as shown in the chapter) for...
In C++ Create a class called Rational (separate the files as shown in the chapter) for performing arithmetic with fractions. Write a program to test your class. Use integer variables to represent the private data of the class-the numerator and the denominator. Provide a constructor that enables an object of this class to be initialized when it's declared. The constructor should contain default values in case no initializers are provided and should store the fraction in reduced form. For example,...
Create this C++ program using classes 1. Create a file text file with a string on...
Create this C++ program using classes 1. Create a file text file with a string on it 2. Check the frecuency of every letter, number and symbol (including caps) 3. Use heapsort to sort the frecuencys found 4. Use huffman code on the letters, symbols or numbers that have frecuencys I created the file, and the frecuency part but i'm having trouble with the huffman and heapsort implementation.
Create a class Student (in the separate c# file but in the project’s source files folder)...
Create a class Student (in the separate c# file but in the project’s source files folder) with those attributes (i.e. instance variables): Student Attribute Data type (student) id String firstName String lastName String courses Array of Course objects Student class must have 2 constructors: one default (without parameters), another with 4 parameters (for setting the instance variables listed in the above table) In addition Student class must have setter and getter methods for the 4 instance variables, and getGPA method...
Write a C++ program that creates a file called Readings.txt. Inside the file, your program must...
Write a C++ program that creates a file called Readings.txt. Inside the file, your program must create a list. The list is composed of integer double pairs. There is one pair per line. The integers are in sequence (0, 1, 2, 3, ...) beginning with zero and ending with some random value between 512 and 1024. The doubles should be random values between 50.000 and 90.000. The doubles only have 3 decimal places. The file should look like this (of...
Write a C++ program to create a text file. Your file should contain the following text:...
Write a C++ program to create a text file. Your file should contain the following text: Batch files are text files created by programmer. The file is written in notepad. Creating a text file and writing to it by using fstream: to write to a file, you need to open thew file as write mode. To do so, include a header filr to your program. Create an object of type fsrteam. Open the file as write mode. Reading from a...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT