/*I am a noob and can not seem to be able to pass a random number to cout. I have tried everything amd am looking for a little guidance*/

#include <iostream>
#include <ctime>
using namespace std;

class Fraction
{
   private:
            int num; // Numerator
            int den; // Denominator

    public:
            int num2;
			int den2;			
			double setNumerator() {return num;} //sets numerator
			double setDenominator() {return den;} //sets Denominator
			double setNumerator2() {return num2;} //sets numerator 2
			double setDenominator2() {return den2;} //sets Denominator 2
			int Reduce(); //will reduce the fraction to lowest terms
			void print(); //will print the fraction in the form: numerator / denominator
			void Result(); //will multiply the fractions
			Fraction(); // constructor
            Fraction(int,int); // 2 Argument Constructor
			
            
};

Fraction::Fraction()
   {
     num=0;
     den=1;
   }

Fraction::Fraction(int n,int d)
   {
     num=n;
     den=(d==0)? 1 :d;
     
   }
   
double setNumerator (int num) //sets numerator 1
{
	
	srand ( time(NULL) );
	num = rand() % 9 + 1;
	
	return num;	
}

double setDenominator (int den) //sets denominator 1
{
	
	srand ( time(NULL) );
	den = rand() % 9 + 1;

	return den;	
}

double setNumerator2 (int num2) //sets numerator 2
{
	
	srand ( time(NULL) );
	num2 = rand() % 9 + 1;

	return num2;	
}

double setDenominator2 (int den2) //sets denominator 2
{
	
	srand ( time(NULL) );
	den2 = rand() % 9 + 1;

	return den2;	
}


//Fract2.setNumerator(rand() % 9 + 1); 

//Fract2.setDenominator(rand() % 9 + 1); 

//Result.setDenominator(Fract1.getDenominator() * Fract2.getDenominator());

void Result(int &num, int &den, int &num2, int &den2) //will multiply the results from the random numbers         
{
        int calcnum;  
		int calcden;		
		calcnum = num * num2;      
        calcden = den * den2;         
}
  
int Reduce(int a, int b) //will reduce Result
{

	int calcden = a; 
	int calcnum = b;
	
	if(b == 0)
	{
	        return a;
	}
	else
	{
		return Reduce(b, a % b);
	}
}

void print(int &num, int &den) //will print out what the fraction is
{
        cout << "The reduced and added fraction is " << num << "/" << den << endl;
}

int main() //the main
{
   int num = 0, den = 0, num2 = 0, den2 = 0;
   int calcnum = 0;
   int calcden = 0;
   int a = 0;
   int b = 0;
   int m = 0;
   int n = 0;
   cout << "What is  " << num << "/" << den<< "*" << num2<< "/" <<den2<< "?"<<endl; 
   cout << "enter numerator / denominator" << "  " << "Your answer must be in reduced form" << endl;
   cin >> num >> den;
   //Result(num, den, num2, den2); 
   Reduce(a, b);
   print(num, den);
   cout << endl;
      
	return(0);
}

You should call srand() only once and in main() before you call the methods that use rand(). What problems are you having with the output? Can you determine (using cout statements) where the data are getting lost?

Do you have numbers in your setNumerator and setDenominator (1 and 2) functions? If so go one step futher and make sure they are still there. Put a cout statement with the values and maybe a nice message like "I'm in setNumerator and num = " then display num. Do this for all those methods and work your way out. There is no other way to do it (aside from stepping through on a debugger).

my setNumerator and setDenominator are empty. Should i set a number there?

double setNumerator() {return num;} //sets numerator
double setDenominator() {return den;} //sets Denominator
double setNumerator2() {return num2;} //sets numerator 2
double setDenominator2() {return den2;} //sets Denominator 2

In your declaration at the top take out the {} and everything in them and just put a ; after each
You do have implementations for these, you should use them.

Edited 6 Years Ago by jonsca: n/a

srand is the seeding function. You do not need it each time you call rand() it just needs to be executed one time only before your first call to rand(). That's why I was saying put it inside main() at the top.

I'm pretty sure the problem is not in your setNumerator, setDenominator etc. but check to see what the value is in that function anyway so you can start to get some concept of debugging. After thatc check to make sure that the values are getting back to the calling function, etc., etc.

Edited 6 Years Ago by jonsca: n/a

Well on line 121 they are going to be zeros because you have assigned them as such up at the top. I've looked back and it doesn't seem like you are even using the methods we are going over. It's getting to be impossible to help you because you don't understand your own code.

Your right. I have made so many changes trying figure out the probs on my own I am unsure of the code now. That is why I posted it for some help. I have made changes to the code I am working on at home as you have suggested with out any luck.

