1

I'm working on an assignment where we have to calculate shipping costs. For some reason, I am getting an infinite loop upon execution. Can some take a look at this and help me out, I cant determine why.

Code:

#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; 



} 
Kevin Schultz
  • 866
  • 3
  • 14
  • 42
  • 7
    Asking people to spot errors in your code is not especially productive. You should use the debugger (or add print statements) to isolate the problem, by tracing the progress of your program, and comparing it to what you expect to happen. As soon as the two diverge, then you've found your problem. (And then if necessary, you should construct a [minimal test-case](http://sscce.org).) – Oliver Charlesworth Apr 03 '13 at 00:33
  • 4
    You can reach an impasse where the next character on the input isn't valid for `length` (for example, a letter) but the stream is not at EOF either (because the next character is still a letter that could perfectly well be read if only you weren't looking for a number). This leads to an infinite loop in your code. You should check that the input operation itself succeeded. – Jonathan Leffler Apr 03 '13 at 00:34
  • @JonathanLeffler - Would that include a decimal? Such as a dimension of 2.5? – Kevin Schultz Apr 03 '13 at 00:37
  • Always, always, always with the totally broken I/O logic... :-( – Kerrek SB Apr 03 '13 at 00:37
  • 2
    Also, I'm pretty sure that this `inFile >> length, width, height, weight;` doesn't do what you want, which is probably `inFile >> length >> width >> height >> weight;`. – Jonathan Leffler Apr 03 '13 at 00:37
  • I can't see what's in your data file. In C, I'd be using `fgets()` and `sscanf()` to convert the string so I could see what was causing errors. In C++, maybe using `cin.getline()` and string input would allow you to see what's going wrong. You could try reading the next character with plain character input and report what is causing trouble. – Jonathan Leffler Apr 03 '13 at 00:40
  • Your [previous question](http://stackoverflow.com/q/15713707/1037210) looks somewhat similar, regarding the problem in question. – Lion Apr 03 '13 at 00:41
  • You should fire up a debugger or add cout commands to trace the execution of your program and view values of any important variables. This will help you track down where the problem could be. – Code-Apprentice Apr 03 '13 at 00:41
  • @Lion - Similia issue but I re-wrote the code trying to fix the issues. – Kevin Schultz Apr 03 '13 at 00:43
  • @JonathanLeffler: you mean `std::getline`, I suppose? :-) – Kerrek SB Apr 03 '13 at 00:43
  • Note that `cerr` exists for reporting errors. You check whether the open succeeded, and report if it didn't, but you still try to use it even when you know it failed. – Jonathan Leffler Apr 03 '13 at 00:43
  • [Must read](http://stackoverflow.com/questions/5605125/why-is-iostreameof-inside-a-loop-condition-considered-wrong). – Alexey Frunze Apr 03 '13 at 00:44
  • @Code-Guru - Tried that too, it errors on opening the file, but I know that the path is correct. – Kevin Schultz Apr 03 '13 at 00:44
  • @KerrekSB: probably — I don't do enough C++ programming to have it remembered, but I know there's a `getline()` operation, and I'd seriously consider using it. – Jonathan Leffler Apr 03 '13 at 00:45
  • @KevinSchultz What is the error and which line of code causes it? – Code-Apprentice Apr 03 '13 at 00:52
  • 1
    possible duplicate of ["while( !feof( file ) )" is always wrong](http://stackoverflow.com/questions/5431941/while-feof-file-is-always-wrong) – user93353 Apr 03 '13 at 03:08

1 Answers1

5

This:

inFile >> length, width, height, weight; 

Is wrong. It should be:

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

You're using the comma operator, which is not what you want.

mfontanini
  • 20,082
  • 3
  • 56
  • 73