2013-10-03 87 views
0

我試圖從我的主體代碼中排除我的二次方程,現在它返回十六進制數字......數字非常隨機地返回,它們的值在每3-10次更改一次即使變量不變,也要運行程序。這遠遠超過了我對C++的瞭解。從c的隨機十六進制答案

我非常想回到二次方程的根在雙數據類型和基座10

最後註釋掉位只是爲了告訴你什麼是我最初做雖然大多是多餘的。

在此先感謝。

#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; 
    */ 
} 
+0

請注意隨機變化的數字是這裏的主要問題。謝謝 –

回答

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

您直接打印功能,並得到他們的地址(它們將被解釋爲函數指針在這種情況下)。而不是quadpos你想要quadpos()

+0

非常快速的答覆非常感謝。將工作,併發布,如果我卡住了。它工作:D –