Functions and Variables

So I am writing a Payroll Program. What I am confused about is, I am using a function to do a math of an employees payment that he has incurred. I included my code below. The portion I am talking about is first initiated in the main() function of my program. I initialize the variable "payment" and send it to the function "ratecalc" to be worked out and returned. But I run into a problem when referencing the variable again in the program, the variable gets reset to 0.00 some how and I have been unable to figure it out. Any advise is helpful. Thanks -DL




1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
#include <iostream>
#include <iomanip>
#include <fstream>
#include <string>
#include <stdlib.h>
using namespace std;


double socialcalc(double payment){
       cout << payment << "payment before soc" << endl;
       double soc =90;
       double socper = 0.07;
       soc = payment * socper;
      cout << payment << "payment in soc" << endl;
      // cout << payment << "Payment " << endl << socper << "Socper" << endl;
      // cout << soc << "Social" << endl;
       
       return soc;
       }

double inscalc(string inscode){
       double ins;
       int q=0;
      
       if (q<1 && (inscode == "N" || inscode == "n")){
       ins = 0.00;
       q++;
       } 
       
       if (q<1 && (inscode == "S" || inscode == "s")){
       ins = 9.50;
       q++;
       }
     
       if (q<1 && (inscode == "F" || inscode == "f")){
       ins = 27.75;
       q++;
       }
    
       
       return ins;
       
       
       
       }


double ratecalc (double rate, double hours){
       double payment=0, ot=0, addedOT=0;
       if(hours > 40)
         {
         double ot = hours - 40;
         double addedOT = (ot * rate) * 2;      
         double payment = addedOT + (rate * (hours - ot));
        cout << payment << "payment" << endl;
         }
       else 
       {
       double payment = rate * hours;
      cout << payment << "payment if else " << endl;
       }
       
       return payment;
       }
       


int main () {
    string name;
    double rate, hours;
    string inscode;
    
  string line;
  ifstream myfile ("payrollinfo.txt");
  if (myfile.is_open())
  { cout << "Program Started\n\n\n" << endl;

    int i=0;
    while (getline(myfile,line))
    {
      name = line.substr(0,21);          
      rate = atof(line.substr(22,5).c_str());   
      hours = atof(line.substr(30,2).c_str());   
      inscode = line.substr(33,1);
     
      double ins = inscalc(inscode);
      double payment = ratecalc(rate, hours);
      cout << payment << "payment in main" << endl;
      double social = socialcalc(payment);

     
     
//output     
      if(i<1) {
cout << fixed << "Name" 
<< setw(22) << "Rate"
<< setw(9) << "Hours "
<< setw(5) << "INS" 
<< setw(12) << "Insurance" 
<< setw(10)<< "Payment" 
<< setw(11)<< "Social" 
<< endl;
      i++;
      }


cout << left << setw(20) << name 
<< right << setw(5) << setprecision(2) <<  rate 
<< setw(6) <<setprecision(0) << hours 
<< setw(7) << inscode   
<< setw(11) <<setprecision(2) << ins
<< setw(11) << payment
<< setw(11) << social
<< endl; 
      
            
      
    }
    myfile.close();
  }

  else cout << "Unable to open file"; 
  cout << "\n\n\nProgram Finished" << endl;
  cin.get();

return 0;

          
        
}

    


Here is data input for anyone who wants to try it.

1
2
3
4
5
6
7
Tom Anderson           7.52   45 N
Bob Conerley   	      17.50   40 S
John Potter            9.30   35 S
Terrance Appleby      31.00   42 F
Joseph Rinker         17.00   35 F
Todd Russell           5.00   30 S
Bill Ryan             18.25   45 N
In line 87, you initialize payment:
double payment = ratecalc(rate, hours);

However, before that initialization, you initialize it to 0.0 at line 49:
double payment = 0;

Line 49 should read:
double payment = 0.0;
as that is what the compiler is expecting. Also, assume that the computer is dumber than you are. If you can't tell the difference between what values a local variable should actually hold, the computer definitely won't. Making the variable names different, maybe renaming the one inside "ratecalc()" function as 'wages' or 'earnings' or something else along those lines, would solve your problem. See if that helps.
Topic archived. No new replies allowed.