Link to home
Start Free TrialLog in
Avatar of businessesatoz
businessesatoz

asked on

if else function c++ not returning valid values

hello i have a if/else statements in a function that is not returning valid values... the program ask the user for an account number the value is then passed to the GetCopayment function and then checks if the account number is over 100,000 but less then 399999 it should return 1, if the account number is <=400000 but less then 599999 it should return 2 but instead keeps returning 1 i don't know why... i have posted my code below

#include <iostream>
using namespace std;
double GetCopayment(double account_number);

int main(void)
{
	char patient_member_check;
cout<<"Is the Patiend a M)ember or N)on-member? ";
cin >> patient_member_check;

if((patient_member_check=='m')||(patient_member_check=='M'))
{
	int account_number;
	double charges;
	cout <<"Please enter your account number ";
	cin >> account_number;
	cout <<"Please enter additional charges";
	cin>> charges;
	GetCopayment(account_number);
	cout <<GetCopayment(account_number);
}

else if((patient_member_check=='n')||(patient_member_check=='N'))
{
	cout <<"your not a member";

}
}

double GetCopayment(double account_number)
{
	if((account_number >=100000)||(account_number <=399999))
	{
		return 1;
	}
    
	else if ((account_number >=400000)||(account_number <=599999))
	{
		return 2;
	}
}

Open in new window

Avatar of ozo
ozo
Flag of United States of America image

all numbers are either >=100000 or <= 399999
ASKER CERTIFIED SOLUTION
Avatar of ozo
ozo
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of businessesatoz
businessesatoz

ASKER

account numbers start over 100000 (6 digits) so they must be over 100000, but less then 399999 to return 1

the next statement account number is over 400000 but less then 599999 and return 2 but only returns 1 even if i put account number 500000 which should return 2
500000 >= 100000 is true, so the first if is satisfied.
but what if the user enters 500000 as their account number  ? it should return 2 not 1 do u understand what i mean? right not it's returing the incorrect value, it's returning 1 instead.
May i need to replace the || with an && statement ? i think maybe that might be the problem like u said ? Sorry just learning..
Read ozo's first comment very carefully.
You told it to return 1 for anything greater or equal to 100000, which includes 500000
and since anything not greater or equal to 100000 will also be less than or equal to 399999, it will also return 1 for all numbers.

you probably meant to say && instead of ||

but then numbers like 399999.5 or 600000 will cause undefined behavior
thanks... i get what i did wrong. :( what an idiot. i will be posting for more help as i'm trying to develop a small dental program.
thanks again.