Having Trouble With This Assignment

I am not good with functions. I just can't really understand them overall. But I finally got to the point where my program doesn't output any errors. However I am now getting crazy numbers in the output like: 2.07379e-317. If it helps this is the assignment. And no I am not just asking for the answer. I just need help with what I am doing wrong. Sorry if this post frustrates you. I just needed to take this class for a credit and I am terrible at this kind of stuff.

Edit: Also just realized that the program won't even display the amount of days the user input. It just says 0.

Write a C++ program that will calculate the charges for a patient’s stay in a hospital. The program should be done using functions. Global variables are not allowed except for constants. Information should be passed with parameters.

The program will print a set of instructions for the user and give a brief explanation of the purpose. It will ask the user for number of days spent in the hospital, the medication charges, the surgical charges and the lab fees. It will display the length of stay, the room charges, the total miscellaneous charges (medication/surgical/lab) and the total charge. The room charges are $500 per day.

You should write a user-defined function to perform each of the following tasks:

• display the instructions
• prompt and read the number of days
• prompt and read the medication charges
• prompt and read the surgical charges
• prompt and read the lab fees
• calculate the room charge
• calculate the miscellaneous charge
• calculate the total charge
• print the results in a neat and well labeled form


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
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
  #include <iostream>     // class for stream input/output
#include <iomanip>		// class for manipulating input/output
#include <string>		// class for string 

using namespace std;    // the standard namespace



//these are the prototypes for the functions
void instructions(); 
int days_prompt();
double med_prompt();
double surgical_prompt();
double lab_prompt();
double calc_total_room(double days);
double calc_total_misc(double medication, double surgical, double lab);
double calc_total(double misc_total, double room_total);
void results(int days, double room_total, double misc_total, double total);


//constant is only needed for room charge because all the other numbers are the inputs from the user
const double ROOM_CHARGE = 500;


int main()

{



double  final_room, 
		final_medication,
		final_surgical,
		final_misc,
		final_lab,
		final_total;
int 	final_days;
		

  instructions();
 
  days_prompt();
  
  med_prompt();
  
  surgical_prompt();
  
  lab_prompt();
  
  calc_total_room(final_room);
  
  calc_total_misc(final_medication, final_surgical, final_lab);
  
  calc_total(final_room, final_misc);
  

  
results(final_days, final_room, final_misc, final_total);
  
  

  return 0;

}

/***********************************************************************/
/*                                                                     */
/*   Function name:  instructions                                      */
/*   Description:  Display the instructions of hospital program.       */
/*   Parameters:   N/A                                                 */
/*                                                                     */
/*                                                                     */
/*   Return Value: N/A                                                 */
/*                                                                     */
/***********************************************************************/

void instructions()

{
	
	cout << "\n \n";
	cout << "This program will calculate the final costs of your hospital bill\nalong with miscellaneous charges.";
	
}
/***********************************************************************/
/*                                                                     */
/*   Function name:  days_prompt                                       */
/*   Description:  Prompts the user to enter the amount of days        */
/*                 they stayed at the hospital.                        */
/*   Parameters:   N/A                                                 */
/*                                                                     */
/*                                                                     */
/*   Return Value: int - days                                          */
/*                                                                     */
/***********************************************************************/
int days_prompt()
{
	
	int days; //input
	//prompt the user for the amount of days he or she stayed at the hospital
	
	cout << "\n\n\n\n\nPlease enter the amount of days you stayed at the hospital. ";
	
	cin >> days;
	
	return days;
	
	
}
/***********************************************************************/
/*                                                                     */
/*   Function name:  med_prompt                                        */
/*   Description:  Prompts the user to enter the cost of medication.   */
/*   Parameters:   N/A                                                 */
/*                                                                     */
/*                                                                     */
/*   Return Value: double - medication                                 */
/*                                                                     */
/***********************************************************************/
double med_prompt()
{
	
	double medication; //input
	//prompt the user for the cost of medication
	
	cout << "\n\nPlease enter the costs of medication.";
	
	cin >> medication;
	
	return medication;
	
}
/***********************************************************************/
/*                                                                     */
/*   Function name:  surgical_prompt                                   */
/*   Description:  Prompts the user to enter the surgical costs.       */
/*   Parameters:   N/A                                                 */
/*                                                                     */
/*                                                                     */
/*   Return Value: double - surgical                                   */
/*                                                                     */
/***********************************************************************/
double surgical_prompt()
{
	
	double surgical; //input
	//prompts the user for the surgical costs
	
	cout << "\n\nPlease enter the surgical costs.";
	
	cin >> surgical;
	
	return surgical;
	
}
/***********************************************************************/
/*                                                                     */
/*   Function name:  lab_prompt                                        */
/*   Description:  Prompts the user to enter the lab costs.            */
/*   Parameters:   N/A                                                 */
/*                                                                     */
/*                                                                     */
/*   Return Value: double - lab                                        */
/*                                                                     */
/***********************************************************************/
double lab_prompt()

{
	
	double lab; //input
	//prompts the user for the lab costs
	
	cout << "\n\nPlease enter the lab costs.";
	
	cin >> lab;
	
	return lab;
	
}

double calc_total_room(double days)
{
	
	double room_total;
	
	room_total = ROOM_CHARGE * days;
	
	return room_total;
}
double calc_total_misc(double medication, double surgical, double lab)
{
	
	
	double misc_total;
	
	misc_total = medication + surgical + lab;
	
	return misc_total;
	
	
}
double calc_total(double misc_total, double room_total)
{
	
	
	double total;
	
	total = misc_total + room_total;
	
	return total;
	
	
}
void results(int days, double room_total, double misc_total, double total)
{
	
	cout << "These are the total costs of your bill.\n\n\n";
	cout << "Number of days stayed: " << days << "\n";
	cout << "Room charges: " << room_total << "\n";
	cout << "Miscellaneous charges: " << misc_total << "\n";
	cout << "Total charges: " << total << "\n";
}
Last edited on
functions have a type and return for a reason. or they have void type and do not return things.
int foo(); // <--- returns an integer.

you can use this result:
cout << foo() << endl; //writes out the integer returned by foo.
int x = foo(); //assigns x the result you computed up in foo.

or you can throw it away and ignore it:
foo(); //the integer has nowhere to go, and is 'lost' forever.

your main() is repeatedly throwing away all the results from your functions.
also, you do not initialize values to most variables, which means they have junk like 3.72e-200 or things like that. Set them to something: zero is a good default for many problems, but 123.456 is a good value that you can spot when debugging and say "hey, wait a sec, that should not be my default value that I put it, it should have been changed and set to something else".

so, you seem like you need this:

int days = days_prompt();
double med = med_prompt();
and so on. and then you need to USE these values somehow to DO something.

also:
calc_total_room(final_room); //what is final_room's value? its 11.31X10^132 for all you know. its junk. you need to call the function with a value that you know what is, computed or gotten from the user (as above) or even hard-coded. But its not right like this.
Last edited on
Registered users can post here. Sign in or register to post.