Welcome to Dream.In.Code
Become a C++ Expert!

Join 137,261 C++ Programmers for FREE! Get instant access to thousands of C++ experts, tutorials, code snippets, and more! There are 1,510 people online right now. Registration is fast and FREE... Join Now!




Functions and parameter passing help

 
Reply to this topicStart new topic

Functions and parameter passing help, And it's due tomorrow! :(

richiewinters
6 Nov, 2006 - 05:53 PM
Post #1

New D.I.C Head
*

Joined: 6 Nov, 2006
Posts: 26


My Contributions
Hi everyone. I'm a n00bie both to the site and to C++ programming.

I've been working on this programming assignment this past week and I'm just stumped.
Probably something simple and stupid that I missed since I tend to do that and this
past week was rediculously busy (taking 5 classes this semester)

Anyway, here's the contents of the "payroll.txt" file accessed in the following program:
QUOTE
John Smith#10.45 40 15
Jane Doe#12.50 45 15
Harry Morgan#20.00 40 20
Carmen Martinez#25.00 35 25
Jacintha Washington#50.85 60 35


And here's what I have as far as code goes. I'm sure my math for the processPay function is wrong.
And I'm also sure something else is screwed up since it only seems to output 1 line.

Help!

CODE
#include <iostream>
#include <fstream>
#include <iomanip>
#include <string>
using namespace std;

void reportDescription();
void reportTitle();
void inputData(ifstream& inFileData, string& name, double& pay, double& time, double& tax);
void processPay(double payrate, double hours, double tax, double& grossPay, double& netPay, double& grossTotal, double& netTotal);
void printEmployeeInfo(string name, double pay, double time, double tax, double gross, double net);
void totalAmounts(double totalGross, double totalNet);

int main()
{    
    string empName;
    double payRate;
    double hoursWorked;
    double taxRate;
    double grossPay;
    double netPay;
    double grossPayTotal;
    double netPayTotal;

    ifstream inFileData;
    inFileData.open("c:\\payroll.txt");

    reportDescription();
    reportTitle();

    while (!inFileData.eof())
    {

        inputData(inFileData, empName, payRate, hoursWorked, taxRate);
        
    }
    inFileData.close();

    processPay(payRate, hoursWorked, taxRate, grossPay, netPay, grossPayTotal, netPayTotal);
    printEmployeeInfo(empName, payRate, hoursWorked, taxRate, grossPay, netPay);
    totalAmounts(grossPayTotal, netPayTotal);

    return 0;
}

void reportDescription()
{
    cout << "This program calculates paychecks for each employee. "<< endl
         << "A text file will be created outside of the program with the information: "<< endl
         << "name, rate of pay, number of hours worked, and tax percentage to be deducted." << endl
         << endl
         << "The program will create a report in columnar format showing the employee "<< endl
         << "name, hourly rate, number of hours worked, tax rate, gross pay, and net pay. "<< endl
         << endl
         << "After all employees are processed, totals will be displayed total gross amount "<< endl
         << "and total net pay." << endl
         << endl << endl;
}

void reportTitle()
{
    cout << setfill(' ') << right << setw(38) << "Payroll Report" << endl;
}

void inputData(ifstream& inFileData, string& name, double& pay, double& time, double& tax)
{
    getline(inFileData, name, '#');
    inFileData >> pay >> time >> tax;
    inFileData.ignore(100, '\n');

}

void processPay(double payrate, double hours, double tax, double& grossPay, double& netPay, double& grossPayTotal, double& netPayTotal)
{
    double temptax = netPay * tax;
    
    netPay = payrate * hours;
    grossPay = netPay - temptax;

    grossPayTotal = grossPayTotal + grossPay;
    netPayTotal = netPayTotal + netPay;

}

void printEmployeeInfo(string name, double pay, double time, double tax, double gross, double net)
{
    cout << fixed << showpoint << setprecision(2);  

    cout << left << setw(20) << "Employee" << setw(10) << "Hourly" << setw(10) << "Hours"  << setw(10) << "Tax" << setw(10) << "Gross" << setw(10) << "Net" << endl;
    cout << left << setw(20) << "Name" << setw(10) << "Rate" << setw(10) << "Worked" << setw(10) << "Rate" << setw(10) << "Amount" << setw(10) << "Amount" << endl;
    cout << endl;

    cout << left << setw(20) << name << setw(10) << setw(10) << pay << setw(10) << time << setw(10) << tax << setw(10) << gross << setw(10) << net << endl;

}

void totalAmounts(double totalGross, double totalNet)
{

    cout << left << setw(5) << "Totals" << right << setw(10) << totalGross << setw(10) << totalNet << endl;

}



User is offlineProfile CardPM
+Quote Post

NyeNye
RE: Functions And Parameter Passing Help
6 Nov, 2006 - 06:03 PM
Post #2

D.I.C Head
**

Joined: 24 Sep, 2006
Posts: 248


My Contributions
is their any errors....please if their are?




User is offlineProfile CardPM
+Quote Post

richiewinters
RE: Functions And Parameter Passing Help
6 Nov, 2006 - 06:34 PM
Post #3

New D.I.C Head
*

Joined: 6 Nov, 2006
Posts: 26


My Contributions
It compiles fine, with no errors. But the output isn't what it should be.

Here's a pic of what the output looks like:

IPB Image
User is offlineProfile CardPM
+Quote Post

NyeNye
RE: Functions And Parameter Passing Help
6 Nov, 2006 - 06:37 PM
Post #4

D.I.C Head
**

Joined: 24 Sep, 2006
Posts: 248


My Contributions
Oh,..i can't see your output from my pc...OMG

damn pc...

hmm..it's maybe some logic error or something..hmm

let me see..


User is offlineProfile CardPM
+Quote Post

richiewinters
RE: Functions And Parameter Passing Help
6 Nov, 2006 - 06:54 PM
Post #5

New D.I.C Head
*

Joined: 6 Nov, 2006
Posts: 26


My Contributions
I copied the text from the command line output. Let me see if it'll post as it looks on the screen:

CODE
This program calculates paychecks for each employee.
A text file will be created outside of the program with the information:
name, rate of pay, number of hours worked, and tax percentage to be deducted.

The program will create a report in columnar format showing the employee
name, hourly rate, number of hours worked, tax rate, gross pay, and net pay.

After all employees are processed, totals will be displayed total gross amount
and total net pay.


                        Payroll Report
Employee            Hourly    Hours     Tax       Gross     Net
Name                Rate      Worked    Rate      Amount    Amount

                    50.85     60.00     35.00     323958709722612380000000000000
0000000000000000000000000000000000.003051.00
Totals3147027465876806800000000000000000000000000000000000000000000000.00-925596
31349317830000000000000000000000000000000000000000000000.00
Press any key to continue . . .




That's basically what it looks like. It doesn't print any of the employee names and only looks like
it prints the last line of data from the file. Also, my math is obviously messed up somewhere. lol


User is offlineProfile CardPM
+Quote Post

NyeNye
RE: Functions And Parameter Passing Help
6 Nov, 2006 - 07:03 PM
Post #6

D.I.C Head
**

Joined: 24 Sep, 2006
Posts: 248


My Contributions
CODE


............

double payRate;
    double hoursWorked;
    double taxRate;
    double grossPay;
    double netPay;
    double grossPayTotal;
    double netPayTotal;
..


some of the declaration could be on int rather on double..


User is offlineProfile CardPM
+Quote Post

jayhuang
RE: Functions And Parameter Passing Help
6 Nov, 2006 - 08:10 PM
Post #7

New D.I.C Head
*

Joined: 11 Oct, 2006
Posts: 31


My Contributions
QUOTE(richiewinters @ 6 Nov, 2006 - 07:34 PM) *

It compiles fine, with no errors. But the output isn't what it should be.

Here's a pic of what the output looks like:

IPB Image



try to limit the decimal places of the outputs. There are three lines of commands for that. can't remember them now.
User is offlineProfile CardPM
+Quote Post

richiewinters
RE: Functions And Parameter Passing Help
6 Nov, 2006 - 08:22 PM
Post #8

New D.I.C Head
*

Joined: 6 Nov, 2006
Posts: 26


My Contributions
I already had that set up in one of the functions, but I added it to the other one that I thought could use it just now.

Didn't seem to help.

I think I have a looping problem somewhere (at least as part of the problem) because it seems like only the last line of the payroll.txt file prints to the screen.


User is offlineProfile CardPM
+Quote Post

richiewinters
RE: Functions And Parameter Passing Help
7 Nov, 2006 - 10:27 AM
Post #9

New D.I.C Head
*

Joined: 6 Nov, 2006
Posts: 26


My Contributions
Figured out the looping problems and all that, but still need help with the math in the processPay function.
User is offlineProfile CardPM
+Quote Post

horace
RE: Functions And Parameter Passing Help
7 Nov, 2006 - 02:04 PM
Post #10

D.I.C Addict
Group Icon

Joined: 25 Oct, 2006
Posts: 573



Thanked: 4 times
Dream Kudos: 50
My Contributions
QUOTE(richiewinters @ 7 Nov, 2006 - 06:27 PM) *

Figured out the looping problems and all that, but still need help with the math in the processPay function.

Should the output be?
CODE

This program calculates paychecks for each employee.
A text file will be created outside of the program with the information:
name, rate of pay, number of hours worked, and tax percentage to be deducted.

The program will create a report in columnar format showing the employee
name, hourly rate, number of hours worked, tax rate, gross pay, and net pay.

After all employees are processed, totals will be displayed total gross amount
and total net pay.


                        Payroll Report
Employee            Hourly    Hours     Tax       Gross     Net      
Name                Rate      Worked    Rate      Amount    Amount    

John Smith          10.45     40.00     15.00     355.30    418.00    
Jane Doe            12.50     45.00     15.00     478.13    562.50    
Harry Morgan        20.00     40.00     20.00     640.00    800.00    
Carmen Martinez     25.00     35.00     25.00     656.25    875.00    
Jacintha Washington 50.85     60.00     35.00     1983.15   3051.00  
Totals   4112.83   5706.50




program is now
CODE

#include <iostream>
#include <fstream>
#include <iomanip>
#include <string>
using namespace std;

void reportDescription();
void reportTitle();
void inputData(ifstream& inFileData, string& name, double& pay, double& time, double& tax);
void processPay(double payrate, double hours, double tax, double& grossPay, double& netPay, double& grossTotal, double& netTotal);
void printEmployeeInfo(string name, double pay, double time, double tax, double gross, double net);
void totalAmounts(double totalGross, double totalNet);

int main()
{    
    string empName;
    double payRate;
    double hoursWorked;
    double taxRate;
    double grossPay;
    double netPay;
    double grossPayTotal=0;                  // ** zero these totals
    double netPayTotal=0;

    ifstream inFileData;
    inFileData.open("payroll.txt");

    reportDescription();
    reportTitle();

    while (!inFileData.eof())
    {

        inputData(inFileData, empName, payRate, hoursWorked, taxRate);
        // ** moved following lines
    processPay(payRate, hoursWorked, taxRate, grossPay, netPay, grossPayTotal, netPayTotal);
    printEmployeeInfo(empName, payRate, hoursWorked, taxRate, grossPay, netPay);
        
    }
    inFileData.close();

    totalAmounts(grossPayTotal, netPayTotal);

    return 0;
}

void reportDescription()
{
    cout << "This program calculates paychecks for each employee. "<< endl
         << "A text file will be created outside of the program with the information: "<< endl
         << "name, rate of pay, number of hours worked, and tax percentage to be deducted." << endl
         << endl
         << "The program will create a report in columnar format showing the employee "<< endl
         << "name, hourly rate, number of hours worked, tax rate, gross pay, and net pay. "<< endl
         << endl
         << "After all employees are processed, totals will be displayed total gross amount "<< endl
         << "and total net pay." << endl
         << endl << endl;
}

void reportTitle()
{
    cout << setfill(' ') << right << setw(38) << "Payroll Report" << endl;
}

void inputData(ifstream& inFileData, string& name, double& pay, double& time, double& tax)
{
    getline(inFileData, name, '#');
    inFileData >> pay >> time >> tax;
    inFileData.ignore(100, '\n');
//cout << " " <<  pay << " " << time << " " <<  tax;
}

void processPay(double payrate, double hours, double tax, double& grossPay, double& netPay, double& grossPayTotal, double& netPayTotal)
{
    
    netPay = payrate * hours;
    double temptax = netPay * tax/100.0;        // ** move this!!
    grossPay = netPay - temptax;

    grossPayTotal = grossPayTotal + grossPay;
    netPayTotal = netPayTotal + netPay;

}

void printEmployeeInfo(string name, double pay, double time, double tax, double gross, double net)
{
     static int first=1;
    cout << fixed << showpoint << setprecision(2);  
   if(first)      // ** only print title first time in
     {
      cout << left << setw(20) << "Employee" << setw(10) << "Hourly" << setw(10) << "Hours"  << setw(10) << "Tax" << setw(10) << "Gross" << setw(10) << "Net" << endl;
      cout << left << setw(20) << "Name" << setw(10) << "Rate" << setw(10) << "Worked" << setw(10) << "Rate" << setw(10) << "Amount" << setw(10) << "Amount" << endl;
      cout << endl;
      first=0;
      }
    cout << left << setw(20) << name << setw(10) << setw(10) << pay << setw(10) << time << setw(10) << tax << setw(10) << gross << setw(10) << net << endl;

}

void totalAmounts(double totalGross, double totalNet)
{

    cout << left << setw(5) << "Totals" << right << setw(10) << totalGross << setw(10) << totalNet << endl;

}

Remember when declaring a local variable (inside {}) such as
double grossPayTotal=0; // ** zero these totals
double netPayTotal=0;
its value is by default undetermined; make such you initialise such variables.

This post has been edited by horace: 7 Nov, 2006 - 10:53 PM
User is offlineProfile CardPM
+Quote Post

Reply to this topicStart new topic
Time is now: 12/4/08 07:00PM

Live C++ Help!

C++ Tutorials

Reference Sheets

C++ Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month