Programing with functions

Pages: 12
Help please!

my assignment is:
Write a program that calculates and prints the bill for a cellular telephone company. The company offers two types of service: regular and premium. Its rates vary, depending on the type of service. The rates are computed as follows:

Regular: $10.00 plus first 50 minutes are free. Charges for over 50 minutes are $0.20 per minute.
Premium: $25.00 plus for calls made from 6 am to 6 pm, the first 75 minutes are free; charges for more than 75 minutes are $0.10 per minute. For calls made from 6pm to 6am, the first 100 minutes are free; charges for more than 100 minutes are $0.05 per minute.

Your program should prompt the user to enter a service type (type char), and the number of minutes the service was used. A service code of r or R means regular service; a service code of p or P means premium service. Treat any other character as an error. Your program should output the type of service, number of minutes the telephone service was used, and the amount due from the user.

For the premium service, the customer may be using the service during the say and the night. Therefore, to calculate the bill, you must ask the user to input the number of minutes the service was used during the day and the number of minutes the service was used during the night.

All input is to be read via cin.
• All output is written via cout.
• Format the output stream to show the decimal point and two places after the decimal because
of monetary values.
• NOTE: Constants declared in the global section of the source code can be seen in all functions.
They DO NOT have to be passed as parameters. They can be simply used in the function.

so far I have 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
#include <iostream>
#include <iomanip>
#include <string>

using namespace std;

const double REG_SERV_CHARGES = 10.00;
const int REG_FREE_MINUTES = 50;
const double REG_RATE_OVER_50 = 0.20;

const double PREM_SERV_CHARGES = 25.00;
const int PREM_FREE_DAY_MINUTES = 75;
const double PREM_DAY_RATE_OVER_75 = 0.10;

const int PREM_FREE_NIGHT_MINUTES = 100;
const double PREM_NIGHT_RATE_OVER_100 = 0.05;

int  getMinutes (string promptMessage);
void regularAmtDue (int minUsed, double &amtDue);
void premiumAmtDue (int dayMin, int nightMin, double &amtDue);
void writeOutput (int minutes1, int minutes2, double amtDue, char service);

int main()
{

    char serviceType;

    int minutesUsed;
    int minutesUsedPN;

    double amountDue;

    cout << fixed << showpoint;
    cout << setprecision(2);



    cout << "Enter service type: (r or R) for regular, " 
         << "(p or P) for premium service: " << endl;
    cin >> serviceType;
    cout << endl;

    switch (serviceType)
    {
	case 'r':
    case 'R': 
    
    		// call getMinutes for minutes of service here

    	    // call regularAmtDue here
            
			// call writeOutput here
			
            break;
    case 'p':
    case 'P': 

    		// call getMinutes for day minutes here

    		// call getMinutes for night minutes here

			// call premiumAmtDue here

			// call writeOutput here
			
        break;
    default: 
        cout << "Invalid Service Type" << endl;
	}//end switch
    system("PAUSE");
	return 0;
} // end main


int getMinutes (char promptMessage)
{
    
     
//-------------------------------------------------------------------
// getMinutes is a value returning function that accepts a string and
//  prompts the user for the number of minutes then returns this value
//  to main.
//-------------------------------------------------------------------

} // end getMinutes


void regularAmtDue (int minUsed, double &amtDue) 
{
    if (minUsed <= 50)
     {
     amtDue = REG_SERV_CHARGES;
     }
     else if (minUsed > 50)
     {
     amtDue = REG_SERV_CHARGES + minUsed * REG_RATE_OVER_50;
     }

} // end regularAmtDue


void premiumAmtDue (int dayMin, int nightMin, double &amtDue)
{
if (dayMin <= 75)
     {
     amtDue = PREM_SERV_CHARGES;
     }
     else if (nightMin > 100)
     {
     amtDue = PREM_SERV_CHARGES + (nightMin * PREM_NIGHT_RATE_OVER_100);
     }
     else if(nightMin > 100 && dayMin > 75)
     {
     amtDue = PREM_SERV_CHARGES + (dayMin * PREM_DAY_RATE_OVER_75)
                   + (nightMin * PREM_NIGHT_RATE_OVER_100);
     }
     else
     {
     amtDue = PREM_SERV_CHARGES + (dayMin * PREM_DAY_RATE_OVER_75);
     }

} // end premiumAmtDue


void writeOutput (int minutes1, int minutes2, double amtDue, char service)
{
     if (service == 'r' || 'R')
     {
     minutes2 = 0;
     amtDue = 0;
     cout << "Your service is regular." << endl;
     cout << "Your bill is " << regularAmtDue << endl;
     } 
     if (service == 'p' || 'P')
     {
     amtDue = 0;
     cout << "Your service is premium." << endl;
     cout << "Your bill is " << premiumAmtDue << endl;
     }

} // end writeOutput 


I am a bit confused at to what to write in the getMinutes function but the comment says what it should do, i guess its just going completely over my head.
Also, this is the rubric my professor gave me to follow with how the functions should work.

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
//-------------------------------------------------------------------
// getMinutes is a value returning function that accepts a string and
//  prompts the user for the number of minutes then returns this value
//  to main.
//-------------------------------------------------------------------
int getMinutes (string promptMessage){

} // end getMinutes

//-----------------------------------------------------------------------
// regularAmtDue is a void function
//  minUsed - passed by value, number of minutes used in regular service
//  amtDue -  passed by reference, calculated amount due for regular
//           service type.
//-----------------------------------------------------------------------
void regularAmtDue (int minUsed, double &amtDue) {

} // end regularAmtDue

