Issue with functions not returning values

This is my first program with user defined functions, and it seems that my functions are not returning back a value, so my totalCharges are incorrect. I am not sure what I can do to resolve the issue, so that it adds the values correctly.

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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
 #include <iostream>
#include <fstream>
#include <iomanip>
using namespace std;

double compALocalCharge(double);
double compALongDisCharge(double);
double compBLocalCharge(double&);
double compBLongDisCharge(double&);

int main()
{
    ifstream inputFile;
     
    inputFile.open("E:/CSC_133/lab6data.dat"); //Opens input file

   //Declare Variables 
  double monthlyRateA = 10.00;//The monthly rate for service for company A
  double monthlyRateB = 7.00; //The monthly rate for service for company B
  int phoneNum;
  char code;
  double localMin;
  double longDistMin;
  double totalCharges = 0; //total from local and long distances charges added togather
  double subtotalA; //all total charges added together for company A
  double subtotalB; //all total charges added together for company B
  double grandTotal; //all subtotals added together
  
  cout<<fixed;
  cout<<showpoint;
  cout<<setprecision(2);
  
  cout<<"Telephone Number "<<"Code"<<"\t"<<"Local Charges"<<"\t"<<"Long Distance Charges"<<"\t"<<"Total Charges"<<endl;
  while(inputFile)
  {
                  inputFile>>phoneNum;
                  cout<<phoneNum;
                  inputFile>>code;
                  cout<<"\t"<<"\t"<<"  "<<code;
                  
                  if (code == 'A')
                    {  
                         
                         totalCharges = totalCharges + monthlyRateA;
                         inputFile>>localMin;
                    
                         totalCharges = totalCharges + compALocalCharge(localMin);
                         
                         inputFile>>longDistMin;
                         
                         totalCharges = totalCharges + compALongDisCharge(longDistMin);
                           
                          subtotalA = subtotalA + totalCharges;
                          cout<<"\t"<<"          "<<totalCharges<<endl; 
                                                 
                           totalCharges = 0;                       
                    }
                  else if (code == 'B')
                    {
                      
                      totalCharges = totalCharges + monthlyRateB;
                      inputFile>>localMin;
                          
                      totalCharges = totalCharges + compBLocalCharge(localMin);
                      
                      inputFile>>longDistMin;
                      
                      totalCharges = totalCharges + compBLongDisCharge(longDistMin);
                      
                      subtotalB = subtotalB + totalCharges;
                      cout<<"\t"<<"          "<<totalCharges<<endl;
                      
                      totalCharges = 0;
                    }                                   
                     
  }
  
    cout<<"\n"<<"Subtotal for company A is "<<subtotalA<<endl;
   cout<<"Subtotal for company B is "<<subtotalB<<endl;                
  grandTotal = subtotalA + subtotalB;
  cout<<"The Grand total of both companies is "<<grandTotal<<endl;
  
     system ("pause");
     return 0; 
} // End of main function

double compALocalCharge(double local)
{
        double monthlyCharge;  
    if (local <= 60.0)
       {
                 double monthlyCharge = 0.0;
                 monthlyCharge = monthlyCharge + 0.0;
                 cout<<"     "<<monthlyCharge;
       }
    else if (local > 60.0)
         {
            double monthlyCharge;
            monthlyCharge = (local - 60.0) * 0.25;
            cout<<"     "<<monthlyCharge;
         }
         return monthlyCharge;
}
double compALongDisCharge(double longDistance)
{
       double monthlyCharge;
    if (longDistance <= 100.0)
       {  
          double monthlyCharge;
          monthlyCharge = longDistance * 0.20;
          cout<<"\t"<<"            "<<monthlyCharge;
       }
    else if (longDistance > 100.0)
         {
            double monthlyCharge;
            monthlyCharge = (100.0 * 0.20) + ((longDistance - 100.0)*0.17);
            cout<<"\t"<<"            "<<monthlyCharge;
         }
return monthlyCharge;
}

double compBLocalCharge(double& localMin)
{
       double monthlyCharge;
    if(localMin <= 30.0)
      {
                double monthlyCharge;
                monthlyCharge = monthlyCharge + 0.0;
                cout<<"\t"<<"0.00";
      }
      else if( localMin > 30.0)
        {
               double monthlyCharge;
               monthlyCharge = (localMin - 30.0)*0.20;
               cout<<"\t"<<monthlyCharge;
        } 
    return monthlyCharge;
}

double compBLongDisCharge(double& longDistMin)
{ 
       double monthlyCharge;
    if (longDistMin <= 200.0)
       {
                   double monthlyCharge;
                   monthlyCharge = (longDistMin * 0.25);
                   cout<<"\t"<<"            "<<monthlyCharge;
       }
    else if (longDistMin > 200.0)
         {  
            double monthlyCharge;
            monthlyCharge= (200.0 * 0.25) + ((longDistMin - 200.0)* 0.18);
            cout<<"\t"<<"            "<<monthlyCharge;
            }
 return monthlyCharge;        
}
In the if/else statements in your functions, you are creating another monthlyCharge variable which is shadowing your original one (and the one you return). The easiest way to fix this is just get rid of the declarations from line 92, 98, 109, 115, 127, 133, 145, and 151.
Thanks a ton, that fixed the issue.
Topic archived. No new replies allowed.