943,937 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 1796
  • C++ RSS
Dec 1st, 2006
0

int mixed always returns huge negative number

Expand Post »
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.
Similar Threads
Reputation Points: 10
Solved Threads: 0
Light Poster
dmmckelv is offline Offline
33 posts
since Nov 2006
Dec 1st, 2006
0

Re: int mixed always returns huge negative number

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.
Reputation Points: 718
Solved Threads: 373
Nearly a Posting Maven
Lerner is offline Offline
2,253 posts
since Jul 2005

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: What is hash_map??
Next Thread in C++ Forum Timeline: I need help again





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC