Solved

if else function c++ not returning valid values

Posted on 2011-09-18
10
530 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
ID: 36558101
all numbers are either >=100000 or <= 399999
0
 
LVL 84

Accepted Solution

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

Author Comment

by:businessesatoz
ID: 36558112
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
Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

 
LVL 84

Expert Comment

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

Author Comment

by:businessesatoz
ID: 36558133
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
 

Author Comment

by:businessesatoz
ID: 36558135
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
ID: 36558140
Read ozo's first comment very carefully.
0
 
LVL 84

Expert Comment

by:ozo
ID: 36558157
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
ID: 36558166
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
ID: 36558173
thanks again.
0

Featured Post

Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Many modern programming languages support the concept of a property -- a class member that combines characteristics of both a data member and a method.  These are sometimes called "smart fields" because you can add logic that is applied automaticall…
Examines three attack vectors, specifically, the different types of malware used in malicious attacks, web application attacks, and finally, network based attacks.  Concludes by examining the means of securing and protecting critical systems and inf…
The goal of this video is to provide viewers with basic examples to understand opening and writing to files in the C programming language.
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use for-loops in the C programming language.

786 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