Help with functions and errors!!! PLEASE

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
/*
PROGRAM: HOSPITAL.CPP
Written by 
This program uses functions and calculates the total cost of a hospital bill after the user inputs their hospital information.
Assignment number 7
*/ 

#include <iostream>
#include <string>
using namespace std;

// Function prototypes
int getPositiveInt( string );
double getMoney ( string );


double calculateTotal(int, double, double, double) ;// Inpatient
double calculateTotal(double, double);// Outpatient



int main()
{
int menuchoice;
int days; // number of days 
double roomRate, // Daily room rate
medication, // Total medication charges, 
services, // Total for tests and other services, 
total;

//Display munuchoice


    // Input and validate patient type
    cout << "This program will compute patient hospital charges.\n";
   

		cout << "You need a the doctor yo. What kind of patient are you?" << endl;

		cout << " 1) Inpatient. " << endl;
		cout << " 2) Outpatient. " << endl;
		cout << " 3) Leaving. " << endl;

	if ( menuchoice == 1)
	{
		days = getPositiveInt( "Enter how long you stay in the hospital:\n  " );
		roomRate = getMoney( "Enter your room charge:\n  " );
		medication = getMoney( "Enter the cost of your medication:\n  " );
		services = getMoney( "Enter the cost of the hospital services:  " );

		    total = calculateTotal( days, roomRate, medication, services );
	}
	else if (menuchoice == 2 )
	{

		medication = getMoney( "Enter the cost of your medication:\n  " );
		services = getMoney( "Enter the cost of the hospital services:  " );

		total = calculateTotal( medication, services );
	}

	else  (menuchoice == 3);
	{
		cout << " Thank you for leaving the Hospital." << endl;
	}

	system( "pause" );
	return 0;
} // main()

   
int getChoice();
{
	int input;
	cin >> input;
}

int getPositiveInt( string prompt )
{
	int input;

	do
	{
		cout << prompt;

		// While you try to read an integer and FAIL
		while (  ! ( cin >> input ) )
		{
			// Clear the error condition of failing to read an int
			cin.clear();
			// Ignore 1 character, up to a \n
			cin.ignore( 1, '\n' );
		} // while
	} while ( input <= 0 );

	return input;
} // getPositiveInt()

double getMoney( string prompt )
{
	int input;

	do
	{
		cout << prompt;

		// While you try to read an integer and FAIL
		while (  ! ( cin >> input ) )
		{
			// Clear the error condition of failing to read an int
			cin.clear();
			// Ignore 1 character, up to a \n
			cin.ignore( 1, '\n' );
		} // while
	} while ( input < 0 );

	return input;
} // getPositiveInt() 
Last edited on
1
2
3
4
5
int getChoice(); // <- this semi-colon should not be here
{
    int input;
    cin >> input;
}
Thank you for your reply. However, I get these errors now

"1>c:\documents and settings\jessica\my documents\visual studio 2008\projects\hospitall\crazzyyy.cpp(44) : warning C4700: uninitialized local variable 'menuchoice' used
1>c:\documents and settings\jessica\my documents\visual studio 2008\projects\hospitall\crazzyyy.cpp(76) : error C4716: 'getChoice' : must return a value"

Thanks.
These are more straightforward than the previous error.

uninitialized local variable 'menuchoice' used

This is because you've declared menuchoice but you have not given it a value before using it in the statement if ( menuchoice == 1). Did you mean to use the getChoice() function to get the menuchoice?

'getChoice' : must return a value"

You said getChoice() returns an int yet it does not do that. It seems you've neglected to put return input after receiving it.
I'm real confused, so you are saying that it needs to be one or the other?

so i just need to either have get choice and leave it at that or..

use the menu choice and remove the get choice?
idk
I was asking if you meant to use the getChoice() function to get the menuchoice.

As in:

1
2
3
4
5
menuchoice = getChoice();

if(menuchoice == 1)
{
    ...
yes we were using it because we were trying to get an input for the menu.

our goals are

1) Limit the user’s input to values that are not negative.
2) Use overloaded functions (two functions with the SAME name) to compute the charges, one for in-patients and one for out-patients. DO NOT have any cin or cout statements in these functions!
3) Use modified versions of getPositiveInt(), one for integers (the days and possibly the menu choice) and one for doubles (the charge amounts). Note that these functions will need two different names. When you make a modified copy for the money, make sure that it returns a double, and uses a double for the variable input.
4) Use double values for money and integer for days.
Alright, well if you call return input; at the end of getChoice() then that compiler error will be fixed.
alright thats fixed, now i have these errors

1>c:\users\asusj\documents\visual studio 2008\projects\bolnitsabolnaya\bolnayabolnitsa.cpp(44) : warning C4700: uninitialized local variable 'menuchoice' used
1>Linking...
1>bolnayabolnitsa.obj : error LNK2019: unresolved external symbol "double __cdecl calculateTotal(double,double)" (?calculateTotal@@YANNN@Z) referenced in function _main
1>bolnayabolnitsa.obj : error LNK2019: unresolved external symbol "double __cdecl calculateTotal(int,double,double,double)" (?calculateTotal@@YANHNNN@Z) referenced in function _main
1>C:\Users\AsusJ\Documents\Visual Studio 2008\Projects\bolnitsabolnaya\Debug\bolnitsabolnaya.exe : fatal error LNK1120: 2 unresolved externals
uninitialized local variable 'menuchoice' used

