Resetting variable used within switch, inside while loop and referenced by function

The user input for "minutes used" will generate a random number rather than the actual user input when outputted by the void printBill() function. I have tried setting int Minutes = 0 in both main() and inside the while loop - not simultaneously - to no effect. The printBill function is supposed to be generic for either input: regular or premium. I greatly appreciate any help you can offer.

#include <iostream>
#include <stdlib.h>
#include <iomanip>

using namespace std;

//Prototype functions
int calcRegBill(int Minutes);
int calcPremBill(int DayMinutes, int NightMinutes);
void printBill(string PlanType, string AccountNum, char& answer, int AmountDue, int TotalMinutes);

const double RegServPlan = 10.00;
const double PremServPlan = 25.00;

int main()
{
string AccountNum, PlanType;
char ServCode, answer;
int DayMinutes, NightMinutes, TotalMinutes, Minutes;
//int Minutes = 0; This does not reset the variable to 0.
double AmountDue;

while (answer != 'n'){
//int Minutes = 0; Placement of variable #2. This also does not reset the variable to 0.
cout << "Enter the account number: ";
cin >> AccountNum;
cout << endl;
cout << "Enter service code: "
<< "R or r (Regular), "
<< "P or p (Premium): ";
cin >> ServCode;
cout << endl;

switch (ServCode){
case 'r':
case 'R':
PlanType = "Regular";
cout << "Enter minutes used: ";
cin >> Minutes;
cout << endl;
AmountDue = calcRegBill(Minutes);
printBill(PlanType, AccountNum, answer, AmountDue, TotalMinutes);
break;

case 'p':
case 'P':
PlanType = "Premium";
cout << "Enter daytime (6:00am to 6:00pm) minutes used: ";
cin >> DayMinutes;
cout << "Enter nighttime (6:00pm - 6:00am) minutes used: ";
cin >> NightMinutes;
cout << endl;
AmountDue = calcPremBill(DayMinutes, NightMinutes);
printBill(PlanType, AccountNum, answer, AmountDue, TotalMinutes);
break;
default:
cout << "Invalid plan type." << endl;
}
}
cin.get();

}
int calcRegBill(int Minutes)
{
double RegBill;
int TotalMinutes = 0;

if (Minutes > 50)
RegBill = RegServPlan + ((Minutes - 50) * .20);
else RegBill = RegServPlan;
TotalMinutes = Minutes;

return RegBill;
}
int calcPremBill(int DayMinutes,int NightMinutes)
{
double PremBill, AmountDueD, AmountDueN;
int TotalMinutes;
if (DayMinutes > 75)
AmountDueD = ((DayMinutes - 75) * .10);
else
AmountDueD = 0;
if (NightMinutes > 100)
AmountDueN = ((NightMinutes - 100) * .05);
else
PremBill = 0;
PremBill = PremServPlan + AmountDueD + AmountDueN;
TotalMinutes = DayMinutes + NightMinutes;
return PremBill;
}
void printBill(string PlanType, string AccountNum, char& answer, int AmountDue, int TotalMinutes)
{
cout << fixed << showpoint;
cout << setprecision(2);
cout << "Billing Summary (compiled by J Amato)" << endl;
cout << setw(40) << left << "Cellular Account #: " << AccountNum << endl;
cout << setw(40) << left << "Type of Service: " << PlanType << endl;
cout << setw(40) << left << "Total Minutes: " << TotalMinutes << endl;
cout << setw(40) << left << "Amount Due: " << "$" << AmountDue << endl;
cout << endl;
cout << "View another bill summary? (y/n): ";
cin >> answer;
cout << endl;
}
You are calling printBill() with the variable TotalMinutes, not Minutes.
That is correct. I need to call printBill() with both calcRegBill and calcPremBill. So: (calcRegBill)
TotalMinutes = Minutes;
and (calcPremBill)
TotalMinutes = DayMinutes + NightMinutes;
In this way I was hoping to make printBill() a generic function that will print either case as necessary.
and the main function won't have a value for TotalMinutes. The bill calculation functions declare a separate local variable TotalMinutes and assign it a value, but it doesn't get back to main.

