Solved

if else function c++ not returning valid values

Posted on 2011-09-18
10
534 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
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:Bill Nolan
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

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

C++ Properties One feature missing from standard C++ that you will find in many other Object Oriented Programming languages is something called a Property (http://www.experts-exchange.com/Programming/Languages/CPP/A_3912-Object-Properties-in-C.ht…
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 how to use strings and some functions related to them in the C programming language.
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.

626 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