C++ for Engineers and Scientists
4th Edition
ISBN: 9781133187844
Author: Bronson, Gary J.
Publisher: Course Technology Ptr
expand_more
expand_more
format_list_bulleted
Question
Chapter 8, Problem 1PP
(a)
Program Plan Intro
- 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
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:
File- pay.dat
(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:
- Calculate Overtime pay, by using the below given formula:
- 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
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:
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
pattern recognitionPCA algor'thm
Consider the following program: LOAD AC, IMMEDIATE(30)
ADD AC, REGISTER(R1)
STORE AC, MEMORY(20)
Given that the value of R1 is 50, determine the value stored at memory address 20 after the program is executed. Provide an explanation to support your answer.
Pattern RecognitonDecision Tree please write the steps not only last answer
Chapter 8 Solutions
C++ for Engineers and Scientists
Ch. 8.1 - Prob. 1ECh. 8.1 - (Practice) a. Write a set of two statements...Ch. 8.1 - Prob. 3ECh. 8.1 - Prob. 4ECh. 8.1 - Prob. 5ECh. 8.1 - Prob. 8ECh. 8.1 - Prob. 9ECh. 8.1 - Prob. 10ECh. 8.2 - Prob. 1ECh. 8.2 - (Practice and modify) a. Enter and run Program...
Ch. 8.2 - (Practice and modify) a. Write a C++ program that...Ch. 8.2 - (Practice) Determine the OS command or procedure...Ch. 8.2 - Prob. 5ECh. 8.2 - (Data processing) a. Write a C++ program that...Ch. 8.2 - Prob. 7ECh. 8.2 - Prob. 8ECh. 8.2 - Prob. 9ECh. 8.3 - Prob. 1ECh. 8.3 - Prob. 2ECh. 8.3 - Prob. 3ECh. 8.3 - Prob. 4ECh. 8.3 - Prob. 5ECh. 8.3 - Prob. 6ECh. 8.4 - Prob. 1ECh. 8.4 - Prob. 2ECh. 8.4 - Prob. 3ECh. 8.4 - Prob. 4ECh. 8.5 - (Practice) Write a C++ program to create the...Ch. 8.5 - Prob. 2ECh. 8.5 - Prob. 3ECh. 8.5 - Prob. 4ECh. 8.5 - Prob. 5ECh. 8 - Prob. 1PPCh. 8 - (Data processing) a. Store the following data in a...Ch. 8 - (Data processing) Write a C++ program that allows...Ch. 8 - (Data processing) Write a C++ program that permits...Ch. 8 - (Data processing) Write a C++ program that reads...Ch. 8 - (Data processing) Write a C++ program that reads...Ch. 8 - Prob. 7PPCh. 8 - (Data processing) A bank’s customer records are to...Ch. 8 - (Inventory) Create an ASCII file with the...
Knowledge Booster
Similar questions
- Pattern RecognitionPCA algorithmplease write the steps not only last answerarrow_forwardPlease original work Final Project: Part I Background Information: E-TechMart, established in 2005, has grown to become a leading global electronics retailer, renowned for its extensive range of electronic devices and accessories. With a robust online platform and numerous brick-and-mortar stores spread across various regions, E-TechMart serves millions of customers worldwide. The company's product offerings include the latest smartphones, laptops, tablets, smart home devices, and a variety of electronic accessories, catering to both individual consumers and businesses. E-TechMart has always prioritized customer satisfaction and aims to provide an exceptional shopping experience. This commitment is evident in their comprehensive customer service, competitive pricing, and a well-organized loyalty program that rewards repeat customers. Despite these strengths, E-TechMart faces increasing competition from other major players in the electronics retail market. To maintain its competitive…arrow_forwardCan you show me how to redesign the GameStop website using HTML and CSS using Sublime Text?arrow_forward
- Manhattan distancearrow_forwardAiwhat cost from Oradea to Goal which Eforie us A* Algorithm G(n)+H(n) last what ıs the costarrow_forwardWhat is the SELECT statement? give one reference with your answer What is a URL and what is it used for? give one reference with your answer What is e-mail, and what are its advantages? Give one reference with your answerarrow_forward
- What is the difference between the World Wide Web (WWW) and the Internet? Give two references from a journal along with your answer.arrow_forwardDiscuss with appropriate examples, the types of relationship in a database. Give two references from an article.arrow_forwardWhat is a cloud and why do we use it? Give one reference with your answer.arrow_forward
- What are triggers and how do you invoke a trigger on demand? give one reference with your anwer.arrow_forwardWhy is database normalization important? Give one reference with your answer.arrow_forwardDescribe the role of databases and database management systems in the context ofenterprise systems. Give two references with your answer.arrow_forward
arrow_back_ios
SEE MORE QUESTIONS
arrow_forward_ios
Recommended textbooks for you
- C++ for Engineers and ScientistsComputer ScienceISBN:9781133187844Author:Bronson, Gary J.Publisher:Course Technology Ptr
C++ for Engineers and Scientists
Computer Science
ISBN:9781133187844
Author:Bronson, Gary J.
Publisher:Course Technology Ptr