(The <> button on the right side of the post will format your code to make it easier to read :) )

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
#include <iostream>
#include <stdlib.h>
#include <iomanip>

using namespace std;

//Prototype functions
int calcRegBill(int Minutes);
int calcPremBill(int DayMinutes, int NightMinutes);
void printBill(string PlanType, string AccountNum, char& answer, int AmountDue, int TotalMinutes);

const double RegServPlan = 10.00;
const double PremServPlan = 25.00;

int main()
{
	string AccountNum, PlanType;
	char ServCode, answer;
	int DayMinutes, NightMinutes, TotalMinutes, Minutes;
	//int Minutes = 0; This does not reset the variable to 0.
	double AmountDue;
	
	while (answer != 'n')
	{
	//int Minutes = 0; Placement of variable #2. This also does not reset the variable to 0.
		cout << "Enter the account number: ";
		cin >> AccountNum;
		cout << endl;
		cout << "Enter service code: " << "R or r (Regular), " << "P or p (Premium): ";
		cin >> ServCode;
		cout << endl;
		
		switch (ServCode)
		{
			case 'r':
			case 'R':
						PlanType = "Regular";
						cout << "Enter minutes used: ";
						cin >> Minutes;
						cout << endl;
						AmountDue = calcRegBill(Minutes);
						printBill(PlanType, AccountNum, answer, AmountDue, TotalMinutes);
						break;
						
			case 'p':
			case 'P':
						PlanType = "Premium";
						cout << "Enter daytime (6:00am to 6:00pm) minutes used: ";
						cin >> DayMinutes;
						cout << "Enter nighttime (6:00pm - 6:00am) minutes used: ";
						cin >> NightMinutes;
						cout << endl;
						AmountDue = calcPremBill(DayMinutes, NightMinutes);
						printBill(PlanType, AccountNum, answer, AmountDue, TotalMinutes);
						break;
			default:
						cout << "Invalid plan type." << endl;
		}
	}
	cin.get();

}
int calcRegBill(int Minutes)
{
	double RegBill;
	int TotalMinutes = 0;
	
	if (Minutes > 50)
		RegBill = RegServPlan + ((Minutes - 50) * .20);
	else 
		RegBill = RegServPlan;
	
	TotalMinutes = Minutes;
	
	return RegBill;
}
int calcPremBill(int DayMinutes,int NightMinutes)
{
	double PremBill, AmountDueD, AmountDueN;
	int TotalMinutes;
	if (DayMinutes > 75)
		AmountDueD = ((DayMinutes - 75) * .10);
	else
		AmountDueD = 0;
	if (NightMinutes > 100)
		AmountDueN = ((NightMinutes - 100) * .05);
	else
		PremBill = 0;
	
	PremBill = PremServPlan + AmountDueD + AmountDueN;
	TotalMinutes = DayMinutes + NightMinutes;
	return PremBill;
}
void printBill(string PlanType, string AccountNum, char& answer, int AmountDue, int TotalMinutes)
{
	cout << fixed << showpoint;
	cout << setprecision(2);
	cout << "Billing Summary (compiled by J Amato)" << endl;
	cout << setw(40) << left << "Cellular Account #: " << AccountNum << endl;
	cout << setw(40) << left << "Type of Service: " << PlanType << endl;
	cout << setw(40) << left << "Total Minutes: " << TotalMinutes << endl;
	cout << setw(40) << left << "Amount Due: " << "$" << AmountDue << endl;
	cout << endl;
	cout << "View another bill summary? (y/n): ";
	cin >> answer;
	cout << endl;
}
Last edited on
@wildblue
(The <> button on the right side of the post will format your code to make it easier to read :) ) Thanks!
If I understand you correctly, I tried setting TotalMinutes = 0 in main(), which behaves like a const., where "Total Minutes: " always equals 0.....?
TotalMinutes = 0;
This just assigns a value to TotalMinutes. It's not a constant.

