-1

Wrote this C++ code in Visual Studios 2012 and it's only the first step of the assignment. However, when trying to run it gives me .exe has stopped working. I am not sure why this is the case because I have used that loop before. Any idea why this is happening?

Here are a few lines from the file being read from.

AA11 11AA Lee Caleb 1 1.01 2 2.01 3 5.01 01012000 1 01102000 P

ZZ33 33ZZ Wolfe Mitch 5 1.01 1 2.01 0 5.01 03051999 1 01112002 M

WW44 44WW Zeitouni Elie 10 1.01 5 2.01 10 5.01 05052012 0 05052013 M

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


struct record
{
    string custID, SPID, custLN, custFN;
    int Q1;
    double P1;
    int Q2;
    double P2;
    int Q3;
    double P3;
    string LOD;
    bool shipRec;
    string NCD, preMethod;
    double totalSales;
};


istream& operator >> (istream& in, record& r)
{
    in >> r.custID >> r.SPID >> r.custLN >> r.custFN >> r.Q1 >> r.P1 >>r.Q2 >> r.P2
       >> r.Q3 >> r.P3 >> r.LOD >> r.shipRec >> r.NCD >> r.preMethod;

    return in;
}

int main()
{
    ifstream inMaster;
    ifstream inTrans;
    inMaster.open("master.txt");
    inTrans.open("trans.txt");
    ofstream outNewM;
    ofstream outErrorL;
    outNewM.open("NewMaster.txt");
    outErrorL.open("errorLog.txt");

    record customer[100];
    int i=0;

    while (!inMaster.eof())
    {
        inMaster >> customer[i];
        customer[i].totalSales = customer[i].Q1 * customer[i].P1 + customer[i].Q2 * customer[i].P2 + customer[i].Q3 * customer[i].P3;
        i++;
    }

    inMaster.close();
    inTrans.close();
    outNewM.close();
    outErrorL.close();
    return 0;
}
  • 2
    If your inMaster file has more than 100 customer records in it, then you are going to write past the end of your customer[100] array and probably crash... – Jeremy Friesner Nov 22 '13 at 06:12
  • It only has 20 records in it – user3020474 Nov 22 '13 at 06:52
  • 2
    VS 2012 has a half decent debugger you can use to step through the program and at least narrow down where the problem is. And `while (!inMaster.eof())` is incorrect. read more about the "EOF anti-pattern" here: http://stackoverflow.com/questions/5431941 and http://drpaulcarter.com/cs/common-c-errors.php#4.2 – Michael Burr Nov 22 '13 at 07:42
  • What did you discover when you debugged the program? – Raymond Chen Nov 22 '13 at 08:10
  • Add [this question and answer](http://stackoverflow.com/questions/5605125/why-is-iostreameof-inside-a-loop-condition-considered-wrong) to the C++ side of why `stream.eof()` is a terrible loop-condition. – WhozCraig Nov 22 '13 at 08:15
  • I'm not very proficient in programming, our instructor just told us to use that method of the while loop. When debugging, it loads garbage into customer and the loop never ends. – user3020474 Nov 22 '13 at 15:19
  • Okay I think it may have been something with the master file I was reading in from. It now puts the correct information into the array including the calculated total sales. However, I can not output any of it and will get "debug error R6010". – user3020474 Nov 22 '13 at 16:15

1 Answers1

0

The problem is that you have an error reading a record at some point. When that happens the stream sets the 'failbit' to indicate it's in an error state and will not perform any further operations. The eof test still indicates the stream isn't at the end, but when you try to read nothing happens because the stream is in a failure state. So you keep looping because no data is being read form the stream after that point.

After performing the input, add something like:

if (inMaster.fail()) { 
    cerr << "Error reading line " << i+1 << endl; 
    return 1; 
}
Michael Burr
  • 311,791
  • 49
  • 497
  • 724