Pass by Value Pass by Function

Hi, I started writing a program to calculate tuition for a school--

Upon completing I am realizing I need to rewrite it to make pass by value/pass by functions included

I am still very uneasy on this topic

If someone could steer me in the right direction of how to set up this program by using these parameters that would be a great help


My Code
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
#include <iostream>
using namespace std;

int main()
{
   char calc, parking, sticker, idcard, resident;
   int credits, semester;
   const double fs_parking = 85.00, ws_parking = 45.00,
	   fs_service = 50.50, ws_service = 47.50, 
	   as_sticker = 19.50, id_card = 13.00, 
	   resident_fee = 46.00, nonresident_fee = 295.00;
   double fee;

   do
   {
	 cout << "Enter number of units enrolled:";
     cin >> credits;
     cout << "Is this Fall[0], Winter[1], Spring[2] or Summer[3] session:";
     cin >> semester;
	 cout << "Are you a state resident[0] or not[1]:";
     cin >> resident;

     switch(resident)
     {
        case '0':   fee = credits * resident_fee;
                    break;

        case '1':   fee = credits * nonresident_fee;
                    break;

        default:    cout << "You must enter 0, or 1\n";

      } // end switch 

	 cout << "Want a parking decal? [y/n]:";
     cin >> parking;
	      
	 switch(parking)
     {
        case 'y':
		case 'Y':	
			if (semester = 0, 2)
			{
				fee = fee + fs_parking;
			}
			else if (semester = 1, 3)
			{
				fee = fee + ws_parking;
			}
                    break;

        case 'n':
		case 'N': fee = fee;
                    break;

        default:    cout << "You must enter Y, or N\n";

      } // end switch 

     cout << "Want an AS sticker? [y/n]:";
     cin >> sticker;
	 	 switch(sticker)
     {
        case 'y':
		case 'Y': fee = fee ;

        case 'n':
		case 'N': fee = fee - as_sticker;
                    break;

        default:    cout << "You must enter Y, or N\n";

      } // end switch 
	 cout << "Want an ID card? [y/n]:";
     cin >> idcard;
	 	 switch(idcard)
     {
        case 'y':
		case 'Y': fee = fee;
				break;
        case 'n':
		case 'N': fee = fee - id_card;
                    break;

        default:    cout << "You must enter Y, or N\n";

      } // end switch 
         

	if (semester == 0)
	{
		cout << "For Fall semester, your total fees are $ " << fee + fs_service << endl;
	}
	else if (semester == 1)
	{
		cout << "For Winter semester, your total fees are $ " << fee + ws_service <<  endl;
	}
	else if (semester ==  2)
	{
		cout << "For Spring semester, your total fees are $ " << fee + fs_service <<  endl;
	}
	else if (semester ==  3)
	{
		cout << "For Summer semester, your total fees are $ " << fee + ws_service <<  endl;
	}
	else
	{
		cout << "Error" <<  endl;
	}

	cout << "\nWould you like to do another calculation? Y or N: ";
     cin >> calc;      

   } while( calc=='y' || calc=='Y' ); 

   return 0;
}



The instructions


Create a C++ program which calculates student fees for those attending Santa Monica College. IN ORDER TO RECEIVE FULL CREDIT, YOU MUST CREATE FUNCTIONS TO SOLVE THIS PROBLEM WITH BOTH PASS-BY-VALUE AND PASS-BY-REFERENCE PARAMETER (No, main() doesn't count). Summarized in the chart below is the cost calculations I want your program to perform.

Enrollment Fee

$ 46.00/ unit for California Residents
$ 295.00/ unit for F1/Non-Residents

Student Services Fee
(AS Sticker fee is Optional, saving $19.50)
(ID Card fee is Optional, saving $13)

$ 47.50 Winter/Summer
$ 50.50 Fall/Spring

Parking Decal (Optional)

$ 45.00 Winter/Summer
$ 85.00 Fall/Spring
You have a logic problem with every one of your switch statements.
If the input is invalid, you simply output a message and fall though as if everything was okay.

For example, if resident is not 0 or 1, fee is left uninitialized and will give you garbage results.

My suggestion is that you make each of the inputs a separate function. Those functions loop until a valid value has been entered.

That will meet the requirement to use functions, but you will still need to provide functions that use use pass by value and pass by reference.

Topic archived. No new replies allowed.