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
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++
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:
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
- 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: The miles traveled between gas refills The average miles per gallon based on the miles traveled and the amount of gasoline purchased after the time the gas tank is refilled Algorithm: Data Example:arrow_forward(C++ Programming) You are given a file consisting of students’ names in the following form: lastName, firstName middleName. (Note that some students may not have a middle name.) Write a program that converts each name to the following form: firstName middleName lastName. Your program must read each student’s entire name into a variable and must contain a function that takes as input a string, consisting of a student’s name, and returns a string consisting of the altered name. • Use the string function find to find the index of , • the function length to find the length of the string • the function substr to extract the firstName, middleName and lastName. Output the altered name into a file. This program has no user interface. Banks,John William Barret,Ron Drew,Lucy Marie Perry,Mark G Smith,John Carrarrow_forwardQ1)There are 10 boxes of apples arranged in a row, assume the values have been stored in an arrayaryBox. Use C code to write a C program segment to find out which box is having the most number of applesas compared to other boxes. A sample output is as follow:Box number 5 contains the most number of apple.[Note: You need to declare all the necessary variables and use an efficient way to representthe data as well as solving the problem]arrow_forward
- (C++ Language) Create a function that writes to a file named Inventy.txt the information of 8 items. Ask the user for the item's name, the item's quantity in storage, and the item's price and write it to the file. Create a function that reads the file and items' quantity and price to two different arrays. Create a function that computes the subtotal of each item. Implement all functions inside the main program and the computed value should be displayed with the item's name.arrow_forward(In C++) This program should be designed and written by a team of students. Here are some suggestions: You may work on a single class. The parameter and return type s of each function and class member function should be decided in advance. The program will be best implemented as a multi-file program, (header file, the main program,..) You need to print all output to an output file and submit it to the instructor during the final exam. Design a generic class to hold the following information about a bank account: Balance Number of deposits this month Number of withdrawals Annual Interest Rate Monthly service charges The class should have the following member function: Constructors Accepts arguments for the balance and annual interest rate. deposit a virtual function that accepts an argument for the amount of the deposit. The function should add the argument to the account balance. It should also increment the variable holding the…arrow_forward(Use Python) The function course_average should calculate and return the average of the three values pass to it. The function main should ask the user to enter three grades and then pass these values to the course_average function. This function should also display a message to the user in the format below. For example, if the user entered 100, 90 and 95 the message would be:The average of 100 , 90 and 95 is 95arrow_forward
- 5. [C++: Program Output (30 points)] In the spaces provided below each of the following programs, write only one of the following in order to indicate how the program performs: • The phrase "Does not compile" indicating the program will never compile. • The phrase "Won't finish" indicating that the program will enter an infinite loop, crash, and/or somehow not finish properly. • The terminal output of the program when run using underscores to denote any spaces (i.e. 1.3.3.7). Assume that all necessary scaffolding is included and that no compiler flags (e.g. -Wall or -Werror) are used. (a) double phi(int n) { if (n > 1) return 1.0 + 1.0 / phi(n-1); return 1; } int main() { double answer; double i; for (unsigned int i = 1; i < 5; i++) { answer = phi(i); cout « i « " iterations, phi is: " <« answer << endl; } return 0; }arrow_forward(C++ Programming language) There are following two major issues associated with cpp programs:• When a program is terminated, the entire data is lost. • If you have to enter a large number of data, it will take a lot of time to enter them all in the different programs.Suggest a solution and elaborate the same with the help of suitable examples.arrow_forward[C++] Please help to develop C++ codes based on the requirements below. (Requirement) Poker Hand Ranking Develop a program that determines to rank and display poker hands' ranking. When reading and displaying use letter representations for cards, e.g. Rank: 23 4 5 6789 10 J Q KA Suit: S HDC Use the set of hands below to demonstrate functionality. For example, demonstrate the three Straight Flush examples below as well as all of the other examplesarrow_forward
- Q2) Write a C++ program to read and write from the file, three numbers (No.1 ,No.2 and No.3) and calculate the max. value using function?arrow_forward(a) Using the program above, Compute a monthly tax (in Namibian Dollars) payableby an individual earning N$980, 953 per annum, with a tax free monthly allowanceof N$15, 000. NB: Your answer to this question should be presented as a commentat the end of the main code, with all details.arrow_forward(In C++ Pls) This program should be designed and written by a team of students. Here are some suggestions: You may work on a single class. The parameter and return type s of each function and class member function should be decided in advance. The program will be best implemented as a multi-file program, (header file, the main program,..) You need to print all output to an output file and submit it to the instructor during the final exam. Design a generic class to hold the following information about a bank account: Balance Number of deposits this month Number of withdrawals Annual Interest Rate Monthly service charges The class should have the following member function: Constructors Accepts arguments for the balance and annual interest rate. deposit a virtual function that accepts an argument for the amount of the deposit. The function should add the argument to the account balance. It should also increment the variable holding the…arrow_forward
arrow_back_ios
SEE MORE QUESTIONS
arrow_forward_ios
Recommended textbooks for you
- Database System ConceptsComputer ScienceISBN:9780078022159Author:Abraham Silberschatz Professor, Henry F. Korth, S. SudarshanPublisher:McGraw-Hill EducationStarting Out with Python (4th Edition)Computer ScienceISBN:9780134444321Author:Tony GaddisPublisher:PEARSONDigital Fundamentals (11th Edition)Computer ScienceISBN:9780132737968Author:Thomas L. FloydPublisher:PEARSON
- C How to Program (8th Edition)Computer ScienceISBN:9780133976892Author:Paul J. Deitel, Harvey DeitelPublisher:PEARSONDatabase Systems: Design, Implementation, & Manag...Computer ScienceISBN:9781337627900Author:Carlos Coronel, Steven MorrisPublisher:Cengage LearningProgrammable Logic ControllersComputer ScienceISBN:9780073373843Author:Frank D. PetruzellaPublisher:McGraw-Hill Education
Database System Concepts
Computer Science
ISBN:9780078022159
Author:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:McGraw-Hill Education
Starting Out with Python (4th Edition)
Computer Science
ISBN:9780134444321
Author:Tony Gaddis
Publisher:PEARSON
Digital Fundamentals (11th Edition)
Computer Science
ISBN:9780132737968
Author:Thomas L. Floyd
Publisher:PEARSON
C How to Program (8th Edition)
Computer Science
ISBN:9780133976892
Author:Paul J. Deitel, Harvey Deitel
Publisher:PEARSON
Database Systems: Design, Implementation, & Manag...
Computer Science
ISBN:9781337627900
Author:Carlos Coronel, Steven Morris
Publisher:Cengage Learning
Programmable Logic Controllers
Computer Science
ISBN:9780073373843
Author:Frank D. Petruzella
Publisher:McGraw-Hill Education