If else not responding

The if else part is only picking one situation and not the other were am i going wrong.it is meant to select one option and then give results using the overloaded function

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

double calccharges(double days,double dailyrate,double hospmedcharges,double hospserv,double totalcharges);
//returns value for inpatient
double calccharges(double hospserv,double totalcharges);
//returns value for outpatient


double calccharges(double days,double dailyrate,double hospmedcharges,double hospserv,double totalcharges)
{
    totalcharges=((days*dailyrate)+hospmedcharges+hospserv);
    return totalcharges;
}

double calccharges(double hospmedcharges1,double hospserv1, double totalcharges1)
{
    totalcharges1=hospmedcharges1+hospserv1;
    return totalcharges1;
}

int main ()
{ string patienttype,inpatient,outpatient;
double days,dailyrate,hospmeds,services,total;


    cout<<"is the patient an outpatient or an inpatient :";
    cin>>patienttype;

if (patienttype == inpatient)
   {cout<<"Input the number or days spent at the hospital :  ";
    cin>>days;
    cout<<"input the daily rate : ";
    cin>>dailyrate;
    cout<<"input hospital medication charges : ";
    cin>>hospmeds;
    cout<<"input the charges for hospital services  :";
    cin>>services;
    }
else

    {   cout<<"this is an outpatient "<<endl;
        cout<<"input hospital medication charges : ";
        cin>>hospmeds;
    cout<<"input the charges for hospital services  :";
 cin>>services;

}


 cout <<"totalcharges for inpatient are :" <<calccharges(days,dailyrate,hospmeds,services,total)<<endl;

 cout <<"totalcharges for outpatients are :"<<calccharges(hospmeds,services,total)<<endl;

 return 0;
}

if (patienttype == inpatient)
should be
if (patienttype == "inpatient" )
Observe the quotes.

Also, your lines 51 and 53 will BOTH run regardless of the type of patient, but one or other of them will have arguments that haven't been set.
Last edited on
Firstly you forgot to include #include <string> header.

Secondly your function prototype is wrong.
double calccharges(double hospserv, double totalcharges); should perhaps be double calccharges(double hospmedcharges1, double hospserv1, double totalcharges1)

if (patienttype == inpatient) should be if (patienttype == "inpatient" )(What lastchance said)

And,
string patienttype, inpatient, outpatient;
inpatient and outpatient are redundant variables that are not required.

Lastly the output and calculation for inpatient outpatient should be inside their respective blocks.

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
	if (patienttype == "inpatient")
	{
		cout << "Input the number or days spent at the hospital :  ";
		cin >> days;
		cout << "input the daily rate : ";
		cin >> dailyrate;
		cout << "input hospital medication charges : ";
		cin >> hospmeds;
		cout << "input the charges for hospital services  :";
		cin >> services;

		cout << "totalcharges for inpatient are :" << calccharges(days, dailyrate, hospmeds, services, total) << endl;
	}
	else

	{
		cout << "this is an outpatient " << endl;
		cout << "input hospital medication charges : ";
		cin >> hospmeds;
		cout << "input the charges for hospital services  :";
		cin >> services;


		cout << "totalcharges for outpatients are :" << calccharges(hospmeds, services, total) << endl;
	}


Oh and also total needs to be passed by reference.
double& totalcharges1 double& totalcharges (in the function parameters and function prototype parameters)
Notice the '&'. This allows the function to edit the values of the passed variable.
totalcharges shouldn't be an argument full stop. This quantity is being returned by the function.
thank you so much for the assisstance
Topic archived. No new replies allowed.