| | |
int mixed always returns huge negative number
Please support our C++ advertiser: Intel Parallel Studio Home
![]() |
•
•
Join Date: Nov 2006
Posts: 32
Reputation:
Solved Threads: 0
My mixed int is getting set to the wrong value somewhere. Can anyone help me see where?
Driver:
header:
Thanks for the help:lol:
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.
•
•
Join Date: Jul 2005
Posts: 1,678
Reputation:
Solved Threads: 263
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.
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.
![]() |
Similar Threads
- Counter changing to a negative number (C++)
- Character's Unicode Value (Java)
- figuring out PK/FK (MS SQL)
- How do i print int num1; a 5 digit number like 1 2 3 4 5 (Java)
- Need help displaying when user enters negative number? (C++)
- hvving problems displaying after user inters negative number? (C++)
- prime numbers (C++)
Other Threads in the C++ Forum
- Previous Thread: What is hash_map??
- Next Thread: I need help again
| Thread Tools | Search this Thread |
api array based binary c++ c/c++ calculator char char* class classes code coding compile console conversion count database delete deploy desktop developer directshow dll download dynamic dynamiccharacterarray email encryption error file forms fstream function functions game givemetehcodez google graph gui homeworkhelp iamthwee ifstream input int integer java lib linkedlist linker linux list loop looping loops map math matrix memory multiple news number numbertoword output pointer problem program programming project python random read recursion recursive reference return rpg sorting string strings struct temperature template templates test text text-file tree unix url variable vector video visual visualstudio win32 windows winsock wordfrequency wxwidgets






