简体   繁体   中英

Converting Object int data members to floating point and dividing appends strange data cout to console

I'm sure I'm doing something wrong, but I just can't figure it out. I've created an object with integer data members, and I want to have a member function return the quotient of it's members as a floating point value, which it does. It then appends some additional stuff. The output is below the program, which should run as is.

#include <iostream>
#include <string>
#include <cstdlib>
using namespace std;

class Rational
{
public:
    explicit Rational(int = 0, int = 1);
    double getRationalAsDouble() const;
private:
    int numerator;
    int denominator;
};

Rational::Rational(int numerator, int denominator)
{
    if (denominator == 0)
        this->denominator = 1;
    else
        this->denominator = denominator;
    this->numerator = numerator;
}

// *******  Problem Function *********
double Rational::getRationalAsDouble() const
{
    double a = 0.0, b = 0.0;
    a = static_cast<double>(numerator);
    b = static_cast<double>(denominator);
    cout << endl << "a = " << a;
    cout << endl << "b = " << b;
    cout << endl << "a/b = " << (a/b);
}
// ******** End Problem Function ********

int main()
{

    { //Create a new Scope so that I can view Destructor Message, not used here

        Rational c(2, 6);
        int data = 10;

        cout << c.getRationalAsDouble(); // prints rational object c as double, but not really
        cout << "\n\n";
            } // End of Scope
    return 0;
} // end main

And here's the output:

a = 2
b = 6
a/b = 0.3333332.31196e-317

I've been playing around, and if I change the function to have any regular division in it, it works fine. What's really interesting is if I add any output after the cout << endl << "a/b = " << (a/b); line, that output is handled before (a/b) part of the line. Any help would be greatly appreciated! Thank you in advance for your time.

Solution: The function wasn't returning anything. When the code was changed to:

double Rational::getRationalAsDouble()
{
    return static_cast<double>(numerator)/denominator;
}

It worked as expected. Thank you tc.

Three problems:

  • You want to print endl at the end of the line, not the "beginning". Your code ends up doing (effectively) cout << endl << "a/b = " << (a/b); ... cout << c.getRationalAsDouble(); cout << "\\n\\n"; cout << endl << "a/b = " << (a/b); ... cout << c.getRationalAsDouble(); cout << "\\n\\n"; which prints the two doubles 0.333333 and 2.31196e-317 next to each other with no space.
  • You want (perhaps) cout << "\\n" << endl instead of cout << "\\n\\n" . endl causes the stream to be flushed; plain "\\n" might not.
  • Rational::getRationalAsDouble() is not returning a value. Listen to your compiler warnings.

The fix looks something like

double Rational::getRationalAsDouble() const
{
  double a = 0.0, b = 0.0;
  a = static_cast<double>(numerator);
  b = static_cast<double>(denominator);
  cout << "a = " << a << endl;
  cout << "b = " << b << endl;
  cout << "a/b = " << (a/b) << endl;
  return a/b;
}

Your implementation of Rational::getRationalAsDouble() can be simplified to:

double Rational::getRationalAsDouble() const
{
   return 1.0*numerator/denominator;
}

I think you had everything else there for debugging purposes, and hence are not really needed.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM