Help With Member Data Input

This is a discussion on Help With Member Data Input within the C++ Programming forums, part of the General Programming Boards category; Let me brief you: I am trying to create a simple fraction calculator using a class named Fraction. I spent ...

  1. #1
    Set Apart -- jrahhali's Avatar
    Join Date
    Nov 2002
    Posts
    256

    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.add(frac1, frac2); break;
    			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
    Clear the mines from our Shazbot!
    Get the enemy Shazbot!

  2. #2
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    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 )
    My best code is written with the delete key.

  3. #3
    Set Apart -- jrahhali's Avatar
    Join Date
    Nov 2002
    Posts
    256
    *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.
    Clear the mines from our Shazbot!
    Get the enemy Shazbot!

  4. #4
    Set Apart -- jrahhali's Avatar
    Join Date
    Nov 2002
    Posts
    256
    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
    Clear the mines from our Shazbot!
    Get the enemy Shazbot!

  5. #5
    Registered User
    Join Date
    Mar 2002
    Posts
    1,595
    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. #6
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    >could i use the conditional operator to short form this
    No, not really.
    My best code is written with the delete key.

  7. #7
    Registered User jlou's Avatar
    Join Date
    Jul 2003
    Posts
    1,088
    Quote 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;
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 48
    Last Post: 09-26-2008, 03:45 AM
  2. problem collecting data from input file
    By gkoenig in forum C Programming
    Replies: 12
    Last Post: 03-30-2008, 07:40 AM
  3. singly linked circular list
    By DarkDot in forum C++ Programming
    Replies: 0
    Last Post: 04-24-2007, 08:55 PM
  4. Problem with Visual C++ Object-Oriented Programming Book.
    By GameGenie in forum C++ Programming
    Replies: 9
    Last Post: 08-29-2005, 11:21 PM
  5. Message printing problem
    By robert_sun in forum C Programming
    Replies: 1
    Last Post: 05-18-2004, 05:05 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21