Question

I am doing this program for class. Have done loops tens of times before with little to no problem. Cannot seem to understand why there is a thread breakpoint occuring but it is. It's occuring right before/ or right during the loop (readFile function). It seems perfectly fine to me, as I have used a similar method in another class. What could possible be the problem with this one?

Here is what I have so far

#include <iostream>
#include <fstream>
#include <cctype>
#include <cmath>
#include <string>
#include <iomanip>
#include <sstream>

using namespace std;

const string IN_FILE = "USAGE.txt";

const int MAX = 1000;

const char LOW_PLAN = 'L';
const char MID_PLAN = 'M';
const char HIGH_PLAN = 'H';
const char UNLIMITED = 'U';

enum phoneStats { LOW, HIGH, AVG };

// Prototypes
void readFile(ifstream& usageFile, double lowCharge[], double midCharge[], double    highCharge[], int& lowCount, int& midCount, int& highCount, char& planType, double& phoneCharge);

int main(int argc, const char * argv[])
{
string phoneNumber;
char planType;
double phoneCharge;

char ch;

double lowCharge[MAX];
double midCharge[MAX];
double highCharge[MAX];

int lowCount = 0;
int midCount = 0;
int highCount = 0;

double lowLowest = 0;
double lowHighest = 0;
double lowAvg = 0;

double midLowest = 0;
double midHighest = 0;
double midAvg = 0;

double highLowest = 0;
double highHighest = 0;
double highAvg = 0;

cout << "Description" << endl << endl;

ifstream usageFile;

usageFile.open(IN_FILE.c_str());

if (usageFile) {
    cout << "File opened" << endl;

    usageFile >> phoneNumber >> planType >> phoneCharge;

    do
    {

        readFile(usageFile, lowCharge, midCharge, highCharge, lowCount, midCount, highCount, planType, phoneCharge);
        ch = usageFile.peek();


        usageFile >> phoneNumber >> planType >> phoneCharge;


        cout << phoneNumber << planType << phoneCharge << endl;
    } while (ch != usageFile.eof());

}
else
{
    cout << "File " << IN_FILE << " could not be opened." << endl;
    return 1;
}


return 0;
}


void readFile(ifstream& usageFile, double lowCharge[], double midCharge[], double  highCharge[], int& lowCount, int& midCount, int& highCount, char& planType, double& phoneCharge)
{
if (planType == LOW_PLAN )
{
    lowCharge[lowCount] = phoneCharge;
    lowCount++;
}
else if (planType == MID_PLAN)
{
    midCharge[midCount] = phoneCharge;
    midCount++;
}
else if (planType == HIGH_PLAN)
{
    highCharge[highCount] = phoneCharge;
    highCount++;
}
else
{
    return;
}


return;
}
Was it helpful?

Solution

You are shooting past your fixed-sized arrays due to a defective EOF test. Your loop should look more like the following:

// also, don't do preceding "usageFile >> ... ;"

while (true) {
  // you should also push as many of your charge/count variable declaration
  // in here as possible ...

  // "readFile" is a poor name here, since it actually doesn't
  readFile(usageFile, lowCharge, midCharge, highCharge, lowCount,
    midCount, highCount, planType, phoneCharge);

  usageFile >> phoneNumber >> planType >> phoneCharge;

  if (usageFile.eof()) {
    break;
  }

  cout << phoneNumber << " " << planType << " " << phoneCharge << endl;
};

or

// If you can't use `break` due to your teacher's rules ...
// Note that this is worse style since it uses the hacky "firstTime" check.
bool firstTime = true;
do {
  if (!firstTime) {
    cout << phoneNumber << " " << planType << " " << phoneCharge << endl;
  }
  firstTime = false;

  readFile(usageFile, lowCharge, midCharge, highCharge, lowCount,
    midCount, highCount, planType, phoneCharge);

  usageFile >> phoneNumber >> planType >> phoneCharge;
} while (!usageFile.eof());
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top