Passing an object into a function

Hey there,
I have an issue with a some code:

This is supposed to check for valid input
1
2
3
4
5
6
7
8
9
10
11
12
	if(((c.integer < 0)&&(c.numerator < 0))||(c.denominator<=0))
	{
		c.integer = 0;
		c.numerator = 0;
		c.denominator = 0;
	}
	if((c.integer>0)&&(c.numerator<0))
	{//WORKS 2-13-13
		c.integer = c.integer*-1;
		c.numerator = abs(c.numerator);
		c.denominator = abs(c.denominator);
	}


And it does just fine, until it's passed into a function, then it doesn't work:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
void validInput(Mixed a)
{
	if(((a.integer < 0)&&(a.numerator < 0))||(a.denominator<=0))
	{
		a.integer = 0;
		a.numerator = 0;
		a.denominator = 0;
	}
	if((a.integer>0)&&(a.numerator<0))//Minus sign shift
	{
		a.integer = a.integer*-1;
		a.numerator = abs(a.numerator);
		a.denominator = abs(a.denominator);
	}
}

It's been driving me nuts! It works, when the object is passed, except for two cases (one where the minus sign shifts) and whenever there is a zero or a negative integer in the denominator. Any thoughts?

Also, I'm passing the function like validInput(c);
Last edited on
Does having zero in the denominator mean your trying to divide by zero? You also can't have a negative denominator since that "flips" the fraction:

1/-1 = -1/1

If you checking for valid input for a fraction the main things to bear in mind are you must have a non zero denominator and if the denominator is negative you need to flip the fraction.

But yeah setting the denominator to zero (especially when it could already be zero) should be changed.
Because in void validInput(Mixed a) you modify copy of a.
Simply pass a by reference:
void validInput(Mixed& a)


tfityo, I see your logic behind this, however, It's popping out an error:
error C2668: 'validInput' : ambiguous call to overloaded function
I think that you defined two functions (or maybe post your full code):
void validInput(Mixed& a)
and
void validInput(Mixed a)

Rename or delete the second one.
Topic archived. No new replies allowed.