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
can u solve this question
1. Unsigned Integers
If we have an n-digit unsigned numeral dn-1d n-2...do in radix (or base) r, then the value of that
numeral is
n−1
r² di
Σi=0
which is basically saying that instead of a 10's or 100's place we have an r's or
r²'s place. For binary, decimal, and hex r equals 2, 10, and 16, respectively.
Just a reminder that in order to write down a large number, we typically use the IEC or SI
prefixing system:
IEC: Ki = 210, Mi = 220, Gi = 230, Ti = 240, Pi = 250, Ei = 260, Zi = 270, Yi = 280;
SI: K=103, M = 106, G = 109, T = 10¹², P = 1015, E = 10¹8, Z = 1021, Y = 1024.
1.1 Conversions
a. (15 pts) Write the following using IEC prefixes: 213, 223, 251, 272, 226, 244
21323 Ki8 Ki
223 23 Mi 8 Mi
b. (15 pts) Write the following using SI prefixes: 107, 10¹7, 10¹¹, 1022, 1026, 1015
107 10¹ M = 10 M
=
1017102 P = 100 P
c. (10 pts) Write the following with powers of 10: 7 K, 100 E, 21 G
7 K = 7*10³
answer shoul avoid using AI and should be basic and please explain
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
- Node A is connected to node B by a 2000km fiber link having a bandwidth of 100Mbps. What is the total latency time (transmit + propagation) required to transmit a 4000 byte file using packets that include 1000 Bytes of data plus 40 Bytes of header.arrow_forwardanswer should avoid using AI and should be basic and explain pleasearrow_forwardasnwer should avoid using AIarrow_forward
- answer should avoid using AI (such as ChatGPT), do not any answer directly copied from AI would and explain codearrow_forwardWrite a c++ program that will count from 1 to 10 by 1. The default output should be: 1, 2, 3, 4, 5, 6 , 7, 8, 9, 10 There should be only a newline after the last number. Each number except the last should be followed by a comma and a space. To make your program more functional, you should parse command line arguments and change behavior based on their values. Argument Parameter Action -f, --first yes, an integer Change place you start counting -l, --last yes, an integer Change place you end counting -s, --skip optional, an integer, 1 if not specified Change the amount you add to the counter each iteration -h, —help none Print a help message including these instructions. -j, --joke none Tell a number based joke. So, if your program is called counter, counter -f 10 --last 4 --skip 2 should produce 10, 8, 6, 4 Please use the last supplied argument. If your code is called counter, counter -f 4 -f 5 -f 6 should count from 6. You should…arrow_forwardshow workarrow_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 PtrC++ Programming: From Problem Analysis to Program...Computer ScienceISBN:9781337102087Author:D. S. MalikPublisher:Cengage Learning
C++ for Engineers and Scientists
Computer Science
ISBN:9781133187844
Author:Bronson, Gary J.
Publisher:Course Technology Ptr
C++ Programming: From Problem Analysis to Program...
Computer Science
ISBN:9781337102087
Author:D. S. Malik
Publisher:Cengage Learning