confused on function, pls help me, urgent!!

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..
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
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..
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
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..
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;
	}
my concern :

I want to pass user input pass to function at main..
Use a reference and the parameter you pass will actually be modified.
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
uh what is this?

I want to pass user input which in main, to function.. not get user input in function..
Last edited on
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;
}
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
no ToniAz, HiteshVaghani1 solved my needs..

thanks HiteshVaghani1!
Topic archived. No new replies allowed.