Cell phone bill - passing by reference parameter

1) cannot get bill to output correctly
2) how do I output the description for the plan

Write a program to calculate and print out cellular phone bills for one month.

Fixed rate plan (code F),
per Minute charge plan (code M),
Home customer plan (code H),
home Plus customer plan (code P), or
Business customer plan (code B).

• Code F: A flat $150.00 fee for unlimited minutes (not available to businesses)
• Code M: A charge of $5, plus 21 cents a minute
• Code H: $40 for the first 600 minutes used, plus 19 cents for each additional minute.
• Code P: $64 for the first 1000 minutes used, plus 17 cents for each additional minute
• Code B: $150 for up to 2,000 minutes; $210 for up to 3,000 minutes, and $240 if over 3,000 minutes

No other codes should be accepted. Then prompt for a 5-digit customer account number (no leading zeros allowed) and error check value is valid. Re-prompt until a valid value is entered. This input should be read and error checked from within the main function. Next call display a menu of plan codes, along with descriptions of each, to the user. Prompt for the plan code and error check that the user entered a valid plan code (accepted in upper or lowercase). Re-prompt until a valid value is entered. If necessary (all plans except plan F), also prompt for the number of minutes used and error check that the number entered is not negative. Re-prompt until a valid value is entered.

Calculate the bill total and display the results. The output should display: the customer account number, the plan chosen in words, the number of minutes used (unless the plan chosen was F), and the total amount of bill. Display neatly
with descriptive text. After displaying the results, ask the user whether to execute the program again, and do so, as long as the user wishes to continue.
At a minimum, the program must implement three separate functions (in addition to main), as described on the next page:

• One function to read and validate the plan choice read and validate the number of minutes used.
• One function to calculate the amount of the bill and return the bill amount.
• One function to display the bill output.

The functions must use parameters to pass required data to each function. Remember to pass all input only parameters by value, and pass all output parameters by reference.
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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
#include <iostream>
#include <iomanip>
#include <cctype>

using namespace std;

void getPlan_Minutes(char& plan, int& minutes);
void calculateBill(char& plan, int& minutes);
void outputBill(char& plan, int& minutes, int account);

//constants
double const CODE_F = 150.00;      //code f - monthly charge for fixed rate plan
double const CODE_M = 5.00;        //code m - monthly base charge for per minute charge plan
double const CODE_H = 40.00;       //code h - monthly base charge for home customer plan
double const CODE_P = 64.00;       //code p - monthly base charge for home plus customer plan
double const CODE_B = 150.00;      //code b - monthly base charge for business customer plan


int main()
{
     int account;
     char plan;
     int minutes;
     char getPlan;
     int getMinutes;
     char planChosen;
     int minutesUsed;

     cout << "This program will calculate and show you your cellular phone bill for one month." << endl << endl;

     do {
          cout << "Please input your 5 digit account number: ";
          cin >> account;
          cout << endl;
          if ((account < 10000) || (account > 99999))
               cout << "Your account number is not valid" << endl << endl;
     } while ((account < 10000) || (account > 99999));

     getPlan_Minutes(plan, minutes);
     calculateBill(planChosen, minutesUsed);
     outputBill(planChosen, minutesUsed, account);

     system ("PAUSE");
     return 0;
}

void getPlan_Minutes(char& plan, int& minutes)
{
cout << "AVAILABLE PLANS:" << endl;
cout << "F - Fixed rate plan - $150 flat fee for unlimited minutes (no businesses)" << endl;
cout << "M - Per minute plan - $5 per month plus 21 cents per minute" << endl;
cout << "H - Home customer plan - $40/first 600 minutes used, plus 19 cents each additional minute" << endl;
cout << "P - Home plus customer plan - $64/first 1000 minutes, plus 17 cents each additional minute" << endl;
cout << "B - Business customer plan - $150 up to 2,000 minutes, $210 up to 3,000 minutes, $240/over 3,000 minutes"
        << endl << endl;
cout << "Please choose the letter of your plan from above: ";
cin >> plan;
plan = toupper (plan);
cout << endl << "Your chosen plan is: " << endl;

// Read letter of plan and output description

switch (plan) {               //start switch plan
     case 'F':
          cout << "F - Fixed rate plan - $150 flat fee for unlimited minutes (no businesses)" << endl << endl;
          break;

     case 'M':
          cout << "M - Per minute plan - $5 per month plus 21 cents per minute" << endl << endl;
          cout << "Please input your minutes used: ";
          cin >> minutes;
          for( ;minutes < 0; )
          {
               cout << "Invalid number, please enter a number above 0: ";
               cin >> minutes;
          }
          cout << endl;
          break;

     case 'H':
          cout << "H - Home customer plan - $40/first 600 minutes used, plus 19 cents each additional minute"
                  << endl << endl;
          cout << "Please input your minutes used: ";
          cin >> minutes;
          for( ;minutes < 0; )
          {
               cout << "Invalid number, please enter a number above 0: ";
               cin >> minutes;
          }
          cout << endl;
          break;

     case 'P':
          cout << "P - Home plus customer plan - $64/first 1000 minutes, plus 17 cents each additional minute"
                  << endl << endl;
          cout << "Please input your minutes used: ";
          cin >> minutes;
          for( ;minutes < 0; )
          {
               cout << "Invalid number, please enter a number above 0: ";
               cin >> minutes;
          }
          cout << endl;
          break;

     case 'B':
          cout << "B - Business customer plan - $150 up to 2,000 minutes, $210 up to 3,000 minutes, $240/over 3,000 "
                  "minutes" << endl << endl;
          cout << "Please input your minutes used: ";
          cin >> minutes;
          for( ;minutes < 0; )
          {
               cout << "Invalid number, please enter a number above 0: ";
               cin >> minutes;
          }
          cout << endl;
          break;

     default:
          cout << "Please enter a valid plan letter from above: ";
          cin >> plan;
          break;

     }         //end switch plan
}         //end function


void calculateBill(char& plan, int& minutes)
{
     double billF;
     double billM;
     double billH;
     double billP;
     double billB;
     char planChosen;
     int minutesUsed;

     if (planChosen == 'F')
          billF = CODE_F;
     else if (planChosen == 'M')
          billM = CODE_M + (.21 * minutesUsed);
     else if (planChosen == 'H')
          if (minutesUsed > 600)
               billH = CODE_H + (.19 * minutesUsed);
          else
               billH = CODE_H;
     else if (planChosen == 'P')
          if (minutesUsed > 1000)
               billP = CODE_P + (.17 * minutesUsed);
          else
               billP = CODE_P;
     else if (planChosen == 'B')
          if (minutesUsed <= 2000)
               billB = CODE_B;
          else if (minutesUsed <= 3000)
               billB = CODE_B + 60;
          else if (minutesUsed > 3000)
               billB = CODE_B + 90;
}

void outputBill(char& plan, int& minutes, int account)
{
     double billF;
     double billM;
     double billH;
     double billP;
     double billB;
     double total;
     char planChosen;
     int minutesUsed;

     cout << "YOUR MONTHLY BILL" << endl << endl;
     cout << setw(20);
     cout << "Your account number: " << account << endl;
     cout << "Your plan: " << planChosen << endl;
     if (planChosen != 'F')
     cout << "Your minutes: " << fixed << showpoint << setprecision(2) << setw(20) << minutesUsed << endl;
     cout << "Total due: " << total << endl;

     switch (planChosen) {               //start switch plan

          case 'F':
               total = billF;
               break;

          case 'M':
               total = billM;
               break;

          case 'H':
               total = billH;
               break;

          case 'P':
               total = billP;
               break;

          case 'B':
               total = billB;
               break;
     }

}
First thing I notice is that you are sending variables to your functions but never using them...declaring new ones...and using those without initializations...

