int mixed always returns huge negative number

Reply

Join Date: Nov 2006
Posts: 32
Reputation: dmmckelv is an unknown quantity at this point 
Solved Threads: 0
dmmckelv dmmckelv is offline Offline
Light Poster

int mixed always returns huge negative number

 
0
  #1
Dec 1st, 2006
My mixed int is getting set to the wrong value somewhere. Can anyone help me see where?

Driver:
#include<iostream>
using std::cout;
using std::endl;
using std::cin;
#include"Rational.h"
int main()
{
int mixed1 = 0;
int numerator1 = 0;
int denominator1 = 0;
cout << "Enter first numerator and denominator to add, subtract, multiply and divide: ";
cin >> numerator1 >> denominator1;
Rational f(mixed1, numerator1, denominator1);//Create Rational object f

cout << "Enter second numerator and denominator to add, subtract, multiply and divide: ";
cin >> numerator1 >> denominator1;

Rational g(mixed1, numerator1, denominator1);//Create Rational object g
Rational a;
cout << "f: ";
f.printRational();
cout << "\ng: ";
g.printRational();
cout << "\na: ";
a.printRational();
a = f + g;
cout << "\n\na = f + g" << endl;
a.printRational();
cout << " = ";
f.printRational();
cout << " + ";
g.printRational();
a = f - g;
cout << "\n\na = f - g" << endl;
a.printRational();
cout << " = ";
f.printRational();
cout << " - ";
g.printRational();
a = f * g;
cout << "\n\na = f * g" << endl;
a.printRational();
cout << " = ";
f.printRational();
cout << " * ";
g.printRational();
a = f / g;
cout << "\n\na = f / g" << endl;
a.printRational();
cout << " = ";
f.printRational();
cout << " / ";
g.printRational();
return 0;
}

header:
#ifndef RATIONAL_H
#define RATIONAL_H
class Rational
{
public:
Rational(int =1, int =1, int = 1); // constructor
Rational operator+(const Rational & ) const;
Rational operator-(const Rational & ) const;
Rational operator*(const Rational & ) const;
Rational operator/(const Rational & ) const;
void printRational();
 
private:
int numerator;
int denominator;
int mixed;
float decimalAnswer;
};
#endif

[color=#0000ff]#include
 
functions:
<iostream>
using std::cout;
using std::cin;
using std::endl;
using std::fixed;
#include<iomanip>
using std::setprecision;
#include"Rational.h"//includes Rational class definition
Rational::Rational( int mixed, int numeratorPart, int denominatorPart) //constructor, initialize fraction array to 3, 4
{
denominator = denominatorPart;
numerator = numeratorPart;
mixed = 0;
if (denominator == 0)
{
cout << "invalid denominator please enter a number other than 0: ";
cin >> denominator;
}

else
denominator = denominatorPart;

if (denominator < 0)
{
denominator = denominator * -1;
numerator = numerator * -1;
}
while (numerator > denominator)
{
mixed++;
numerator -= denominator;
}
 

for (int i = numerator; i > 1; i--)
{
if ( numerator%i == 0 && denominator%i ==0)
{
numerator = numerator/i;
denominator = denominator/i;
}
}
}
Rational Rational::operator+(const Rational &x ) const
{

return Rational(mixed + x.mixed, numerator * x.denominator + x.numerator * denominator, 
denominator * x.denominator);
}
Rational Rational::operator-(const Rational &x) const
{
return Rational(mixed - x.mixed, numerator * x.denominator - x.numerator * denominator, 
denominator * x.denominator);
}
Rational Rational::operator*(const Rational &x) const
{
return Rational(mixed * x.mixed, numerator * x.numerator, denominator * x.denominator);
}
Rational Rational::operator /(const Rational &x) const
{
return Rational(mixed / x.mixed, numerator * x.denominator, denominator * x.numerator);
}
void Rational::printRational()
{
if (mixed == 0)
cout << numerator << "/" << denominator;
else
{
cout << mixed << " " << numerator << "/" << denominator;
}
}

Thanks for the help:lol:
Last edited by ~s.o.s~; Dec 2nd, 2006 at 1:11 pm. Reason: Fixed code tags, read the annoncements for proper use.
Reply With Quote Quick reply to this message  
Join Date: Jul 2005
Posts: 1,671
Reputation: Lerner is a name known to all Lerner is a name known to all Lerner is a name known to all Lerner is a name known to all Lerner is a name known to all Lerner is a name known to all 
Solved Threads: 261
Lerner Lerner is offline Offline
Posting Virtuoso

Re: int mixed always returns huge negative number

 
0
  #2
Dec 1st, 2006
First , thank you for using code tags.

Second, aye carumba! What a mess! Please try editing your post removing the color tags. If you didn't add the color tags manually and/or if you aren't allowed to do edit your post to remove the color tags, then paste your code into something like Note Book to remove the color tags and copy paste it from Note Book to the board.

Third, please indent your code before reposting if you don't indent routinely.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC