Gabriel Brodowski
Gabriel Brodowski

Reputation: 33

random hexadecimal answers from c

I was trying to exclude my quadratic equations from my main body of code and now it's returning hexadecimal numbers... the numbers are returned quite randomly, they change in value about once every 3-10 times you run the program even though the variables are constant. This far surpasses my knowledge of C++.

I'd ideally like to return the roots of the quadratic equation in the double data type and base 10.

The last commented out bit is just to show you what I've initially done though is mostly redundant.

Thanks in advance.

#include <iostream>
#include <cmath>
using namespace std;


double a,b,c,bsq;

double quadpos() {

    double quadpos = (-b+(sqrt((bsq)-4.0*a*c)))/(2.0*a);

    double a=1.0,b=2.0,c=1.0;
    double bsq = pow(2.0,2);

    return quadpos;
}

double quadneg() {

    double quadneg = (-b-(sqrt((bsq)-4.0*a*c)))/(2.0*a);
    return quadneg;
}


int main() {
    // 4a)
    cout << /*" - Question 4 - \n\n" << "Part A\n" << */quadpos << "\n" << quadneg << std::endl;

    /*float a = 5.0, b = 7.0, c = -8.0;
    int bsq;
    bsq = pow(b,2);
    double quadpos = (-b+(sqrt((bsq)-4.0*a*c)))/(2.0*a), quadneg = (-b-(sqrt((bsq)-4.0*a*c)))/(2.0*a);

    cout << a << "\n" << b << "\n" << c << "\n" << bsq << "\n" << quadpos << "\n" << quadneg << endl;
    The above line is used for troubleshooting

    cout << "The roots of the quadratic equation 5x^2 + 7x - 8 are " << quadpos << " and " << quadneg << ".\n" << "The roots are the values of the x variable when the equation, 5x^2 + 7x - 8 is equal to 0.\n" << endl;
    */
}

Upvotes: 0

Views: 304

Answers (1)

cnicutar
cnicutar

Reputation: 182684

cout << /* ... */ quadpos << "\n" << quadneg << std::endl;
                       ^^^^ ?

You are printing functions directly and getting their addresses (they are interpreted as function pointers in this context). Instead of quadpos you want quadpos().

Upvotes: 1

Related Questions