You have still neglected to give menuchoice a value before it's being used in the if statement...

unresolved external symbol "__cdecl calculateTotal(double,double)" (?calculateTotal@@YANNN@Z) referenced in function _main
unresolved external symbol "double __cdecl calculateTotal(int,double,double,double)" (?calculateTotal@@YANHNNN@Z) referenced in function _main

These errors are appearing because you did not provide any implementations for these functions. You have their prototypes but their definitions do not appear anywhere in this code.
i am obviously missing your point.. umm.. i know that what i wanna do is have menuchoice as a input for the user to choose a menu option....

im not sure what you mean by giving it a value.. so i know im neglecting it.. do you knowhow to fix it?
im not sure what you mean by giving it a value

When I say "giving it a value", I mean assigning a value to it. Assignment in C++ uses the = operator.

1
2
3
4
5
//e.g.
int x; //here, x is uninitialized (does not have a value)
x = 5; //here, 5 is assigned to x. Now, x has the value 5

bool b = false; //here, b is initialized to have the value false 


In your code, you've declared the variable menuchoice

1
2
3
4
5
6
7
8
9
10
11
12
int main()
{
int menuchoice;
int days; // number of days 
double roomRate, // Daily room rate
medication, // Total medication charges, 
services, // Total for tests and other services, 
total;

//here, menuchoice is declared, but uninitialized
//same thing with days, roomRate, medication, services, and total.
//Each of these variables are currently uninitialized 


The difference between menuchoice and the other variables above is that you assign the other variables values before using them while menuchoice remains uninitialized.

When your program reaches this statement

if ( menuchoice == 1)

menuchoice has not been given a value yet, hence the warning given to you by the compiler. Now, all you need to do to make this warning go away is to give menuchoice a value before the above if statement.
thanks for everything.. this has been solved.. it turns out our problem was that we didnt need getchoice at all and we missed a few things a long the way

Thank you so much for everything though. I appreciate what you do.

finished 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
#include <iostream>
#include <string>
using namespace std;

// Function prototypes
int getPositiveInt( string );
double getMoney ( string );


double calculateTotal(int days , double roomRate, double medication, double services) // Inpatient
{
	return days * roomRate + medication + services;
}// calculateTotal() 4 parameters
double calculateTotal(double medication, double services)// Outpatient
{
	return medication + services;
}//	calculateTotal() 2 parameters
int main()
{

int menuchoice; //pick a menu choice
int days; // number of days 
double roomRate, // Daily room rate
medication, // Total medication charges, 
services, // Total for tests and other services, 
total; // total of everything

    // Input and validate patient type
    cout << "This program will compute patient hospital charges.";
   
		//Display munuchoice
		cout << "What type of patient is he/she?" << endl;

		cout << " 1) In-patient. " << endl;
		cout << " 2) Out-patient. " << endl;
		cout << " 3) Leaving. " << endl;

menuchoice = getPositiveInt ("Patient type; ");



	if ( menuchoice == 1)
	{
		days = getPositiveInt( "Enter how long you stay in the hospital:\n  " );
		roomRate = getMoney( "Enter your room charge:\n  " );
		medication = getMoney( "Enter the cost of your medication:\n  " );
		services = getMoney( "Enter the cost of the hospital services:  " );

		    total = calculateTotal( days, roomRate, medication, services );
			cout << "Your total is: " << total << endl;

	}//if
	else if (menuchoice == 2 ) //else if (1)
	{

		medication = getMoney( "Enter the cost of your medication:\n  " );
		services = getMoney( "Enter the cost of the hospital services:  " );

		total = calculateTotal( medication, services );
		cout << "Your total is: " << total << endl;
	}//else if (1)

	else if  (menuchoice == 3) //else if (2)
	{
		cout << " You have left the hospital." << endl;
		cout << " Vy ne ostavili v bol'nitse." << endl;
	} // else if (2)

	system( "pause" );
	return 0;
} // main()

   

int getPositiveInt( string prompt )
{
	int input;

	do
	{
		cout << prompt;

		// While you try to read an integer and FAIL
		while (  ! ( cin >> input ) )
		{
			// Clear the error condition of failing to read an int
			cin.clear();
			// Ignore 1 character, up to a \n
			cin.ignore( 1, '\n' );
		} // while
	} while ( input <= 0 );

	return input;
} // getPositiveInt()

double getMoney( string prompt )
{
	int input;

	do
	{
		cout << prompt;

		// While you try to read an integer and FAIL
		while (  ! ( cin >> input ) )
		{
			// Clear the error condition of failing to read an int
			cin.clear();
			// Ignore 1 character, up to a \n
			cin.ignore( 1, '\n' );
		} // while
	} while ( input < 0 );

	return input;
} // getPositiveInt() 
Topic archived. No new replies allowed.