-6

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;
}
Bubz21
  • 15
  • 1
  • 4
  • Okay, that's cool. Just down-vote the noob because he doesn't know what he's doing. – Bubz21 May 15 '14 at 15:21
  • I just fixed your formerly unbounded loop. You should also consider using `std::vector` for these stores. – Jeff May 15 '14 at 15:24

1 Answers1

0

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());
Jeff
  • 3,475
  • 4
  • 26
  • 35
  • I don't think I'm allowed to break during a loop for my class. Also, that solution still isn't working in my program. – Bubz21 May 15 '14 at 15:31
  • be more specific about what's not working, please. I just copy-pasted your code, got a segfault, then made my edit and verified the results. – Jeff May 15 '14 at 15:38
  • No matter what I try, whether it's your solution or one of my previous solutions. There continues to be a Thread 1: breakpoint 1.1 occurring right at the readFile function. I'm using Xcode if that matters – Bubz21 May 15 '14 at 15:41
  • You're hitting a breakpoint is all? Disable it. That doesn't sound like a code issue, which you actually do have in your original post. Is this what you need?: http://stackoverflow.com/questions/3900308/deleting-breakpoints-in-xcode?lq=1 – Jeff May 15 '14 at 15:42
  • But will my professor still recognize the code as working? Or will he have to disable breakpoints too? – Bubz21 May 15 '14 at 15:47
  • The breakpoint is a setting particular to your Xcode project, not the .cpp code itself. If you are just turning in the .cpp, you have nothing to worry about. If you are turning in the whole project, just save it after you disable the breakpoint. – Jeff May 15 '14 at 15:48
  • Ahh, I see. Okay, thank you. You have been very helpful. Is there any link that explains why exactly the breakpoints occur? And why are they needed? – Bubz21 May 15 '14 at 15:52
  • You probably just clicked on the left-hand margin of your code editing window by accident. As I said, breakpoints are settings/tools in a development environment (e.g., Xcode) for developing and testing programs and are completely separate from the code itself. If you build a binary, save it somewhere else and run it, the breakpoints are long since gone. They only exist when you choose to Debug in your IDE. – Jeff May 15 '14 at 15:52
  • Please also upvote/accept this answer if it helped you; I'm trying to build reputation for question bounties. Thanks! – Jeff May 15 '14 at 16:03
  • I can't, everyone downvoted me for being a noob so my reputation is too low -_- – Bubz21 May 15 '14 at 16:15
  • So it goes. In the future, try to understand more clearly what your problem is before you ask, and how various broader concepts interact. Thinking more analytically and being more self-sufficient are crucial to being a successful software developer, more-so than homework in intro CS classes. – Jeff May 15 '14 at 16:19