这是我在此的头一篇博文。我是 C++ 编程的相对新手,这个错误让我很难过。
我的程序应该从几个空格分隔的文本文件中获取输入并输出一个 csv 文件。
我的代码编译得很好,但是程序崩溃了,我已经能够检索到以下错误:
正在调试的程序在从 GDB 调用的函数中发出信号。GDB 已将上下文恢复到调用前的状态。要更改此行为,请使用“set unwindonsignal off”。将放弃对包含函数 (std::string::size() const) 的表达式的评估。程序收到信号 SIGSEGV,分段错误。0x00423576 在 std::string::size() const ()
此代码的第 16 行出现错误:
#include "arrayUtils.h"
#include "enrollment.h"
#include <string>
#include <iostream>
using namespace std;
/*
* Read the course names and max enrollments. Keep the courses
* in alphabetic order by course name.
*/
void readCourses (istream& courseFile, int numCourses,
string* courseNames, int* maxEnrollments)
{
string courseNameValue = ""; // PROBLEMS START HERE
int maxEnrollmentValue = 0;
while (courseFile){
courseFile >> courseNameValue;
addInOrder(courseNames, numCourses, courseNameValue); //PROGRAM CRASHES HERE
courseFile >> ws;
courseFile >> maxEnrollmentValue;
addInOrder(maxEnrollments, numCourses, maxEnrollmentValue);
}
}
/*
* Read the enrollment requests, processing each one and tracking
* the number of students successfully enrolled into each course.
*/
void processEnrollmentRequests (istream& enrollmentRequestsFile,
int numCourses,
string* courseNames,
int* maxEnrollments,
int* enrollments)
{
// Start the enrollment counters at zero
for (int pos = 0; pos < numCourses; ++pos)
{
enrollments[pos] = 0;
}
// Read the requests, one at a time, serving each one
string courseName;
int courseIndex = 0;
enrollmentRequestsFile >> courseName;
while (enrollmentRequestsFile) {
enrollmentRequestsFile >> ws;
string studentName;
getline (enrollmentRequestsFile, studentName);
courseIndex = binarySearch(courseNames, numCourses, courseName);
if (courseIndex >= 0)
{
if (maxEnrollments[courseIndex] >= enrollments[courseIndex])
{
++enrollments[courseIndex];
cout << studentName << " has enrolled in " << courseName << "\n";
}
else
{
cout << studentName << " cannot be enrolled in " << courseName << "\n";
}
}
else
{
cout << studentName << " cannot be enrolled in " << courseName << "\n";
}
enrollmentRequestsFile >> courseName;
}
}
/*
* Write a CSV report listing each course and its enrollment.
*/
void generateReport (ostream& reportFile,
int numCourses,
string* courseNames,
int* enrollments)
{
for (int pos = 0; pos < numCourses; ++pos)
{
reportFile << "\"" << courseNames[pos] << "\"," << enrollments << "\n";
}
}
void processEnrollments (istream& courseFile, istream& enrollmentRequestsFile,
ostream& reportFile)
{
int numCourses = 0;
int arraySize = 0;
courseFile >> numCourses;
arraySize = numCourses + 1;
// Create the arrays we need
string courseNames[arraySize];
int maxEnrollments[arraySize];
int enrollments[arraySize];
// Process the enrollments
readCourses (courseFile, numCourses, courseNames, maxEnrollments);
processEnrollmentRequests (enrollmentRequestsFile, numCourses,
courseNames, maxEnrollments, enrollments);
generateReport (reportFile, numCourses, courseNames, enrollments);
}
我调用来组织字符串数组的函数是:
// Assume the elements of the array are already in order
// Find the position where value could be added to keep
// everything in order, and insert it there.
// Return the position where it was inserted
// - Assumes that we have a separate integer (size) indicating how
// many elements are in the array
// - and that the "true" size of the array is at least one larger
// than the current value of that counter
template <typename T>
int addInOrder (T* array, int& size, T value)
{
// Make room for the insertion
int toBeMoved = size - 1;
while (toBeMoved >= 0 && value < array[toBeMoved]) {
array[toBeMoved+1] = array[toBeMoved];
--toBeMoved;
}
// Insert the new value
array[toBeMoved+1] = value;
++size;
return toBeMoved+1;
}
请帮忙!我不知道该怎么做才能解决这个问题,该程序即将到期!
编辑:
主程序如下所示:
#include <cstdlib>
#include <iostream>
#include <string>
#include <fstream>
#include "enrollment.h"
using namespace std;
int main (int argc, char** argv)
{
if (argc != 4)
{
cerr << "Usage: " << argv[0] << " courseFile enrollmentFile reportFile" << endl;
return -1;
}
// Take input and output file names from the command line
ifstream coursesIn (argv[1]);
ifstream enrollmentIn (argv[2]);
ofstream reportOut (argv[3]);
processEnrollments (coursesIn, enrollmentIn, reportOut);
coursesIn.close();
enrollmentIn.close();
reportOut.close();
return 0;
}