Reduce Fractions Program (I think it might be a dumb mistake)

This is a discussion on Reduce Fractions Program (I think it might be a dumb mistake) within the C++ Programming forums, part of the General Programming Boards category; Part of our assignment is to reduce two user inputted fractions so they are in their simplest form. The error ...

  1. #1
    Registered User
    Join Date
    Oct 2005
    Posts
    15

    Reduce Fractions Program (identifier not found)

    Part of our assignment is to reduce two user inputted fractions so they are in their simplest form. The error messages I get mostly have to do with my "num" and "denom" for numerator and denomenator being undeclared identifiers, but there may be more. I'm kinda bad at C++. I'd really appreciate any help I can get.

    Code:
    #include <iostream>
    #include <cstdlib>
    using namespace std;
    
    class Fraction
    {
    private:
    	int num;
    	int denom;
    	char something;
    public:
    	Fraction() {}
    	void reduce();
    
    friend istream operator>> (istream, Fraction);
    friend ostream& operator<< (ostream&, const Fraction&);
    };
    
    Fraction::Fraction (int numerator, int denominator, char something)
    {
    	num = numerator;
    	denom = denominator;
                        something = a;
    }
    
    void reduce() 
    {
    	int n, m, r, sign;
    	n = num;
    	m = denom;
    	
    	if (num = 0)
    	{
    		denom = 1;
    		return;
    	}
    
    	else
    	{
    		sign = 1;
    		
    		if (num < 0)
    		{
    			sign = -1;
    			num = -num;
    		}
    
    		if (denom < 0)
    		{
    			sign = -sign;
    			denom = -denom;
    		}
    
    		r = n % m;
    		while (r != 0){
    			n = m;
    			m = r;
    			r = n % m;
    		}
    		num = num * sign / m;
    		denom = denom * sign / m;
    	}
    }
    
    istream operator>> (istream input, int num, int denom, char something, Fraction& oneRatio)
    {
    	input >> num >> something >> denom;
    	return input;
    	oneRatio.reduce();
    }
    
    
    ostream& operator<< (ostream& output, const Fraction& oneRatio)
    {
    	
    	output << oneRatio.num;
    	if (oneRatio.denom != 1)
    		output << "/" << oneRatio.denom;
    	return output;
    }
    
    int main()
    {
    	
    	Fraction fract_1, fract_2;
    	cin >> fract_1 >> fract_2; 
    	cout << "The entered fraction, reduced is "<< fract_1 << endl 
    		<< "The entered fraction, reduced is " << fract_2 << endl << endl;
    	return 0;
    }
    All of my errors concern the use of num and denom in the function reduce. When I set n=num and m=denom I get "undeclared identifier" and everytime I use num and denom for the rest of the function, I get "identifier not found, even with argument-dependent lookup"

    I also get the error 'Fraction::Fraction(int,int,char)' : overloaded member function not found in 'Fraction' in the constructor
    Last edited by rrum; 10-31-2005 at 11:48 PM.

  2. #2
    Registered User
    Join Date
    Apr 2003
    Posts
    2,662
    All of my errors concern the use of num and denom in the function reduce.
    You should also be getting some in your friend functions.

    When I set n=num and m=denom I get "undeclared identifier" and everytime I use num and denom for the rest of the function, I get "identifier not found, even with argument-dependent lookup"
    What's the proper format for defining a member function outside of the class? Inside your class, declare a member function called greeting() which takes no arguments and returns nothing. Define the function outside your class to display "hello". When you get that working, compare it to reduce().

    I also get the error 'Fraction::Fraction(int,int,char)' : overloaded member function not found in 'Fraction' in the constructor
    Well, when I look through your class declaration, I can't see where you declared a constructor that takes 3 arguments. A class can have more than one function with the same name as long as each function has different parameters. When you have more than one function with the same name, that's called 'overloading' and that's what the compiler is refering to.

    By the way, operator functions like >> and << have very specific parameters and return values, so you need to study up on those as well.
    Last edited by 7stud; 11-01-2005 at 01:05 AM.

  3. #3
    Registered User
    Join Date
    Oct 2005
    Posts
    15
    In my istream operator I get "binary 'operator >>' has too many parameters" And then I get an error where I try to cin fract_1 and 2 in the main: "binary '>>' : no operator found which takes a right-hand operand of type 'Fraction' (or there is no acceptable conversion)"
    Theres also an error having to do with the overloaded member function that says "see declaration of 'Fraction' The rest I already stated.

    I added void greeting() to the public part of the class and defined it the same way with just
    Code:
    cout << "hello" << endl;
    and then I added a line in my input operator saying oneRatio.greeting(); It doesn't give me an errors....

    Am I supposed to have something inside the Fraction() {} part of the class?

    I appreciate you taking the time to respond.

  4. #4
    Registered User
    Join Date
    Apr 2003
    Posts
    2,662
    I added void greeting() to the public part of the class and defined it the same way with just

    cout << "hello" << endl;
    Ok. You have so many errors, you're going to have to start over. It's not so bad though because you can cut and paste your old function defintions into your new project. When you write a program, you need to write one function at a time, and then you need to compile and test it to make sure everything works before moving on to the next function.

    So, save your old project and start a version two.

    Generally, you will start by writing the constructors for your class and then test them in main(). After all, you have to be able to construct an object before you can do anything else with it. Since you are getting errors with your constructors that's a good place to start. Make sure all the constructors for your class work by creating objects in main() with each one before moving on. To be clear, there shouldn't be anything else in your class but the constructors and the member variables.

    After that, perform these three steps:

    1) Declare the function greeting() in your class.

    2) Define greeting() outside the class.

    3) In main(), create an object of your class and call greeting() with the object.

    When you get that working, compare it to how you defined reduce(). Performing those steps will demonstrate two things:

    1) what you are doing wrong with reduce()

    2) how to write and test code.

    With every function you write, you must test it in main() to see if it works the way it's supposed to before writing another function.
    Last edited by 7stud; 11-01-2005 at 03:34 AM.

  5. #5
    Registered User
    Join Date
    Oct 2005
    Posts
    15
    aha:
    Code:
    #include <iostream>
    #include <cstdlib>
    using namespace std;
    
    class Fraction
    {
    public:
    	Fraction() {}
    	void greeting();
    
    };
    
    void Fraction::greeting()
    {
    	cout << "Hello" << endl;
    }
    
    
    
    int main()
    {
    	Fraction test;
    	test.greeting();
    
    	return 0;
    }
    Thats one thing down....
    Doing the same thing for reduce() takes away most of my errors, but I still have:
    "c:\Documents and Settings\Kevin\My Documents\Visual Studio Projects\HW11\HW11.h(21) : error C2511: 'Fraction::Fraction(int,int,char)' : overloaded member function not found in 'Fraction'
    c:\Documents and Settings\Kevin\My Documents\Visual Studio Projects\HW11\HW11.h(6) : see declaration of 'Fraction'
    c:\Documents and Settings\Kevin\My Documents\Visual Studio Projects\HW11\HW11.h(70) : error C2804: binary 'operator >>' has too many parameters
    c:\Documents and Settings\Kevin\My Documents\Visual Studio Projects\HW11\HW11.cpp(10) : error C2679: binary '>>' : no operator found which takes a right-hand operand of type 'Fraction' (or there is no acceptable conversion)
    "
    Last edited by rrum; 11-01-2005 at 04:25 AM.

  6. #6
    Registered User
    Join Date
    Oct 2005
    Posts
    15
    Ok, here's what I have now:
    Code:
    #include <iostream>
    #include <cstdlib>
    using namespace std;
    
    class Fraction
    {
    
    public:
    	Fraction() {}
    	Fraction(int, int, char);
    	void reduce();
    
    private:
    	int num;
    	int denom;
    	char something;
    
    friend istream operator>> (istream, Fraction);
    friend ostream& operator<< (ostream&, const Fraction&);
    };
    
    Fraction::Fraction (int numerator, int denominator, char some)
    {
    	num = numerator;
    	denom = denominator;
    	something = some;
    }
    
    void Fraction::reduce() 
    {
    	int n; 
    	int m; 
    	int r; 
    	int sign;
    	n = num;
    	m = denom;
    	
    	if (num = 0)
    	{
    		denom = 1;
    		return;
    	}
    
    	else
    	{
    		sign = 1;
    		
    		if (num < 0)
    		{
    			sign = -1;
    			num = -num;
    		}
    	
    		if (denom < 0)
    		{
    			sign = -sign;
    			denom = -denom;
    		}
    
    		r = n % m;
    		while (r != 0){
    			n = m;
    			m = r;
    			r = n % m;
    		}
    		num = num * sign / m;
    		denom = denom * sign / m;
    	}
    }
    
    istream operator>> (istream input, Fraction oneRatio)
    {
    	input >> oneRatio.num >> oneRatio.something >> oneRatio.denom;
    	return input;
    	oneRatio.reduce();
    
    }
    
    
    ostream& operator<< (ostream& output, const Fraction& oneRatio)
    {
    	
    	output << oneRatio.num;
    	if (oneRatio.denom != 1)
    		output << "/" << oneRatio.denom;
    	return output;
    	
    }
    
    int main()
    {
    	
    	Fraction fract_1, fract_2;
    	cin >> fract_1 >> fract_2; 
    	cout << "The entered fraction, reduced is "<< fract_1 << endl 
    		<< "The entered fraction, reduced is " << fract_2 << endl << endl;
    	return 0;
    }
    Now there are only 2 errors.
    "c:\Documents and Settings\Kevin\My Documents\Visual Studio Projects\HW11\HW11.h(75) : error C2558: class 'std::basic_istream<_Elem,_Traits>' : no copy constructor available or copy constructor is declared 'explicit'
    with
    [
    _Elem=char,
    _Traits=std::char_traits<char>
    ]
    c:\Documents and Settings\Kevin\My Documents\Visual Studio Projects\HW11\HW11.cpp(10) : error C2679: binary '>>' : no operator found which takes a right-hand operand of type 'Fraction' (or there is no acceptable conversion)"

    I'm not sure what to do at this point. If I pass the class by refernce in the istream operator, it actually compiles. But then the reduce function doesn't do anything, I guess thats the whole point in not passing by reference.

  7. #7
    Registered User
    Join Date
    Apr 2003
    Posts
    2,662
    Wow! You've made a lot of progress. Congratulations.

    I'm not sure what to do at this point. If I pass the class by refernce in the istream operator, it actually compiles.
    The first error is due to the fact you aren't following the operator function "idioms", i.e. the blue prints for defining those functions. The blue print for each operator function is very specific, and the blue prints vary depending on the operator, and you don't have leeway to change them. With your operator>> you are returning an istream object. That means you are passing by value. Whenever you pass by value, the compiler has to make a copy of the object--it doesn't matter whether you are sending the function an argument are returning a value from a function, if it is passed by value, then the compiler has to make a copy. The compiler makes copies of objects using the copy constructor. A copy constructor is a constructor that has a reference to the class type as a parameter, e.g.

    Fraction(Fraction& aFraction)

    Just like with constructors, the compiler will provide an invisible default copy constructor which copies the members one for one. Most of the time that will be sufficient(unless you have pointers for member variables).

    I'm not quite sure why you are getting that particular copy constructor error, but fix the format of your operator>> and it should go away. I'm not sure why you are getting the second error, so fix what you can and see if it goes away. In the future, please post a comment on the line where the error is occuring.

    I'm not sure what to do at this point. If I pass the class by refernce in the istream operator, it actually compiles. But then the reduce function doesn't do anything, I guess thats the whole point in not passing by reference.
    Your reduce() function doesn't have anything to do with your operator>>function, so I'm not sure why you think changing your operator>> function will fix reduce().

    Finally, how will this line:

    oneRatio.reduce();

    ever execute?
    Last edited by 7stud; 11-01-2005 at 12:42 PM.

  8. #8
    Registered User
    Join Date
    Oct 2005
    Posts
    15
    DONE AND DONE! THANK GOD. And by God I mean you

    Stayed up all night, and now I gotta statics test tonight to study for! Yay!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Please help! - Who find mistake in the program??
    By nivek in forum C++ Programming
    Replies: 4
    Last Post: 01-06-2008, 12:28 AM
  2. Using variables in system()
    By Afro in forum C Programming
    Replies: 8
    Last Post: 07-03-2007, 12:27 PM
  3. BOOKKEEPING PROGRAM, need help!
    By yabud in forum C Programming
    Replies: 3
    Last Post: 11-16-2006, 10:17 PM
  4. I need some help with my program please.
    By agentxx04 in forum C Programming
    Replies: 9
    Last Post: 09-26-2004, 07:51 AM
  5. My program, anyhelp
    By @licomb in forum C Programming
    Replies: 14
    Last Post: 08-14-2001, 10:04 PM

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