发生循环/线程断点

时间:2014-05-15 14:52:37

标签: c++ multithreading while-loop infinite-loop do-while

我正在上课这个课程。之前完成了几十次循环,几乎没有问题。似乎无法理解为什么会发生线程断点,但确实如此。它在循环之前/之前或之前发生(readFile函数)。这对我来说似乎很好,因为我在另一个类中使用了类似的方法。这可能是什么问题?

这是我到目前为止所拥有的

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

1 个答案:

答案 0 :(得分:0)

由于EOF测试有缺陷,您正在射击固定大小的阵列。你的循环看起来应该如下所示:

// 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;
};

// 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());