#include <iostream>
#include <ctime>
using namespace std;

class Fraction
{
   private:
            int num; // Numerator
            int den; // Denominator

    public:
            int num2; 
			int den2;			
			double setNumerator(); //sets numerator
			double setDenominator(); //sets Denominator
			double setNumerator2(); //sets numerator 2
			double setDenominator2(); //sets Denominator 2
			int Reduce(); //will reduce the fraction to lowest terms
			void print(); //will print the fraction in the form: numerator / denominator
			void Result(); //will multiply the fractions
			Fraction(); // constructor
            Fraction(int,int); // 2 Argument Constructor
			
            
}; //end class Fraction

Fraction::Fraction() //constructor
   {
     num=0; //initializes num to 0
     den=1; //initializes den to 1
   }

Fraction::Fraction(int n,int d)  // 2 Argument Constructor
   {
     num=n;
     den=(d==0)? 1 :d;
     
   }
   
void setNumerator (int num) //sets numerator 1
{
	
	srand ( time(NULL) );
	num = rand() % 9 + 1;
	
	//return num;	//returns num
}

void setDenominator (int den) //sets denominator 1
{
	
	srand ( time(NULL) );
	den = rand() % 9 + 1;

	//return den;	//returns den
}

void setNumerator2 (int num2) //sets numerator 2
{
	
	srand ( time(NULL) );
	num2 = rand() % 9 + 1;

	//return num2; //returns num2	
}

void setDenominator2 (int den2) //sets denominator 2
{
	
	srand ( time(NULL) );
	den2 = rand() % 9 + 1;

	//return den2; //returns den2	
}

void Result(int &num, int &den, int &num2, int &den2) //will multiply the results from the random numbers         
{
        int calcnum;  
		int calcden;		
		calcnum = num * num2; //multiplies num and num2 together     
        calcden = den * den2; //multiplies den and den2 together             
}
  
int Reduce(int a, int b) //will reduce Result
{

	int calcden = a; //multiplied denominator
	int calcnum = b; //multiplied numerator
	
	if(b == 0) //GCD from dreamincode.net, serious trouble with it working
	{
	        return a; //whole number
	}
	else
	{
		return Reduce(b, a % b); //returns reduced denominator and numerator in num / den order
	}
}

void print(int &num, int &den) //will print out what the fraction is
{
        cout << "The reduced and added fraction is " << num << "/" << den << endl;
}

int main() //the main
{
   rand ();
   int num = 0, den = 0, num2 = 0, den2 = 0;
   int calcnum = 0;
   int calcden = 0;
   int a = 0;
   int b = 0;
   cout << "What is  " << num << "/" << den << "*" << num2 << "/" << den2 << "?" << endl; 
   //cout << "What is  3/4 * 2/5 ?" << endl; //test line to see why 0's show for the num and den
   cout << "<enter numerator / denominator>" << "  " << "Your answer must be in reduced form" << endl;
   cin >> num >> den;
   //Result(num, den, num2, den2); 
   Reduce(a, b);
   print(num, den);
   cout << endl;
      
	return(0);
}

So you understand that on 113 those numbers have not changed from when you set them to zero above? Remove 43, 52, 61,70 and change 107 to an srand((unsigned)time(0)) . In fact I would say get rid of all that setNumerator, setDenominator, etc. stuff completely. Setters do not return anything normally, the values you are passing in mean nothing and lastly you're not calling them anywhere. I would make your default constructor for the class set the values to random numbers.

Edited 6 Years Ago by jonsca: n/a

how would you have the default set the values? Set line 44 for example in the constructor?

One of the overarching issues though that you have to deal with is to make the class hold only one fraction at a time. I suspect that was the intention of the assignment.
So just have a num and denom members of your class and have one set of "getters" for numerator and denominator and one set of setters for numerator and denominator. Hint on these, the prototype for getters is int getNumerator(); and void setNumerator(int); so clean out the versions you have and make 4 that cover numerators and denominators.

I would set your values to the random one on lines 29-30. Let a random fraction be the default of your class.

How would I set one set of getters and one set of setters when i need two sets of numerators and denominators?

How would I set one set of getters and one set of setters when i need two sets of numerators and denominators?

Each fraction object is going to hold just that, one fraction.
Here's the first few lines of my main()

Fraction frac1(10,36);
	Fraction randomfrac;
	Fraction frac2(4,29);

	frac1.Print();
	frac2.Print();
	frac1.Reduce();
	frac1.Print();

That should give you a bit of a push towards what you should do.

This article has been dead for over six months. Start a new discussion instead.