Solved

if else function c++ not returning valid values

Posted on 2011-09-18
10
533 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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

This tutorial is posted by Aaron Wojnowski, administrator at SDKExpert.net.  To view more iPhone tutorials, visit www.sdkexpert.net. This is a very simple tutorial on finding the user's current location easily. In this tutorial, you will learn ho…
This is a short and sweet, but (hopefully) to the point article. There seems to be some fundamental misunderstanding about the function prototype for the "main" function in C and C++, more specifically what type this function should return. I see so…
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 member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

738 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