C++ for Engineers and Scientists
C++ for Engineers and Scientists
4th Edition
ISBN: 9781133187844
Author: Bronson, Gary J.
Publisher: Course Technology Ptr
Question
Book Icon
Chapter 8, Problem 1PP

(a)

Program Plan Intro

Program Plan:

  • Include library files for various operations.
  • Declare object of ofstream.
  • Create a file named “pay.dat” to write contents.
  • Use if statement with fail() method to check that the entered file name is available or not.
  • Read data from the program and write data to the file.
  • int main() function is used to perform all the task.
  • Display the calculated results to the user.

Program Description: The main purpose of this program is to create the “pay.dat” file and to store the given data in the file.

(a)

Expert Solution
Check Mark

Explanation of Solution

Program:

//including necessary header files
#include<iostream>
#include<fstream>
#include<cstdlib>
#include<string>
#include<iomanip>
usingnamespace std;
int main() { 
string filename = "pay.dat";
// ofstream object
ofstream outFile; 
// open file 
outFile.open(filename.c_str());
// check if file is opened successfully 
if (outFile.fail()) 
{
// display message
    cout <<"The file was not successfully opened"<<endl;
    exit(1);
    } 
// Send data to be written to the file using the 
     outFile <<"Callaway, G."<<"\t"<<16.00<<"  \t"<<40<<endl;
     outFile <<"Hanson, P."<<"  \t"<<15.00<<"  \t"<<48<<endl;
     outFile <<"Lasard, D. "<<"  \t"<<16.50<<"\t"<<35<<endl;
     outFile <<"Stillman, W."<<"\t"<<18.00<<"  \t"<<50<<endl;

// close file 
 outFile.close();
cout<<"File is created successfully!!!!!";

}

Sample output:

C++ for Engineers and Scientists, Chapter 8, Problem 1PP , additional homework tip  1

File- pay.dat

C++ for Engineers and Scientists, Chapter 8, Problem 1PP , additional homework tip  2

(b)

Program Plan Intro

Program Plan:

  • Include library files for various operations.
  • Declare an object of ifstream.
  • Create a statement to open the given file “pay.dat”.
  • Use if statement with fail() method to check that the entered file name is available or not.
  • Use while loop to read the file.
  • Calculate Regular pay, by using the below given formula:

  Regular pay=pay amount*hours

  • Calculate Overtime pay, by using the below given formula:

  overtime pay=(hours-40)*(rates*1.5)

  • int main() function is used to perform all the task.
  • Display the calculated results to the user.

Program Description: The main purpose of this program is to modify the program code given in part (a), so that the program can accept data from the given file “pay.dat” and display the name, rate, hours, regular payment, overtime, gross pay, totals of the regular, overtime, and gross pay on the console.

(b)

Expert Solution
Check Mark

Explanation of Solution

Program:

//including necessary header files
#include<iostream>
#include<fstream>
#include<cstdlib>
#include<string>
#include<iomanip>
usingnamespace std;
//main method
int main() 
{ 
//declaring variables
string filename = "pay.dat";
string first, second; 
int hours;
double rates, regPay, overPay=0, total=0, totalOvertym=0, totalGross=0;
// ifstream object
ifstream in; 
// open file 
in.open(filename.c_str());
// check if file is opened successfully 
if (in.fail()) 
{
// display message
    cout <<"The file was not successfully opened"<<endl;
    exit(1);
    } 
in>>first>>second>>rates>>hours;
    cout<<"Name"<<"\t\tRate"<<"\t Hours"<<"\tRegular"<<"\tOvertime"<<"\t GrossPay"<<endl;
//while loop
while(in.good())
    {
//if working hours are 40 or less
if(hours<=40)
        {
//calculating pay
            regPay=hours*rates;
            overPay=0;
        }
//if pay is greater than 40
if(hours>40)
        {
//calculating pay
            regPay=40* rates;
            overPay=(hours-40)*(rates*1.5);
        }
//displaying message to the user
        cout<<first<<" "<<second<<"\t"<<rates<<"\t"<<hours<<"\t"<<regPay
<<"\t"<<overPay<<"\t\t"<<(regPay+overPay)<<endl;
//calculating total amount
        total=total+regPay;
        totalOvertym=totalOvertym+overPay;
        totalGross=totalGross+regPay+overPay;
//reading data from the file
in>>first>>second>>rates>>hours;

    }
//displaying calculated results to the user
    cout<<"______________________________________________________________"<<endl;
    cout<<"Total \t\t\t\t"<<total<<"\t"<<totalOvertym<<"\t\t"<<totalGross;
// close file 
in.close();
}//end of main method

Sample output:

C++ for Engineers and Scientists, Chapter 8, Problem 1PP , additional homework tip  3

Want to see more full solutions like this?

Subscribe now to access step-by-step solutions to millions of textbook problems written by subject matter experts!
Students have asked these similar questions
Huffman Code[Problem Description]For an English article, the frequency of occurrence of 26 lowercase letters is counted, and then they are encoded by Hoffman code.[Basic Requirements]1)      Read the original text file from the file and display the text on the screen..2)      Output the number of occurrences of 26 English lowercase letters, and the corresponding Hoffman code... Below is the data to be displayed  At present, most of the dynamic sign language recognition is only for sign language words,the continuous sign language sentence recognition research and the corresponding results are less, because the segmentation of such sentence is very difficult. In this paper, a sign languagesentence recognition algorithm is proposed based on weighted key frames. Key frames can be regardedas the basic unit of sign word, therefore, according to the key frames we can get related vocabularies, and thus we can further organize these vocabularies into meaningful sentence. Such work can avoid the…
Problem 1: (Hint:  Use a sequential structure) How do you calculate the miles you are getting per gallon of gasoline if you record the mileage readings from your car’s odometer each time you fill up the gas tank?  Two odometer readings should be inputted, one reading is before you fill up the gas tank, and the second reading is when you refill the gas tank.  The third input is the quantity of gallons of gasoline purchased when the gas tank is refilled.  After inputting these 3 values, calculate and output:
Huffman Code [Problem Description] For an English article, the frequency of occurrence of 26 lowercase letters is counted, and then they are encoded by Hoffman code. [Basic Requirements] Read the original text file from the file and display the text on the screen.. Output the number of occurrences of 26 English lowercase letters, and the corresponding Hoffman code.  please do the code in c++
Knowledge Booster
Background pattern image
Similar questions
SEE MORE QUESTIONS
Recommended textbooks for you
Text book image
Database System Concepts
Computer Science
ISBN:9780078022159
Author:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:McGraw-Hill Education
Text book image
Starting Out with Python (4th Edition)
Computer Science
ISBN:9780134444321
Author:Tony Gaddis
Publisher:PEARSON
Text book image
Digital Fundamentals (11th Edition)
Computer Science
ISBN:9780132737968
Author:Thomas L. Floyd
Publisher:PEARSON
Text book image
C How to Program (8th Edition)
Computer Science
ISBN:9780133976892
Author:Paul J. Deitel, Harvey Deitel
Publisher:PEARSON
Text book image
Database Systems: Design, Implementation, & Manag...
Computer Science
ISBN:9781337627900
Author:Carlos Coronel, Steven Morris
Publisher:Cengage Learning
Text book image
Programmable Logic Controllers
Computer Science
ISBN:9780073373843
Author:Frank D. Petruzella
Publisher:McGraw-Hill Education