-1

我正在为一个班级做一个项目。该代码根据从文件中读取的数据计算运费。我遇到的问题是在计算中。从音量函数传递的值保持为零。我认为我没有正确传递它。此外,不需要使用全局变量——尤其是使用计算函数——使用参数是我从教授那里得到的最后反馈。

这是代码:

#include <iostream> 
#include <iomanip> 
#include <cmath> 
#include <fstream> 


using namespace std;  


void header(int lab_number, char lab_part)
{ 
    cout << "Kevin Schultz\n"; 
    cout << "Lab" << lab_number << lab_part << endl << endl; 
} 


double length, width, height, x, weight, total = 0, shipping_cost, volume;




int main () 
{ 
    double calculate(double length, double width, double height);
    double volume;
    double chargeAmount;
    double charge(ifstream & inFile, ofstream & prt);


    ifstream inFile; 
    ofstream prt("lab7new_out.txt"); 

    header(7, 'A'); 
    prt << "        S & S Global Services\n";
    prt << "    Shipping Cost Analysis Report\n\n"; 
    prt << "Length Width Height Weight Shipping\n"; 
    prt << "                             Cost\n\n"; 

    inFile.open("c:\\lab7\\pkg.txt"); 
        if (!inFile) 
        cout << "Error opening the file\n"; 

    inFile >> length; 
    inFile >> width; 
    inFile >> height; 
    inFile >> weight; 


    volume = calculate(length, width, height);      
    chargeAmount = charge(inFile, prt);



    prt << "------------------------------------" << endl;
    prt << "\nTotal cost: $" << total; 

    system("pause"); 
    return 0; 

} 

double calculate(double length, double width, double height)
        { 
            double volume; 
            volume = length * width * height; 
            return volume; 
        } 

double charge(ifstream & inFile, ofstream & prt) 
        {
            const double basic_charge = 12;
            const double Vsurcharge = 5;
            const double Dsurcharge = 4;
            const double Wsurcharge = 2;
            double netWeight = 0;

            while (!inFile.eof())  
            { 
                shipping_cost = basic_charge; 

                if (volume > 7)  
                    shipping_cost += Vsurcharge; 
                if (length > 3 || width > 3 || height > 3) 
                    shipping_cost += Dsurcharge; 
                if (weight > 50) 
                {
                    netWeight = weight - 50;
                    shipping_cost += netWeight * Wsurcharge; 
                }
                total += shipping_cost; 
                prt << setw(4) << right << setprecision(1) << length << setw(6) << right << setprecision(1) << width << setw(6) << right << setprecision(1) << height << setw(8) << right; 
                prt << weight << setw(5) << right << setprecision(2) << fixed << "$" << shipping_cost << endl; 

                inFile >> length; 
                inFile >> width; 
                inFile >> height; 
                inFile >> weight; 
            }
             return total;
        }

任何帮助都会很棒!

4

2 回答 2

2

正如您的教授所说,请不要使用全局变量。在您的代码中,您有不同的变量volume,我怀疑您是否知道何时访问哪些变量。让我们浏览一下您的代码:

 double volume;    // global variable, never written, read in function charge

 int main() {
    ...
    double volume; // local variable, only visible in function main, but never used
    ...
 }

 double calculate(double length, double width, double height) {
    double volume; // local variable, only visible in calculate
    volume = ...;
    return volume;
 }

 double charge(ifstream & inFile, ofstream & prt) {
    ...
    if(volume > 7) // no local variable volume defined, so using global variable volume
    ...
 }

此外,您声明了一个函数计算,但您从不调用它。

请考虑启用尽可能多的编译器警告。它将帮助您找到错误。

于 2013-04-17T15:01:28.517 回答
0

尝试这样的事情。问题是你需要在调用它之前声明你的函数原型,但是如果你把你的主函数放在你的 .c 文件的底部,你就不需要贴花原型函数,希望它很清楚

#include <iostream> 
#include <iomanip> 
#include <cmath> 
#include <fstream> 


using namespace std;  


void header(int lab_number, char lab_part)
{ 
    cout << "Kevin Schultz\n"; 
    cout << "Lab" << lab_number << lab_part << endl << endl; 
} 


double length, width, height, x, weight, total = 0, shipping_cost, volume;

double calculate(double length, double width, double height)
{ 
    double volume; 
    volume = length * width * height; 
    return volume; 
} 

double charge(ifstream & inFile, ofstream & prt) 
{
            const double basic_charge = 12;
            const double Vsurcharge = 5;
            const double Dsurcharge = 4;
            const double Wsurcharge = 2;
            double netWeight = 0;

            while (!inFile.eof())  
            { 
                shipping_cost = basic_charge; 

                if (volume > 7)  
                    shipping_cost += Vsurcharge; 
                if (length > 3 || width > 3 || height > 3) 
                    shipping_cost += Dsurcharge; 
                if (weight > 50) 
                {
                    netWeight = weight - 50;
                    shipping_cost += netWeight * Wsurcharge; 
                }
                total += shipping_cost; 
                prt << setw(4) << right << setprecision(1) << length << setw(6) << right << setprecision(1) << width << setw(6) << right << setprecision(1) << height << setw(8) << right; 
                prt << weight << setw(5) << right << setprecision(2) << fixed << "$" << shipping_cost << endl; 

                inFile >> length; 
                inFile >> width; 
                inFile >> height; 
                inFile >> weight; 
            }
     return total;
}

int main () 
{ 

    double volume = calculate(length, width, height);
    double chargeAmount;


    ifstream inFile; 
    ofstream prt("lab7new_out.txt"); 

    header(7, 'A'); 
    prt << "        S & S Global Services\n";
    prt << "    Shipping Cost Analysis Report\n\n"; 
    prt << "Length Width Height Weight Shipping\n"; 
    prt << "                             Cost\n\n"; 

    inFile.open("c:\\lab7\\pkg.txt"); 
        if (!inFile) 
        cout << "Error opening the file\n"; 

    inFile >> length; 
    inFile >> width; 
    inFile >> height; 
    inFile >> weight; 

    chargeAmount = charge(inFile, prt);


    prt << "------------------------------------" << endl;
    prt << "\nTotal cost: $" << total; 

    system("pause"); 
    return 0; 

} 
于 2013-04-17T14:48:47.333 回答