Solved

if else function c++ not returning valid values

Posted on 2011-09-18
10
527 Views
Last Modified: 2012-05-12
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

0
Comment
Question by:businessesatoz
  • 5
  • 4
10 Comments
 
LVL 84

Expert Comment

by:ozo
Comment Utility
all numbers are either >=100000 or <= 399999
0
 
LVL 84

Accepted Solution

by:
ozo earned 500 total points
Comment Utility
You may have wanted && in place of ||
0
 

Author Comment

by:businessesatoz
Comment Utility
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
0
 
LVL 84

Expert Comment

by:ozo
Comment Utility
500000 >= 100000 is true, so the first if is satisfied.
0
 

Author Comment

by:businessesatoz
Comment Utility
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.
0
6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

 

Author Comment

by:businessesatoz
Comment Utility
May i need to replace the || with an && statement ? i think maybe that might be the problem like u said ? Sorry just learning..
0
 
LVL 7

Expert Comment

by:Slimfinger
Comment Utility
Read ozo's first comment very carefully.
0
 
LVL 84

Expert Comment

by:ozo
Comment Utility
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
0
 

Author Comment

by:businessesatoz
Comment Utility
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.
0
 

Author Closing Comment

by:businessesatoz
Comment Utility
thanks again.
0

Featured Post

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

When writing generic code, using template meta-programming techniques, it is sometimes useful to know if a type is convertible to another type. A good example of when this might be is if you are writing diagnostic instrumentation for code to generat…
Have you thought about creating an iPhone application (app), but didn't even know where to get started? Here's how: ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ Important pre-programming comments: I’ve never tri…
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.

771 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now