confused on function, pls help me, urgent!!

Jul 5, 2012 at 1:47am
double PayRatesOne(double r){

char Customer;

cout << "Enter the type of customer (W/M/S) > ";
cin >> Customer;

if(Customer == 'W'){
r = 4.50;
return (r);

}else if(Customer == 'M'){
r = 4.00;
return (r);

}else if(Customer == 'S'){
r = 3.50;
return (r);
}

int main(){
double Rates;
PayRatesOne(Rates);

cout << Rates;
}


How I pass r to Rates instead Rates to r? the result I got is zero..
Jul 5, 2012 at 1:56am
First of all, the iostream interface (if we can call it this way) is better be done outside your function, that is, in main. So your function should take two parameters, the rate and the type of customer, a double and a char.

Could you carry on after this?
Last edited on Jul 5, 2012 at 1:56am
Jul 5, 2012 at 2:01am
I'm stuck of getting parameter pass back to main function, that's why I try to figure out how it work 1st, I know I should take 2 parameter..
Jul 5, 2012 at 2:10am
Did you copy/paste this code from your editor? You missed an end brace '}' for the last condition, plus it's else not else if for the final condition.

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


double PayRatesOne(char c)
{
    double r;
    if(c == 'W')
    {
        r = 4.50;
        return (r);
    }
    else if(c == 'M')
    {
        // ...
    }
    // ...
    
}

int main()
{
    // ...
    char customer;
    cout << "Enter the type of customer (W/M/S) \n";
    cin >> customer;


    // ...
}
Last edited on Jul 5, 2012 at 2:13am
Jul 5, 2012 at 3:01am
how u can use the PayRatesOne to receive user input? and then the r? I need to calculate it in the main function, i avoid global variable..
Jul 5, 2012 at 3:40am
You should be able to fill in the other conditions on your own and you should have a default in switch in case you select wrong letter.

Notice @ 13 - 14. Your code would have missed lower case letters

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
#include <iostream>

using namespace std;

double PayRatesOne (void) {
	char Customer;
	double r;
	
	cout << "Enter type of Customer (W/M/S) > ";
	cin >> Customer;
	
	switch ( Customer ) {
		case 'W':
		case 'w':
			r = 4.50;
			break;			
		}
	
	return r;
	}
	
int main() {
	double Rates;
	Rates = PayRatesOne ();
	cout << Rates;
 	return 0;
	}
Jul 5, 2012 at 4:28am
my concern :

I want to pass user input pass to function at main..
Jul 5, 2012 at 5:00am
Use a reference and the parameter you pass will actually be modified.
Jul 5, 2012 at 5:14am
Use pass by reference concept.


void PayRatesOne(double & r)
{
    char Customer;
    cout << "Enter the type of customer (W/M/S) > ";
    cin >> Customer;

    if(Customer == 'W')
        r = 4.50;
    else if(Customer == 'M')
        r = 4.00;
    else if(Customer == 'S')
        r = 3.50;
}

int main()
{
    double  Rates;
    PayRatesOne(Rates);
    
    cout << Rates;
    return 0;
}


please try to follow some coding conventions that will be useful to you in future.
Last edited on Jul 5, 2012 at 5:25am
Jul 5, 2012 at 5:17am
uh what is this?

I want to pass user input which in main, to function.. not get user input in function..
Last edited on Jul 5, 2012 at 5:22am
Jul 5, 2012 at 7:11am
ohhh sorry for misunderstood.

here is your requirement.

double PayRatesOne(char key)
{
    double r;
    r= (key == 'W' ? 4.50 :(key == 'M'?4.00 :(key == 'S'?3.50 : 1)));
    return r;
}

int main()
{
    double Rates;
    char Customer;
    cout << "Enter the type of customer (W/M/S) > ";
    cin>>Customer;
    
    Rates = PayRatesOne(Customer);
    cout << Rates;
    
    return 0;
}
Jul 6, 2012 at 6:49am
I don't think he understood the concept, because basically TightCoder and I provided him with the same code as above, with some missing statements...

:S
Jul 6, 2012 at 7:21am
no ToniAz, HiteshVaghani1 solved my needs..

thanks HiteshVaghani1!
Topic archived. No new replies allowed.