Functions Programming in C++ Computer Science Dept Va Tech August 2000 ©2000 William D McQuain 1 #include // for file streams for input/output #include.

Slides:



Advertisements
Similar presentations
C++ Basics March 10th. A C++ program //if necessary include headers //#include void main() { //variable declaration //read values input from user //computation.
Advertisements

CPS120: Introduction to Computer Science INPUT/OUTPUT.
Numeric Types, Expressions, and Output ROBERT REAVES.
1 11/3/08CS150 Introduction to Computer Science 1 Reading from and Writing to Files Section 3.12 & 13.1 & 13.5.
1 Lecture 6: Input/Output (II) Introduction to Computer Science Spring 2006.
1 Lecture 19 Chapter 4 Program Input and the Software Design Process Dale/Weems/Headington.
1 CS 105 Lecture 9 Files Version of Mon, Mar 28, 2011, 3:13 pm.
1 10/29/07CS150 Introduction to Computer Science 1 Reading from and Writing to Files Section 3.12 & 13.1 & 13.5.
1 9/08/06CS150 Introduction to Computer Science 1 Arithmetic Operators.
Computer Science 1620 Formatting. Suppose you work for the HR dept. of a company you wish to write a program to show their earnings per month Details:
1 CS150 Introduction to Computer Science 1 Exponents & Output page & Section 3.8.
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 3: Input/Output.
C++ Programming: From Problem Analysis to Program Design, Fifth Edition Chapter 3: Input/Output.
1 10/25/06CS150 Introduction to Computer Science 1 Reading from and Writing to Files.
1 9/26/07CS150 Introduction to Computer Science 1 Exponents & Output page & Section 3.8.
1 11/8/06CS150 Introduction to Computer Science 1 More Functions! page 343 November 8, 2006.
Chapter 3: Input/Output
A function (procedure) code to perform a task (carry out an algorithm) Return_type Func(parameters) ---- Func (arguments) --- Return value.
Chapter 11: Structured Data. Slide Introduction An array makes it possible to access a list or table of data of the same data type by using a single.
Introduction to Programming (in C++) Data and statements Jordi Cortadella, Ricard Gavaldà, Fernando Orejas Dept. of Computer Science, UPC.
Streams, Files, and Formatting Chapter Standard Input/Output Streams t Stream is a sequence of characters t Working with cin and cout t Streams.
6. Iteration Intro Programming in C++ Computer Science Dept Va Tech August, 2002 © Barnette ND & McQuain WD 1 Iteration pass (or iteration)-one.
Chapter 8 Streams and Files Lecture Notes Prepared By: Blaise W. Liffick, PhD Department of Computer Science Millersville University Millersville, PA
Computer Arithmetic Programming in C++ Computer Science Dept Va Tech August, 2000 © Barnette ND, McQuain WD, Keenan MA 1 Independent Representation.
4. Input/Output Intro Programming in C++ Computer Science Dept Va Tech August, 2002 © Barnette ND & McQuain WD 1 C++ Input/Output: Streams The.
CHAPTER 3 INPUT/OUTPUT. In this chapter, you will:  Learn what a stream is and examine input and output streams  Explore how to read data from the standard.
C++ Programming: From Problem Analysis to Program Design, Third Edition Chapter 3: Input/Output.
Exposure C++ Chapter VII Program Input and Output.
C++ Programming: Program Design Including Data Structures, Fifth Edition Chapter 3: Input/Output.
1 Chapter 4 Program Input and the Software Design Process Dale/Weems/Headington.
Lecture 6: Expressions and Interactivity (Part II) Professor: Dr. Miguel Alonso Jr. Fall 2008 CGS2423/COP1220.
First steps Jordi Cortadella Department of Computer Science.
Copyright 2004 Scott/Jones Publishing Alternate Version of STARTING OUT WITH C++ 4 th Edition Chapter 3 Formatting Output.
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 3: Input/Output.
I/O and Data Formatting Introduction to Class Concepts INFSY 307 Spring 2003 Lecture 3.
Input/Output Sujana Jyothi C++ Workshop Day 2. C++ I/O Basics 2 I/O - Input/Output is one of the first aspects of programming that needs to be mastered:
4. Input/Output Intro Programming in C++ Computer Science Dept Va Tech August, 2001 © Barnette ND & McQuain WD 1 C++ Input/Output: Streams The.
1 Chapter 4 Program Input and the Software Design Process Dale/Weems/Headington.
Chapter 3: Input/Output
Quiz // // The function exchanges the two parameters. // Param: ( ) // Param:
Operating System Using setw and setprecision functions Using setiosflags function Using cin function Programming 1 DCT
Chapter 3: Input/Output. Objectives In this chapter, you will: – Learn what a stream is and examine input and output streams – Explore how to read data.
1 Manipulators manipulators are used only in input and output statements endl, fixed, showpoint, setw, and setprecision are manipulators that can be used.
Prof. amr Goneid, AUC1 CSCE 110 PROGRAMMING FUNDAMENTALS WITH C++ Prof. Amr Goneid AUC Part 9. Streams & Files.
GE 211 Programming in C ++ Dr. Ahmed Telba Room :Ac -134
Chapter 05 (Part II) Control Statements: Part II.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 8: Streams and Files Problem Solving, Abstraction, and Design using.
Taxes and Your Paycheck Career and Financial Management 2.
Functions Procedural Abstraction Flow of Control INFSY 307 Spring 2003 Lecture 4.
Chapter 13 Preparing Payroll Records Part II. Payroll Register A business form used to record payroll information.
Income Taxes for Individuals Calculated as a percent of your taxable income Taxable Income = Gross Pay - Pre-Tax Deductions (examples: retirement plan.
Intro Programming in C++ Computer Science Dept Va Tech August, 2001 © Barnette ND & McQuain WD 1 Pass-by-Value - default passing mechanism except.
Chapter 3: Input/Output. Objectives In this chapter, you will: – Learn what a stream is and examine input and output streams – Explore how to read data.
INPUT & OUTPUT 10/20/2016Department of Computer Science, UOM | Introduction | Fakhre Alam.
Topic 2 Input/Output.
Introduction to C++ (Extensions to C)
Introduction to Programming
CPS120: Introduction to Computer Science
CPS120: Introduction to Computer Science
Input and Output Chapter 3.
Enumeration used to make a program more readable
Chapter 3 Input output.
Chapter 3: Expressions and Interactivity
CS150 Introduction to Computer Science 1
Formatted Input, Output & File Input, Output
CS150 Introduction to Computer Science 1
CS 144 Advanced C++ Programming February 5 Class Meeting
Reading from and Writing to Files
Reading from and Writing to Files Part 2
Reading from and Writing to Files
Presentation transcript:

Functions Programming in C++ Computer Science Dept Va Tech August 2000 ©2000 William D McQuain 1 #include // for file streams for input/output #include // for formatting manipulators #include // for string variables using namespace std; // Reads the employee's pay data from the input file. void GetData(ifstream& In, string& EmpID, double& HWorked, double& HRate, double& FITrate, double& SITrate, double& Insrate); // Calculates employee's gross pay amount: double Gross(double HWorked, double HRate); // Calculate employee's tax withholding amounts: double FIT(double Gross, double FITRate); double FICA(double Gross); double SIT(double Gross, double SITRate); double FMED(double Gross); // Prints out the employee's pay stub. void WriteResults(string EmpID, double Gross, double FIT, double FICA, double FMED, double SIT, double Ins, double Net); //... continues... Simple Payroll Program (P3)

Functions Programming in C++ Computer Science Dept Va Tech August 2000 ©2000 William D McQuain 2 void main() { ifstream In("PayData.txt"); // Open the input file. string EmployeeID; // employee's ID number double HoursWorked, // hours employee worked this pay period HourlyRate, // employee's hourly pay rate FITrate, // FIT rate SITrate, // SIT rate InsPremium; // insurance premium double GrossPay, // employee's gross pay amount (hours * rate) FITWithheld, // FIT withholding amount FICAWithheld, // FICA withholding amount FMEDWithheld, // FMED withholding amount SITWithheld, // SIT withholding amount NetPay; // net pay amount (gross - all deductions) GetData(In, EmployeeID, HoursWorked, HourlyRate, FITrate, SITrate, InsPremium); In.close(); //... continues... Simple Payroll Program (P3)

Functions Programming in C++ Computer Science Dept Va Tech August 2000 ©2000 William D McQuain 3 // Calculate the gross pay for this period: GrossPay = Gross(HoursWorked, HourlyRate); // Calculate the deductions for this period: FITWithheld = FIT(GrossPay, FITrate); FICAWithheld = FICA(GrossPay); SITWithheld = FIT(GrossPay, SITrate);; FMEDWithheld = FMED(GrossPay); // Calculate the net pay for this period: NetPay = GrossPay - FITWithheld - FICAWithheld - SITWithheld - FMEDWithheld - InsPremium; WriteResults(EmployeeID, GrossPay, FITWithheld, FICAWithheld, FMEDWithheld, SITWithheld, InsPremium, NetPay); } //... continues... Simple Payroll Program (P3)

Functions Programming in C++ Computer Science Dept Va Tech August 2000 ©2000 William D McQuain 4 /////////////////////////////////////////////////////////////// GetData() // // GetData() reads the employee's payroll data from the input file. // // Input/Output parameter: // In input stream // // Output parameters: // EmpID employee's ID string // HWorked # of hours employee has worked in this period // HRate hourly pay rate for employee // FITrate rate for computing FIT withholding // SITrate rate for computing SIT withholding // Insrate amount of insurance premium // // Preconditions: // In has been opened on a properly formatted input file. // // Postconditions: // The specified data has been read and stored in the corresponding // parameters. // // Called by: main() // Calls: none Simple Payroll Program (P3)

Functions Programming in C++ Computer Science Dept Va Tech August 2000 ©2000 William D McQuain 5 void GetData(ifstream& In, string& EmpID, double& HWorked, double& HRate, double& FITrate, double& SITrate, double& Insrate) { const char Delimiter = '|'; // label separator in pay data file In.ignore(255, '\n'); // ignore the header line In >> EmpID; // read employee ID In.ignore(255, Delimiter); // ignore everything through hours worked // label // Read employee's pay data: In >> HWorked; // Read the # of hours worked this period. In.ignore(255, Delimiter); // Advance to the next value. In >> HRate; // Read the hourly pay rate. In.ignore(255, Delimiter); // Advance to the next value. In >> FITrate; // Read the FIT rate. In.ignore(255, Delimiter); // Advance to the next value. In >> SITrate; // Read the SIT rate. In.ignore(255, Delimiter); // Advance to the next value. In >> Insrate; // Read the insurance premium amount. return; } Simple Payroll Program (P3)

Functions Programming in C++ Computer Science Dept Va Tech August 2000 ©2000 William D McQuain 6 /////////////////////////////////////////////////////// NOTE!! // Function header comments omitted to save space in notes. // double Gross(double HWorked, double HRate) { return (HWorked * HRate); } double FIT(double Gross, double FITRate) { return (Gross * FITRate / 100.0); } double FICA(double Gross) { const double FICArate = 1.45; return (Gross * FICArate / 100.0); } Simple Payroll Program (P3)

Functions Programming in C++ Computer Science Dept Va Tech August 2000 ©2000 William D McQuain 7 /////////////////////////////////////////////////////////// WriteResults() // // WriteResults() writes the employee's payroll stub to the output file. // // Input parameters: // EmpID employee's ID string // Gross employee's gross pay amount for this period // FIT FIT withholding for this period // FICA FICA withholding for this period // FMED FMED withholding for this period // SIT SIT withholding for this period // Ins insurance premium for this period // Net employee's net pay amount for this period // // Preconditions: // Each of the function parameters has been assigned the correct value. // // Postconditions: // A pay stub, formatted as specified, has been printed to the output file // "PayStub.txt". // // Called by: main() // Calls: none // Simple Payroll Program (P3)

Functions Programming in C++ Computer Science Dept Va Tech August 2000 ©2000 William D McQuain 8 void WriteResults(string EmpID, double Gross, double FIT, double FICA, double FMED, double SIT, double Ins, double Net) { ofstream Out("PayStub.txt"); // Open the output file. // Enable floating-point output formatting. Out << fixed << showpoint; // Write the header information to the output file: Out << "Programmer: Bill McQuain" << endl; Out << "CS 1044 Project 3 Fall 2000" << endl; Out << endl; // Mark the beginning of the pay stub: Out << " " << endl; // Write out the pay stub: Out << "Pay data for: " << EmpID << endl; Out << endl; //... continues... Simple Payroll Program (P3)

Functions Programming in C++ Computer Science Dept Va Tech August 2000 ©2000 William D McQuain 9 Out << "Gross pay: " << setw(8) << setprecision(2) << Gross << endl; Out << endl; Out << "Tax deductions: FIT FMED FICA SIT" << endl; Out << endl; Out << setw(21) << setprecision(2) << FIT << setw( 9) << setprecision(2) << FICA << setw(10) << setprecision(2) << FMED << setw( 9) << setprecision(2) << SIT << endl; Out << endl; Out << "Other deductions: Health Ins" << endl; Out << setw(28) << setprecision(2) << Ins << endl; Out << endl; Out << "Net pay: " << setw(8) << setprecision(2) << Net << endl; // Mark the end of the pay stub: Out << " " << endl; // Close the output file: Out.close(); return; } Simple Payroll Program (P3)