正在比较标头记录值和读取程序崩溃的行数.C++
Comparing header record value with number of lines read crashes program. C++
我正在尝试读取文件中的行数,以获得记录数,并将其与文件指示的头记录数进行比较。readInput
函数在返回true时工作,但当删除文件中的行以使函数返回false时,它会使程序崩溃。
#include <iostream>
#include <string>
#include <fstream>
using namespace std;
struct Info
{
string name;
string idNum;
int testNum;
int* tests;
int average;
char grade;
};
int headRec(ifstream&);
Info* allocate(int);
bool readInput(Info* , ifstream& , int);
void displayData(Info* , int);
void deallocate(Info* , int);
int main()
{
int record;
string filename;
Info* data;
cout << "Please enter the input file name --> ";
cin >> filename;
ifstream inputFile(filename.c_str());
while (!inputFile)
{
cout << filename << " cannot be found. Input another file name" << endl;
cout << "Please enter the input file name --> ";
cin >> filename;
inputFile.clear();
inputFile.open(filename.c_str());
}
record = headRec(inputFile);
if (record == 0)
{
cout << "Error. Program Ending";
exit(0);
}
data = allocate(record);
if (data == nullptr)
{
cout << "Memory Allocation Error. Program Ending";
exit(0);
}
if (readInput(data, inputFile, record) == false)
{
cout << "Program Ending";
}
displayData(data, record);
deallocate(data, record);
cout << "nPress Enter to end --> ";
cin.ignore();
cin.ignore();
return 0;
}
int headRec(ifstream& inputFile)
{
int record = 0;
inputFile >> record;
if (record < 5)
{
return record = 0;
}
return record;
}
Info* allocate(int record)
{
Info* data = new Info[record];
return data;
}
bool readInput(Info* data, ifstream& inputFile, int record)
{
int total = 0, end = 0;
char c;
for (int i = 0; i < record; i++)
{
inputFile >> data[i].name;
inputFile >> data[i].idNum;
inputFile >> data[i].testNum;
data[i].tests = new int[data[i].testNum];
for (int j = 0; j < data[i].testNum; j++)
{
inputFile >> data[i].tests[j];
total += data[i].tests[j];
}
inputFile.get(c);
data[i].average = total / data[i].testNum;
total = 0;
if (data[i].average < 60)
data[i].grade = 'F';
else if (data[i].average <= 70)
data[i].grade = 'D';
else if (data[i].average <= 80)
data[i].grade = 'C';
else if (data[i].average <= 90)
data[i].grade = 'B';
else
data[i].grade = 'A';
if (c == 'n')
end++;
}
cout << end;
cout << record;
if (end == record)
return true;
else
return false;
}
void displayData(Info* data, int record)
{
for (int i = 0; i < record; i++)
{
cout << "nName: " << data[i].name;
cout << "tID Number: " << data[i].idNum << endl;
cout << "Test Scores: ";
for (int j = 0; j < data[i].testNum; j++)
{
cout << data[i].tests[j] << " ";
}
cout << endl;
cout << "Average: " << data[i].average;
cout << "tGrade: " << data[i].grade << endl;
}
}
void deallocate(Info* data, int record)
{
for (int i = 0; i < record; i++)
delete[] data[i].tests;
delete[] data;
}
这就是文件包含的内容:
10
Mark 2345A78 6 78 67 98 34 78 69
Emily 3246B59 5 89 56 74 59 98
Yao 8942A85 7 98 75 69 89 85 78 95
Claudette 6587C69 5 56 45 63 75 60
Niklos 5874B58 6 69 68 52 69 75 74
Channah 4587B32 7 98 95 100 96 89 96 94
Parissa 8524C84 5 98 96 85 91 90
Rodrick 9856C65 5 56 75 84 65 75
Chance 5648A67 6 100 89 96 89 82 86
Melissa 8786C78 5 80 63 75 77 72
Niels 4854A78 6 48 56 85 65 42 75
您永远不会在readInput()中的读取循环中检查'eof'。这意味着在点击eof:之后,这些行有垃圾值(可能为0)
inputFile >> data[i].name;
inputFile >> data[i].idNum;
inputFile >> data[i].testNum;
那么,如果data[i].testNum为零,您认为这里会发生什么?
data[i].average = total / data[i].testNum;
您可以考虑在每次读取流之前检查inputFile.good()
,并在返回false时对其进行适当处理。
相关文章:
- 提升 ASIO Async_receive崩溃程序
- 类指针方法崩溃程序
- get_body来自 IHTMLDocument2 崩溃程序
- 是否有可能存在不会崩溃程序的内存问题
- 调试运行时出现奇怪的崩溃程序(Eclipse C++)
- 儿童对话框 - setWindowTexta或sendmessagea崩溃程序-MFC
- 顶点阵列GLFW崩溃C 程序
- 将值分配给Float数据类型崩溃程序
- OpenCV-将变量添加到类成员崩溃程序
- boost::interprocess::managed_shared_memory 崩溃程序
- 将枚举值分配给整数崩溃程序
- SDL 1.2 -> SDL 2.0(崩溃程序)
- 空析构函数崩溃程序:C++
- 冒泡排序崩溃程序c++
- Ofstream关闭崩溃程序
- 删除字符* 崩溃程序
- c++矢量擦除崩溃程序
- Lua_getglobal崩溃程序
- 通过Copy Constructor创建的e2打印崩溃程序
- RNG崩溃c++程序