Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/multithreading/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 当循环/线程断点出现时_C++_Multithreading_While Loop_Infinite Loop_Do While - Fatal编程技术网

C++ 当循环/线程断点出现时

C++ 当循环/线程断点出现时,c++,multithreading,while-loop,infinite-loop,do-while,C++,Multithreading,While Loop,Infinite Loop,Do While,我在课堂上做这个节目。以前做过几十次循环,几乎没有问题。似乎无法理解为什么会出现线程断点,但事实确实如此。它发生在循环之前或循环过程中(readFile函数)。这对我来说似乎很好,因为我在另一个类中使用了类似的方法。这个可能有什么问题 这是我到目前为止所拥有的 #include <iostream> #include <fstream> #include <cctype> #include <cmath> #include <string&g

我在课堂上做这个节目。以前做过几十次循环,几乎没有问题。似乎无法理解为什么会出现线程断点,但事实确实如此。它发生在循环之前或循环过程中(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;
}
#包括
#包括
#包括
#包括
#包括
#包括
#包括
使用名称空间std;
_FILE=“USAGE.txt”中的常量字符串;
常数int MAX=1000;
const char LOW_PLAN='L';
const char MID_PLAN='M';
const char HIGH_PLAN='H';
const char UNLIMITED='U';
枚举phoneStats{LOW,HIGH,AVG};
//原型
无效读取文件(ifstream&usageFile、双低收费[]、双中收费[]、双高收费[]、整数和低计数、整数和中计数、整数和高计数、字符和植物类型、双重和电话收费);
int main(int argc,const char*argv[]
{
字符串电话号码;
半植物型;
双重电话费;
char ch;
双低电荷[最大值];
双中电荷[最大值];
双高电荷[最大值];
int lowCount=0;
整数中数=0;
int高计数=0;
双下限=0;
双低最高=0;
双低平均值=0;
双中下=0;
双倍中高=0;
双midAvg=0;
双高最低=0;
双高=0;
双高平均值=0;
电话计费;
做
{
readFile(usageFile、lowCharge、midCharge、highCharge、lowCount、midCount、highCount、planType、phoneCharge);
ch=usageFile.peek();
用法文件>>电话号码>>植物类型>>电话费用;

cout由于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;
};
//另外,不要执行前面的“usageFile>>…”
while(true){
//您还应该推送尽可能多的charge/count变量声明
//尽可能在这里。。。
//“readFile”在这里是一个糟糕的名字,因为它实际上并没有
readFile(usageFile、lowCharge、midCharge、highCharge、lowCount、,
中数、高数、planType、phoneCharge);
用法文件>>电话号码>>植物类型>>电话费用;
if(usageFile.eof()){
打破
}

好吧,那太酷了。只要你不知道他在做什么,我就修正了你以前的无界循环。你也应该考虑使用<代码> STD::向量< /代码>这些商店。我不认为我可以在我的班上休息。而且,这个解决方案在我的程序中仍然不起作用。具体什么更具体些?它不起作用。我只是复制粘贴了你的代码,得到了一个segfault,然后进行了编辑并验证了结果。无论我尝试了什么,无论是你的解决方案还是我以前的解决方案。在readFile函数上仍然会出现线程1:breakpoint 1.1。如果这很重要,我将使用Xcode。如果你遇到的断点是all?禁用它。这听起来不像是一个代码问题,你在原来的帖子中确实遇到过。这是你需要的吗?:但是我的教授是否仍能识别代码是否正常工作?或者他是否也必须禁用断点?
// 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());