2016-12-15 111 views
0

?我被困在我們需要使用函數頭創建二次公式程序的地方,並通過值編號和引用傳遞。一切似乎都正確的計算,但它沒有輸出任何東西到我正在指導它的輸出文件。任何建議傢伙?程序沒有輸出到文件? C++

#include<iostream> 
#include<fstream> 
#include<cmath> 
#include<string> 
#include<iomanip> 
using namespace std; 


void GetInputs(ifstream&in, double &a, double &b, double &c); 
int Quadroots(double a, double b, double c, double &r1, double &r2); 
void Print(ofstream &out, double a, double b, double c, double r1, double r2, int EquationKind); 
ifstream in; 
ofstream out; 


void GetInputs(ifstream &in, double &a, double &b, double &c) 
{ 
    in >> a >> b >> c; 

} 

int Quadroots (double a, double b, double c, double& r1, double& r2) 
{ 
    double radical; 
    if (a==0) 
    { 
     return -1; 
    } 

    radical = b*b-4*a*c; 

    if (radical < 0) 
    { 
     return -2; 
    } 

    else 
    { r1 = (-b + sqrt(radical))/2*a; 
     r2 = (-b - sqrt(radical))/2*a; 

     return 0; 
    } 
} 
void Print(ofstream& out, double a, double b, double c, double r1, double r2, int EquationKind) 
{ 

     out << "Solving roots for Quadratic equations (ax^2+bx+c)"<< endl; 
     out << "a "<< "b " << "c   " << "Root1   "<< "Root2   "<< "message" << endl; 
     out << "-----------------------------------------------------------------------------------------------" << endl; 
     out << a << "  "<< b << "  "<< c << endl; 


     if (r1 != 10000.0 & r2 != 10000.0) 
     out << r1 <<"  " << r2 << "  " << "Two Real roots." << endl; 
     if (r1!=10000.0 || r2 !=10000.0) 
     out <<r1 <<"    " << "One real roots" << endl; 
     if (a==0) 
     out << "       " << "It is a line"<< endl; 
     if (EquationKind== -2) 
     out << "       " << "No real solution" << endl; 
} 



int main() 
{ 

    int Quadroot1, Quadroot2, EquationKind; 
    double a, b, c, r1=10000.0, r2=10000.0; 

    in.open("input.txt"); 
    if (!in) 
    { 
     out << "error opening file"; 
     return -1; 
    } 
    out.open("output.txt"); 
    if (!out) 
    { 
     out << "Output file cannot be created. Program ends" << endl; 
     in.close(); 
     return -1; 

GetInputs(in, a, b, c); 

    while(!in.eof()) 
    { 

     EquationKind = Quadroots(a, b, c, r1, r2); 
     Print(out, a, b, c, r1, r2, EquationKind); 
     GetInputs(in, a, b, c); 
    } 

Print(out, a, b, c, r1, r2, EquationKind); 

out.close(); 

return 0; 


}} 
+0

if條件未正確關閉。它應該在返回-1之後結束。如果(!out) out <<「輸出文件不能被創建,程序結束<< << endl; in.close(); return -1; } –

回答

0

您沒有正確關閉if(!out)條件。將其更改爲:

if (!out) 
{ 
    out << "Output file cannot be created. Program ends" << endl; 
    in.close(); 
    return -1; 
} 

您將要刪除主函數末尾的最後一個大括號。

+1

非常感謝你,感謝你的幫助KTing! – mikeyboy6952

0

通過格式化您的代碼(我使用了clang格式,還有其他工具,但我離題了),您的錯誤變得更加清晰。

您的代碼:

int main() {              
    int Quadroot1, Quadroot2, EquationKind;       
    double a, b, c, r1 = 10000.0, r2 = 10000.0;      

    in.open("input.txt");           
    if (!in) {              
    out << "error opening file";         
    return -1;             
    }                
    out.open("output.txt");           
    if (!out) {              
    out << "Output file cannot be created. Program ends" << endl; 
    in.close();             
    return -1;             

    GetInputs(in, a, b, c);          

    while (!in.eof()) {           
     EquationKind = Quadroots(a, b, c, r1, r2);     
     Print(out, a, b, c, r1, r2, EquationKind);     
     GetInputs(in, a, b, c);          
    }                

    Print(out, a, b, c, r1, r2, EquationKind);     

    out.close();             

    return 0;              
    }                
} 

的最終原因(至少我假設沒有其他錯誤!)放錯地方的支柱。固定在main收益:(!出)

int main() {              
    int Quadroot1, Quadroot2, EquationKind;       
    double a, b, c, r1 = 10000.0, r2 = 10000.0;      

    in.open("input.txt");           
    if (!in) {              
    out << "error opening file";         
    return -1;             
    }                
    out.open("output.txt");           
    if (!out) {              
    out << "Output file cannot be created. Program ends" << endl; 
    in.close();             
    return -1;             
    }                

    GetInputs(in, a, b, c);           

    while (!in.eof()) {            
    EquationKind = Quadroots(a, b, c, r1, r2);     
    Print(out, a, b, c, r1, r2, EquationKind);     
    GetInputs(in, a, b, c);          
    }                

    Print(out, a, b, c, r1, r2, EquationKind);      

    out.close();             

    return 0;              
}                 
+0

如果您感謝某人的幫助,最好接受他們的答案。無論是我的答案還是KTing的答案都可以接受,並且都可以投票。 – druckermanly