GIDForums  

Go Back   GIDForums > Computer Programming Forums > CPP / C++ Forum
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 01-May-2006, 00:35
iceman2006 iceman2006 is offline
New Member
 
Join Date: Apr 2006
Location: Hawaii
Posts: 21
iceman2006 is on a distinguished road

Fraction calculations not displaying


The goal of my program is to enter 2 fractions and display the results of calculations on the 2 fractions. When I run the program I am able to enter the two fractions, but am unable to view the results of the calculations. I've looked at some similar code, but they were above my skill level. I don't know what I'm doing wrong. I've added a few comments in the code. Thanks in advance for any help.


CPP / C++ / C Code:
#include <iostream>
#include <cmath>
#include <stdio.h>


using namespace std;

class fractionType
{	
public:

    void setFractionX(double Xnumerator, double Xdenominator);
    void setFractionY(double Ynumerator, double Ydenominator);
    double add();
    double subtract();
    double multiply();
    double divide();
    void print();
    double xnum, xden;
    double ynum, yden;
    double znum, zden; 
    double Xnumerator, Xdenominator;
    double Ynumerator, Ydenominator;
};

void fractionType::setFractionX(double Xnumerator, double Xdenominator)
{
	xnum = Xnumerator;
	xden = Xdenominator;
}
void fractionType::setFractionY(double Ynumerator, double Ydenominator)
{
	ynum = Ynumerator;
	yden = Ydenominator;
}
double fractionType::add()
{	
	znum = xnum * yden + ynum * xden;
	zden = xden * yden;
                return znum, zden;
	cout <<xnum <<"/"<<xden <<" + " <<ynum <<"/"<<yden <<" = " <<znum <<"/"<<zden << endl;  // just to see if anything prints... can I put this statement in here?

  }
double fractionType::subtract()
{	
	znum = xnum * yden - ynum * xden;
	zden = xden * yden;
}
double fractionType::multiply()
{	
	znum = xnum * ynum;
	zden = xden * yden;
}
double fractionType::divide()
{	
	znum = xnum * yden;
	zden = xden * ynum;
}

void fractionType::print()
{
	cout <<xnum <<"/"<<xden <<" + " <<ynum <<"/"<<yden <<" = " <<add() << endl;  // just to see if anything prints
	cout <<xnum <<"/"<<xden <<" - " <<ynum <<"/"<<yden <<" = " <<znum <<"/"<<zden << endl;  // just to see if anything prints
}


int main()
{
    fractionType Fraction;
	double Xnumerator, Xdenominator;
	double Ynumerator, Ydenominator;
	char slash;

	cout << "Enter first fraction: ";
	scanf("%d%c%d",&Xnumerator,&slash,&Xdenominator);  //is this the appropriate way to use this?
	Fraction.setFractionX(Xnumerator, Xdenominator);
	cout << "Enter second fraction: ";
	scanf("%d%c%d",&Ynumerator,&slash,&Ydenominator);
	Fraction.setFractionY(Ynumerator, Ydenominator);
	cout << endl;
    system("PAUSE");

	return 0;
}
Last edited by LuciWiz : 02-May-2006 at 07:23. Reason: Please insert your C++ code between [c++] & [/c++] tags
  #2  
Old 01-May-2006, 08:48
Sokar Sokar is offline
Member
 
Join Date: May 2005
Posts: 243
Sokar has a spectacular aura aboutSokar has a spectacular aura about

Re: Fraction calculations not displaying


Quote:
Originally Posted by iceman2006
I don't know what I'm doing wrong.
One thing that is not helping is doing to much at once. Just pick one thing and get it working then move to the next. What I mean is just pick multiplication of fractions get that working then move to division ...
This might be a good start.
CPP / C++ / C Code:
#include <iostream>
#include <cmath>
#include <cstdio>


using namespace std;

class fractionType
{	
public:
    void setFractionX(int Xnumerator, int Xdenominator);
    void setFractionY(int Ynumerator, int Ydenominator);
    double multiply();
    int xnum, xden;
    int ynum, yden;
    int znum, zden; 
};

void fractionType::setFractionX(int  Xnumerator, int  Xdenominator)
{
	xnum = Xnumerator;
	xden = Xdenominator;
}
void fractionType::setFractionY(int  Ynumerator, int  Ydenominator)
{
	ynum = Ynumerator;
	yden = Ydenominator;
}
double fractionType::multiply()
{	
	znum = xnum * ynum;
	zden = xden * yden;
}

int main()
{
	fractionType Fraction;
	int Xnumerator, Xdenominator;
	int Ynumerator, Ydenominator;
	char slash;

	cout << "Enter first fraction: ";
	scanf("%d%c%d",&Xnumerator,&slash,&Xdenominator);  //is this the appropriate way to use this?
	Fraction.setFractionX(Xnumerator, Xdenominator);
	cout << "Enter second fraction: ";
	scanf("%d%c%d",&Ynumerator,&slash,&Ydenominator);
	Fraction.setFractionY(Ynumerator, Ydenominator);
	cout << endl;

	cin.ignore();
	cin.get();
	//system("PAUSE");

	return 0;
}
  #3  
Old 01-May-2006, 13:51
iceman2006 iceman2006 is offline
New Member
 
Join Date: Apr 2006
Location: Hawaii
Posts: 21
iceman2006 is on a distinguished road

Re: Fraction calculations not displaying


You're right I was trying to do too much at once. When you simplified it for me I noticed I forgot a Fraction.print() in the Main function. Or in this case Fraction.add(), Fraction.subtract(), etc. since I will print the results after each calculation with:
Code:
cout <<xnum <<"/"<<xden <<" + " <<ynum <<"/"<<yden <<" = " <<znum <<"/"<<zden << endl;
  #4  
Old 01-May-2006, 14:13
Sokar Sokar is offline
Member
 
Join Date: May 2005
Posts: 243
Sokar has a spectacular aura aboutSokar has a spectacular aura about

Re: Fraction calculations not displaying


Yes it is not as overwhelming when you break the task into little pieces.

CPP / C++ / C Code:
double fractionType::multiply()
{	
	znum = xnum * ynum;
	zden = xden * yden;
}
Did you fix this?

You might want to take a look at adding a constructor to your class. It would take the place of this.
CPP / C++ / C Code:
    void setFractionX(int Xnumerator, int Xdenominator);
    void setFractionY(int Ynumerator, int Ydenominator);
 
 

Recent GIDBlog2nd Week of IA Training by crystalattice

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
overloading operators in a class. fevershark CPP / C++ Forum 11 22-Feb-2006 19:34
fraction class program fevershark CPP / C++ Forum 7 31-Jan-2006 11:12
Interface for opening and displaying text file ladyscarlet99 MS Visual C++ / MFC Forum 0 29-Sep-2005 02:34
Non-member method needs info from class... how? Elsydeon CPP / C++ Forum 3 19-Sep-2005 15:13
opening files and displaying text pin215 CPP / C++ Forum 7 21-Feb-2004 21:27

Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 01:52.


vBulletin, Copyright © 2000 - 2008, Jelsoft Enterprises Ltd.