27 - minutesUsed is declared in main()
41 - pass minutesUsed into function outputbill
161 - minutesUsed becomes the variable 'minutes'. minutesUsed currently does not exist in function 'outputbill'
170 - declare minutesUsed(?why?this is not the same minutesUsed as line 27)
177 - printing 'minutesUsed' (uninitialized local variable) ( should u print 'minutes' instead?)
Last edited on
I think you are totally confused with parameters when passing it into a function
Ill illustrate with a simple example, which MAY make u clear
1
2
3
4
5
6
7
int main()
{
  int choice=0;
  get_choice(choice);
   cout<<choice;
  return 0;
}

1. Pass by reference(passing parameters with '&' symbol)
1
2
3
4
5
 void get_chioce(int& choice)
{
   cout<<"Enter choice";
   cin>>choice; //lets say user enter 3
}


Output will be: 3

2. Pass by value(passing parameter without '&' symbol)
1
2
3
4
5
void get_choice(int choice)
{
cout<<"Enter choice:'
cin>>choice;
} 

Now output will be: 0


'&choice' gives the name of memory space where choice is located.
this will allow store a new number in that memory space

'choice' gives the value of the choice itself,i.e) zero
as choice value is zero, u cannot change it.
u cannot assign
0=3;

After editing your code, it will be like this
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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195

#include <iostream>
#include <iomanip>
#include <cctype>

using namespace std;

void getPlan_Minutes(char& PLAN, int& MINUTES);
void calculateBill(char PLAN, int MINUTES,double& F,double& M,double& H,double& P,double& B);
void outputBill(char PLAN, int MINUTES,double F,double M,double H,double P,double B, int account);

//constants
double const CODE_F = 150.00;      //code f - monthly charge for fixed rate plan
double const CODE_M = 5.00;        //code m - monthly base charge for per minute charge plan
double const CODE_H = 40.00;       //code h - monthly base charge for home customer plan
double const CODE_P = 64.00;       //code p - monthly base charge for home plus customer plan
double const CODE_B = 150.00;      //code b - monthly base charge for business customer plan


int main()
{
     int account;
     char plan;
     int minutes;
     char getPlan;
     int getMinutes;
	 double billF;
     double billM;
     double billH;
     double billP;
     double billB;
    // char planChosen;
     //int minutesUsed;

     cout << "This program will calculate and show you your cellular phone bill for one month." << endl << endl;

     do {
          cout << "Please input your 5 digit account number: ";
          cin >> account;
          cout << endl;
          if ((account < 10000) || (account > 99999))
               cout << "Your account number is not valid" << endl << endl;
     } while ((account < 10000) || (account > 99999));

     getPlan_Minutes(plan, minutes);
     calculateBill(plan, minutes,billF,billM,billH,billP,billB);
     outputBill(plan, minutes,billF,billM,billH,billP,billB,account);

     system ("PAUSE");
     return 0;
}

void getPlan_Minutes(char& PLAN, int& MINUTES)
{
cout << "AVAILABLE PLANS:" << endl;
cout << "F " << endl;
cout << "M " << endl;
cout << "H" << endl;
cout << "P" << endl;
cout << "Bs"<< endl << endl;
cout << "Please choose the letter of your plan from above: ";
cin >> PLAN;
PLAN = toupper (PLAN);
cout << endl << "Your chosen plan is: " << endl;

// Read letter of plan and output description

switch (PLAN) {               //start switch plan
     case 'F':
          cout << "F - Fixed rate plan - $150 flat fee for unlimited minutes (no businesses)" << endl << endl;
          break;

     case 'M':
          cout << "M - Per minute plan - $5 per month plus 21 cents per minute" << endl << endl;
          cout << "Please input your minutes used: ";
          cin >> MINUTES;
          for( ; MINUTES < 0; )
          {
               cout << "Invalid number, please enter a number above 0: ";
               cin >>  MINUTES;
          }
          cout << endl;
          break;

     case 'H':
          cout << "H - Home customer plan - $40/first 600 minutes used, plus 19 cents each additional minute"
                  << endl << endl;
          cout << "Please input your minutes used: ";
          cin >>  MINUTES;
          for( ; MINUTES < 0; )
          {
               cout << "Invalid number, please enter a number above 0: ";
               cin >>  MINUTES;
          }
          cout << endl;
          break;

     case 'P':
          cout << "P - Home plus customer plan - $64/first 1000 minutes, plus 17 cents each additional minute"
                  << endl << endl;
          cout << "Please input your minutes used: ";
          cin >>  MINUTES;
          for( ; MINUTES < 0; )
          {
               cout << "Invalid number, please enter a number above 0: ";
               cin >>  MINUTES;
          }
          cout << endl;
          break;

     case 'B':
          cout << "B - Business customer plan - $150 up to 2,000 minutes, $210 up to 3,000 minutes, $240/over 3,000 "
                  "minutes" << endl << endl;
          cout << "Please input your minutes used: ";
          cin >> MINUTES;
          for( ; MINUTES < 0; )
          {
               cout << "Invalid number, please enter a number above 0: ";
               cin >>  MINUTES;
          }
          cout << endl;
          break;

     default:
          cout << "Please enter a valid plan letter from above: ";
          cin >> PLAN;
          break;

     }         //end switch plan
}         //end function


