当循环/线程断点发生时

While loop / Thread breakpoint occurs

本文关键字:断点 线程 循环      更新时间:2023-10-16

我做这个程序是为了上课。以前做过几十次循环,几乎没有问题。似乎不能理解为什么有一个线程断点发生,但它是。它发生在循环之前或循环期间(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;
}

由于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());