cout << setw(40) << left << "Total Minutes: " << TotalMinutes << endl;

TotalMinutes will print whatever value is stored in the variable.

Last edited on
I found the issue was actually with poor references. After I pointed TotalMinutes in printBill() to TotalMinutes in calcRegBill() and calcPremBill(), the variable worked fine. Thank you for all your input!! See below:
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
#include <iostream>
#include <stdlib.h>
#include <iomanip>

using namespace std;

//Prototype functions
int calcRegBill(int Minutes, int& TotalMinutes);
int calcPremBill(int DayMinutes, int NightMinutes, int& TotalMinutes);
void printBill(string PlanType, string AccountNum, char& answer, double AmountDue, int TotalMinutes);

const double RegServPlan = 10.00;
const double PremServPlan = 25.00;

int main()
{
string AccountNum, PlanType;
char ServCode, answer;
int DayMinutes, NightMinutes, Minutes, TotalMinutes;
double AmountDue;

   while (answer != 'n'){
        cout << "Enter the account number: ";
        cin >> AccountNum;
        cout << endl;
        cout << "Enter service code: "
        << "R or r (Regular), "
        << "P or p (Premium): ";
        cin >> ServCode;
        cout << endl;

        switch (ServCode){
        case 'r':
        case 'R':
            PlanType = "Regular";
            cout << "Enter minutes used: ";
            cin >> Minutes;
            cout << endl;
            AmountDue = calcRegBill(Minutes, TotalMinutes);
            printBill(PlanType, AccountNum, answer, AmountDue, TotalMinutes);
            break;

        case 'p':
        case 'P':
            PlanType = "Premium";
            cout << "Enter daytime (6:00am to 6:00pm) minutes used: ";
            cin >> DayMinutes;
            cout << "Enter nighttime (6:00pm - 6:00am) minutes used: ";
            cin >> NightMinutes;
            cout << endl;
            AmountDue = calcPremBill(DayMinutes, NightMinutes, TotalMinutes);
            printBill(PlanType, AccountNum, answer, AmountDue, TotalMinutes);
            break;
        default:
            cout << "Invalid plan type." << endl;
        }
}
      cin.get();
}
int calcRegBill(int Minutes, int& TotalMinutes)
{
    double RegBill;
    cout << showpoint << setprecision(2)<< fixed;
    TotalMinutes = Minutes;
    if (TotalMinutes > 50)
        RegBill = RegServPlan + ((TotalMinutes - 50) * .20);
    else RegBill = RegServPlan;
    return RegBill;
}
int calcPremBill(int DayMinutes,int NightMinutes, int& TotalMinutes)
{
    double PremBill, AmountDueD, AmountDueN;
    cout << showpoint << setprecision(2)<< fixed;
    if (DayMinutes > 75)
        AmountDueD = ((DayMinutes - 75) * .10);
    else
        AmountDueD = 0;
    if (NightMinutes >= 100)
        AmountDueN = ((NightMinutes - 100) * .05);
    else
        PremBill = 0;
    PremBill = PremServPlan + AmountDueD + AmountDueN;
    TotalMinutes = DayMinutes + NightMinutes;
    return PremBill;
}
void printBill(string PlanType, string AccountNum, char& answer, double AmountDue, int TotalMinutes)
{
    cout << showpoint << setprecision(2)<< fixed;
    cout << setw(40) << left << "Cellular Account #: " << AccountNum << endl;
    cout << setw(40) << left << "Type of Service: " << PlanType << endl;
    cout << setw(40) << left << "Total Minutes: " << TotalMinutes << endl;
    cout << setw(40) << left << "Amount Due: " << "$" << AmountDue << endl;
    cout << endl;
    cout << "View another bill summary? (y/n): ";
    cin >> answer;
    cout << endl;
}
Topic archived. No new replies allowed.