当循环/线程断点发生时
While loop / Thread breakpoint occurs
我做这个程序是为了上课。以前做过几十次循环,几乎没有问题。似乎不能理解为什么有一个线程断点发生,但它是。它发生在循环之前或循环期间(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());
相关文章:
- 从不同线程使用int64的不同字节安全吗
- 删除一个线程上有数百万个字符串的大型哈希映射会影响另一个线程的性能
- 在C++中使用cURL和多线程
- 为什么我的C#代码在调用回C++COM直到Task时会暂停.等待/线程.加入
- 在cuda线程之间共享大量常量数据
- 如何将元素添加到数组的线程安全函数?
- 线程,如果else语句,都是错误的上下文切换后,会发生什么
- C++Boost Asio Pool线程,带有lambda函数和传递引用变量
- Qt C++静态thread_local QNetworkAccessManager是线程应用程序的好选择吗
- 异常属于C++中的线程还是进程
- C++中的线程安全删除
- C++使用params创建线程函数会导致转换错误
- 类与私有变量的其他类之间的线程安全性
- CoInitialize()在单独的线程上崩溃而不返回
- c++中的线程池
- 实现在多线程代码中安全恢复的断点
- Eclipse不会在断点处停止,并将线程状态置于Running: User Request
- 多线程C程序中assert on的断点
- 当循环/线程断点发生时
- 在数组赋值处的线程断点