function getting user input and passing it to another function

Hi all

Hope all are fine. I have a problem in which I have to find out billing amount of lawyer for his client. I need to get the following inputs from user: hourly rate,consultation time, yes or no response for the condition(employee income<=25000), then calculate the billing amount. the calculation part function I have done. But I dont how to get the user input as a function and return it to the main or other function which calculate billing amount.

I have given the code below, that I have done so far. But not working. Please help me.

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

double input();//function 1
double billing_amount(int minutes,double hourly_rate,char b);//function 2
int main()
{
	
	
	input();
	
	system("pause");
	return 0;
}

//User Input Function
double input()
{
	int hrs,int min,double hr_rate,char a;
	double service_charge;
	cout<<"\nEnter the hourly rate:$ ";
	cin>>hr_rate;
	cout<<endl;
	
	cout<<"Consulting Time:"<<endl;
	cout<<"NOTE: Enter '0' for hours if the consulting time is less than a hour"<<endl;
	cout<<"Hours: ";
	cin>>hrs;
	
	int hrs2min=hrs*60;

	cout<<"\nMinutes: ";
	cin>>min;
	min=hrs2min+min;

	

	cout<<"Enter 'Y' or 'y' if your income less than or equal to 25000"<<endl;
	cout<<"Enter 'N' or 'n' if your income greater than 25000"<<endl;
	cout<<"'Y/y' or 'N/n': ";
	cin>>a;
	while(a!='Y'||a!='y'||a!='N'||a!='n')
	{
		cout<<"Invalid Input!"<<endl;
		cout<<"Enter 'Y/y' or 'N/n': ";
		cin>>a;
		cout<<endl;
	}
	
	cout<<billing_amount(min,hr_rate,a);
	
}
//billing amount calculating function
double billing_amount(int minutes,double hourly_rate,char b)
{
	
	double charge;
	int h,m;
	
	if(b=='Y' ||b=='y')
	{
		if(minutes<=30)
		{
			charge=0;
		}
		else if(minutes>30 && minutes<60)
		{
			minutes=minutes-30;
			
			charge=hourly_rate*0.4*(minutes/60);
		}
		else
		{
			minutes=minutes-30;
			h=minutes/60;
			m=minutes%60;
			charge=hourly_rate*(h+(0.4*(m/60)));
		}
		
	}
	else
	{
		if(minutes<=20)
		{
			charge=0;
		}
		else if(minutes>20 && minutes<60)
		{
			minutes=minutes-20;
			charge=hourly_rate*0.7*(minutes/60);
		}
		else
		{
			minutes=minutes-20;
			h=minutes/60;
			m=minutes%60;
			charge=hourly_rate*(h+(0.7*(m/60)));
		}
		
	}
	cout<<charge<<endl;
}





You've got some mistakes:
1
2
int hrs,int min,double hr_rate,char a;
	double service_charge;

you should separate variables using ;.

double input() should be changed to void input() since you don't return any values.

Finally, this is the correct way:
while(a!='Y'&& a!='y'&& a!='N'&& a!='n')

If you want to return a value to main, I suggest you declare a structure:
1
2
3
4
struct lawyer_info
{
    //variables
}


and changing the input function to lawyer_info input()
or void input(lawyer_info &input)

These might help:
http://www.cplusplus.com/doc/tutorial/structures/
http://www.cplusplus.com/doc/tutorial/functions2/ (passing by reference)
Last edited on
Hello maniax,

Thanks for you help. And sorry for the late reply. I used your first 3 points and solved my problem by somehow. here is the code

I didnt use struct because I havent studied that topic yet.

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

void input(int &a_hrs,int &b_min,double &c_rate,char &d_res);//function 1
double billing_amount(int t_min,double h_rate,char reply);//function 2
int main()
{
	cout<<"hewerwreo"<<endl;//to check whether current code or previous code is working
	
	int hours,minutes,temp;
	char response,rerun_option;
	double hourly_rate,bill;
	do
	{
	input(hours,minutes,hourly_rate,response);
		
	temp=hours*60+minutes;
	bill=billing_amount(temp,hourly_rate,response);
	if(bill==0)
		cout<<"THere is no charges for you"<<endl;
	else
		cout<<"Service charge:$ "<<bill<<endl;

	cout<<"Do you run the program again:(Y/N) ";
	cin>>rerun_option;
	
	
	}while(rerun_option=='y' || rerun_option=='Y');
	
	system("pause");
	return 0;
}

//User Input Function
void input(int &a_hrs,int &b_min,double &c_rate,char &d_res)
{
	int temp,minutes;
	cout<<"Enter the hourly rate:$ ";
	cin>>c_rate;
	cout<<endl;
	
	cout<<"Consulting Time:"<<endl;
	cout<<"NOTE: Enter '0' for hours if the consulting time is less than a hour"<<endl;
	cout<<"Hours: ";
	cin>>a_hrs;
	
	

	cout<<"\nMinutes: ";
	cin>>b_min;
	
	

	cout<<"Enter 'Y' or 'y' if your income less than or equal to 25000"<<endl;
	cout<<"Enter 'N' or 'n' if your income greater than 25000"<<endl;
	cout<<"'Y/y' or 'N/n': ";
	cin>>d_res;
	while(d_res!='Y' && d_res!='y' && d_res!='N' && d_res!='n')
	{
		cout<<"Invalid Input!"<<endl;
		cout<<"Enter 'Y/y' or 'N/n': ";
		cin>>d_res;
		cout<<endl;
	}
	
	
	
}
//billing amount calculating function
double billing_amount(int t_min,double h_rate,char reply)
{
	
	double charge,m;
	int h,rem_min;
	
	if(reply=='Y' ||reply=='y')
	{
		if(t_min<=30)
		{
			charge=0;
		}
		else 
		{
			rem_min=t_min-30;
			if(rem_min<60)
			{
				charge=(h_rate*0.40*rem_min)/60;
			}
			else
			{
				h=rem_min/60;
				m=rem_min%60;
				charge=h_rate*h+h_rate*0.40*(m/60);
			}
		}
	}
	else
	{
		if(t_min<=20)
			charge=0;
		else
		{
			rem_min=t_min-20;
			if(rem_min<60)
			{
				charge=h_rate*0.70*(rem_min/60);
			}
			else
			{
				h=rem_min/60;
				m=rem_min%60;
				charge=h_rate*h+h_rate*0.70*(m/60);
			}
		}
		
	}
	return charge;
}
Topic archived. No new replies allowed.