//------------------------------------------------------------------------------------
// premiumAmtDue is a void function
//  dayMin   - passed by value, number of minutes used during day for premium service
//  nightMin - passed by value, number of minutes used during night for premium service
//  amtDue   -  passed by reference, calculated amount due for premium service type.
//-------------------------------------------------------------------------------------
void premiumAmtDue (int dayMin, int nightMin, double &amtDue){

} // end premiumAmtDue

//------------------------------------------------------------------------------------
// writeOutput is a void function
//  All parameters passed by value.
//  This function determines which service type is used and based on that decision 
//  writes the appropriate set of output statements.
//  When calling this function any int parameter not used can be passed 0 in that
//  position.
//-------------------------------------------------------------------------------------
void writeOutput (int minutes1, int minutes2, double amtDue, char service){

} // end writeOutput 


if possible can someone please point me in the right direction. thanks.
That's a serious wall of text. Could you please be more specific in your questions?
My main question is I guess with writing a function that does this specifically:
1
2
3
4
5
6
7
8
9
10
11
int getMinutes (string promptMessage)
{
    
     
//-------------------------------------------------------------------
// getMinutes is a value returning function that accepts a string and
//  prompts the user for the number of minutes then returns this value
//  to main.
//-------------------------------------------------------------------

} // end getMinutes 


and I think im missing something huge because based on another function when compiled the error pops up with int to string malfunction.
okay so i guess my real question is with what to put into a designated function, given the rubric my professor gave me ^^^. Everyone is saying functions are amazing and im here thinking this is worse than writing out a long program.
Could you post the code you tried to use and the error message?
I think those instructions want you to display promptMessage and then input an int, then return it. You shouldn't be converting between integers and strings.
Thats the code I put in, and im not even sure how this function is suppose to work.
1
2
3
4
5
6
7
8
9
10
11
12
int getMinutes (string promptMessage)
{

minutes2 = 0; 
cout << "Enter number of minutes used ";
cin >> promptMessage;


cout << "Enter number of night time minutes used: " << endl;
cin >> promptMessage;

} // end getMinutes 


and the message was:

In function main 'int getMinutes[std::string]'

1. You need to print out promptMessage, not ask for the user to enter the prompt themselves.
2. You need to input to minutes2 (why 2?)
3. That's the location of the error message, not the error message itself.
so all im doing is outputting the prompt but i also thought i needed to get the minutes from them, thats what i thought this meant.

1
2
3
4
5
//-------------------------------------------------------------------
// getMinutes is a value returning function that accepts a string and
//  prompts the user for the number of minutes then returns this value
//  to main.
//------------------------------------------------------------------- 


so does that mean i need to add more datatypes in order to get the minutes which would be an int?
No. Your function is 'given' a string called "promptMessage", so it must be the message for the prompt. You need to cout THIS, and do NOT type your own message within this function.

The only variable you need to make in your function would be an int called "minutes", or whatever name you want. You need to cin to this variable, then return it from the function.
1
2
3
4
5
6
7
8
9
10
int getMinutes (string promptMessage)
{
int minutes;

cout << promptMessage << endl;
cin >> minutes;

return minutes;

} // end getMinutes 


something like this?

and my second concern is because im dealing with someone either having a regular account which means I only need to ask for minutes or someone who may have a premium account in which I need to ask for minutes as well as nightMinutes or some variable, how would i differentiate?
You programmed the function right. Remember, all it does it output the prompt and return the minutes - your code to detirmine what kind of user should be in main.
okay ,thank you. So now that my functions are in good shape.

i need to call them back to main right? so using the variables that are already here would for example say minutesUsed = getMinutes (string promptMessage) as a function call?

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
int main()
{

    char serviceType;

    int minutesUsed;
    int minutesUsedPN;

    double amountDue;

    cout << fixed << showpoint;
    cout << setprecision(2);



    cout << "Enter service type: (r or R) for regular, " 
         << "(p or P) for premium service: " << endl;
    cin >> serviceType;
    cout << endl;

    switch (serviceType)
    {
	case 'r':
    case 'R': 
    
    		// call getMinutes for minutes of service here

    	    // call regularAmtDue here
            
			// call writeOutput here
			
            break;
    case 'p':
    case 'P': 

    		// call getMinutes for day minutes here

    		// call getMinutes for night minutes here

			// call premiumAmtDue here

			// call writeOutput here
			
        break;
    default: 
        cout << "Invalid Service Type" << endl;
	}//end switch
    system("PAUSE");
	return 0;
} // end main 
for example say minutesUsed = getMinutes (string promptMessage) as a function call?
Close, you would do:
minutesUsed = getMinutes("Please enter the number of minutes: ");
Remember, you are 'giving' the message to the function to use as the prompt.
Last edited on
ohhhhh its legit putting in a prompt statement. okay I see.

okay, say im using regAmnt as the call for the regularAmtDue function. I can just bring up all the parameters right? so it's like regAmnt = (minUsed, &amtDue);
or do I have to substitute minsUsed and amtDue with the variables in main. Right because whats designated in the function cant be used in main, or because I have the functions in the universal declaration space can I keep the variables that i had in the function and use them in main?
The variables in any given function do not exist in any other functions. This is called "scope", which you may have heard of. This prevents you from having issues with variables with the same names in different functions - it's perfectly ok thanks to scoping rules.

If you want to get a value out of a function, you either have to return it or assign it via reference.

I'm not sure I understand the rest of your post/question?
yes i remember reading about scoping but i thought that since my functions are in the global declaration space they can be used in both main and that function itself.
The functions themselves can be used in any other functions, but not the variables that are their parameters or local variables - only the function itself can access those.
okay gotcha, so the parameters that i have in my functions have to be replaced with new variables that are designated in my main function.
By "replace" I hope you mean "pass" or 'give', otherwise I think you misunderstood...
Pages: 12