void calculateBill(char PLAN, int MINUTES,double& F,double& M,double& H,double& P,double& B)
{
     
     

     if (PLAN == 'F')
          F = CODE_F;
     else if (PLAN == 'M')
          M = CODE_M + (.21 * MINUTES);
     else if (PLAN == 'H')
          if ( MINUTES > 600)
               H = CODE_H + (.19 *  MINUTES);
          else
               H = CODE_H;
     else if (PLAN == 'P')
          if ( MINUTES > 1000)
               P = CODE_P + (.17 *  MINUTES);
          else
               P = CODE_P;
     else if (PLAN == 'B')
          if ( MINUTES <= 2000)
               B = CODE_B;
          else if ( MINUTES <= 3000)
               B = CODE_B + 60;
          else if ( MINUTES > 3000)
               B = CODE_B + 90;
}

void outputBill(char PLAN, int MINUTES,double F,double M,double H,double P,double B, int account)
{
     double total;
     cout << "YOUR MONTHLY BILL" << endl << endl;
     cout << setw(20);
     cout << "Your account number: " << account << endl;
     cout << "Your plan: " << PLAN << endl;
     if (PLAN != 'F')
     cout << "Your minutes: " << fixed << showpoint << setprecision(2) << setw(20) <<  MINUTES << endl;
     cout << "Total due: " ;

     switch (PLAN) {               //start switch plan

          case 'F':
               total = F;
               break;

          case 'M':
               total = M;
               break;

          case 'H':
               total = H;
               break;

          case 'P':
               total = P;
               break;

          case 'B':
               total = B;
               break;
     }
	 cout<<total<<endl;
}
Thanks so much for the help!
How do I get the plan description to output on the bill output instead of just the letter?

For example:

YOUR MONTHLY BILL
Your account number: 12345
Your plan: F - Fixed rate plan - $150 flat fee for unlimited minutes (no businesses)
Your minutes: 300
Total due: $150

Slowly but surely I will figure this all out. I HOPE!
U can do what u did in the getPlan_Minutes function where you use a switch statement to determine the output.
But, since you're using the 'plan' multiple times it would probably be better to just store into a string instead of repeatedly typing it all out with cout each time.

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
string s_plan;

//then get user input
cout << "choose your plan: ";
char c_plan;
cin >> c_plan;
//convert plan to an integer
int i_plan;
switch(c_plan)
{
   case 'f':    //fixed
   {
        s_plan = "F - Fixed rate plan - $150 flat fee for unlimited minutes (no businesses)";
   }
   case 'h':    //home
   {
        s_plan = "H - Home customer plan - $40/first 600 minutes used, plus 19 cents each additional minute";
   }
//etc
}
//then it's just 1 time to do this in 'getPlan_Minutes':
cout << s_plan;
          cout << "Please input your minutes used: ";
          cin >>  MINUTES;
//...
//then once u get to 'outputBill' on line 167
 cout << "Your plan: " << s_plan << endl;


If it gets any more complicated...like needing to have multiple messages of the same plan, u'll be better off using structures or some sort of array of strings to hold each one.
Topic archived. No new replies allowed.