1

我正在处理一项任务,我们必须计算运费。出于某种原因,我在执行时遇到了无限循环。有人可以看看这个并帮助我,我无法确定原因。

代码:

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

using namespace std; 

const double basic_charge = 12; 
const double Vsurcharge = 5; 
const double Dsurcharge = 4; 
const double Wsurcharge = 2; 

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

} 
int main () 
{ 

    ifstream inFile; 
    ofstream prt("c:\\lab6a_out.txt"); 

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

    cout << "    Shipping Information\n\n"; 
    cout << "Length Width Height Weight Shipping\n"; 
    cout << "                           Cost\n\n";

    prt << "    Shipping Information\n\n"; 
    prt << "Length Width Height Weight Shipping\n"; 
    prt << "                           Cost\n\n"; 

    inFile.open("pkg6a.dat"); 
        if (!inFile) 
        cout << "Error opening the file\n"; 

    inFile >> length, width = 0, height = 0, weight = 0; 

    while (!inFile.eof())  
    { 
        shipping_cost = basic_charge; 
        volume = calculate (length, width, height); 

        if (volume > 7)  
            shipping_cost += Vsurcharge; 
        if (length > 3 || width > 3 || height > 3) 
            shipping_cost += Dsurcharge; 
        if (weight > 50) 
            shipping_cost += weight * Wsurcharge; 

        total += shipping_cost; 

        cout << setw(6) << right << setprecision(0) << length << setw(6) << right << width << setw(7) << right << height << setw(7) << right; 
        cout << weight << setw(9) << right << setprecision(2) << fixed << shipping_cost << endl;

        prt << setw(6) << right << setprecision(0) << length << setw(6) << right << width << setw(7) << right << height << setw(7) << right; 
        prt << weight << setw(9) << right << setprecision(2) << fixed << shipping_cost << endl; 

        inFile >> length, width, height, weight; 

    } 

    cout << "\nTotal cost: " << total;
    prt << "\nTotal cost: " << total; 
    //cin >> x; 
    return 0; 



} 
4

1 回答 1

5

这:

inFile >> length, width, height, weight; 

是错的。它应该是:

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

您正在使用逗号运算符,这不是您想要的。

于 2013-04-03T00:37:54.170 回答