Well if you really want to do maths and calculate it yes you can do it like that.
I would probaby assign the fees like this(as AmitTheInfinity said):
EDIT: Just realized he didn't really said it this way... but close enough...
cpp
// Check if the user is a member
if( memberStatus == 'M' )
{
// Check age
if( age < 65 ) // If the member is less than 65 years old...
fee = 10; // ...we charge him 10...$?
else
fee = 5; // If the member is 65 years or older his fee is 5
}
else
fee = 20; // If he isn't a member we don't bother about the age
But if you really want to do it with maths and calculate it then...
cpp
int age_60 = 2, age_65 = 1, no_member = 4, charge = 5;// Check if the user is a member
if( memberStatus == 'M' )
{
// Check age
if( age < 65 ) // If the member is less than 65 years old...
fee = age_60 * charge; // ...we charge him 10...$?
else
fee = age_65 * charge; // If the member is 65 years or older his fee is 5
}
else
fee = no_member * charge; // If he isn't a member we don't bother about the age
You could of course do it with structs, classes, switch statements or whatever way you want to but the first example really is a simple one and a good way to go in this particular case since the simplest way is usually the best way. If you don't really have to do it any other way... or just want to just for the fun of coding
This post has been edited by Linkowiezi: 16 Oct, 2008 - 04:31 AM