# Thread: Help With Member Data Input

1. ## Help With Member Data Input

Let me brief you: I am trying to create a simple fraction calculator using a class named Fraction. I spent enough time trying to get the lowterms() member function to work. (still not sure if it does work, if you see a problem tell me) But the program still wouldn't output the correct answer. Anyways, i 'watched' the variables ff1.num and ff2.den in the member function getFrac() and it turns out they don't change, even though i'm promting for input. For example, if i created a constructor to set all objects data to 0, then call the function getFrac(), num and den are initialized, but ff1.num and ff2.den don't change; they still remain 0;

.NOTE: den = denominator, num = numerator, gcd = greatest common divisor
Code:
#include <iostream.h>

char oper;   // for operation on fractions

class Fraction
{
private:
int num, den;  // numerator and denominator
public:
void getFrac( Fraction ff1 )
{
char misc; // character inbetween num and den
cin >> num >> misc >> den >> oper
>> ff1.num >> misc >> ff1.den; // <--PROBLEM HERE!!!
}
void add( Fraction ff1, Fraction ff2 )
{
num = ff1.num*ff2.den + ff1.den*ff2.num;
den = ff1.den*ff2.den;
}
void sub( Fraction ff1, Fraction ff2 )
{
num = ff1.num*ff2.den - ff1.den*ff2.num;
den = ff1.den*ff2.den;
}
void mul( Fraction ff1, Fraction ff2 )
{
num = ff1.num*ff2.num;
den = ff1.den*ff2.den;
}
void div( Fraction ff1, Fraction ff2 )
{
num = ff1.num*ff2.den;
den = ff1.den*ff2.num;
}
void display()
{ cout << num << '/' << den; }
void lowterms();
};

void Fraction::lowterms()
{
enum testRun { didntRun, didRun };
testRun whileLoopRan = didntRun;
for(int gcd = 2; gcd <= den/2; gcd++ )
{
while(!(num%gcd) && !(den%gcd) )
{
num = num / gcd;
den = den / gcd;
whileLoopRan = didRun;
}
if( whileLoopRan )
{ gcd = 1; whileLoopRan = didntRun; }
}
}
int main()
{
Fraction frac1, frac2, fracAns;
char choice = 99;

while( choice != 'n' && choice != 'N' )
{
cout << "Enter fraction, operator, second fraction: ";
frac1.getFrac( frac2 );
switch(oper)
{
case '-': fracAns.sub(frac1, frac2); break;
case '*': fracAns.mul(frac1, frac2); break;
case '/': fracAns.div(frac1, frac2); break;
default: cout << "\nIllegal operator.";
continue;
}
fracAns.lowterms();
cout << "Answer = "; fracAns.display();
cout << "\nAgain(y/n)?: "; cin >> choice;
}
return 0;
} // end main
I suspect that that's the biggest problem - that the second fraction the user enters ( ff1.num, ff2.num ) are not initilized to what hte user enters. thus, giving a totalaly wrong output. Why won't teh value of ff1.num and ff1.den change when prompted for input? thanks

2. In getfrac, ff1 is a copy of the object you passed, not the object itself. When you return to main, frac2 remains uninitialized. Pass a reference to change frac2 as well:
Code:
void getFrac( Fraction& ff1 )

3. *CLAP CLAP* great . wow, i love when you struggle with somthing fo so long and then the answer is simple. love it and then i say to myselt "what the hec!" i didn't see that. "Now i know! cheers.

4. oh and one more quetsion:
could i use the conditional operator to short form this:
Code:
void display()
{
if( num==den )
cout << num;
else
cout << num << '/' << den;
}
i was thinking cout << ( (num==den) ? num : num << '/' << den; )
buuuuut, it didn't work

5. Do you mean the if/else syntax didn't display anything? or the tertiary statement didn't show anything eventhogh the if/else syntax did, or that both statements worked by gave the wrong result. "it didn't work" could mean any of the above, or something else. Be as specific as you can.

BTW---usually if num == den the value is unity, not num, (that is 4/4 == 1, not 4); but you can do it however you want.

6. >could i use the conditional operator to short form this
No, not really.

7. Originally Posted by jrahhali
oh and one more quetsion:
could i use the conditional operator to short form this:
Code:
void display()
{
if( num==den )
cout << num;
else
cout << num << '/' << den;
}
i was thinking cout << ( (num==den) ? num : num << '/' << den; )
buuuuut, it didn't work
Why would you want to? Even if the tertiary operator had worked, it doesn't look very clear (clarity vs. brevity).

Maybe:
Code:
void display
{
cout << num;
if (den != 1)
cout << '/' << den;
}