我有一个代码项目,在该项目中我已经对其进行编译并运行,但似乎它只是返回一条错误消息,如
有人可以帮助我吗?
#include <iostream>
#include <iomanip>
#include <string>
#include <vector>
#include <fstream>
#include <stdlib.h>
#include <sstream>
using namespace std;
struct course
{
string CourseID;
string CourseName;
int CourseSemester;
int CourseSKS;
}c;
void printMenu_course();
void printReport_course(vector <course> x, int sem);
vector <course> readData_course(int sem);
void splitData(string temp1, string temp2[4]);
int ccounter;
int sem;
int main()
{
printMenu_course();
return 0;
}
void printMenu_course()
{
char answer;
cout << "PRINT MENU COURSE" << endl;
cout << "Which semester of course that you want to print ?" <<endl;
cin >> c.CourseSemester;
cout << "Do you want to print [Y/N] ?";
cin >>answer;
if (answer== 'Y' || answer== 'y')
{
printReport_course(readData_course(sem), c.CourseSemester);
}
else if (answer == 'n' || answer == 'N')
{
cout << "Program Terminated.";
}
else
cout << "Wrong Input.";
}
void splitData(string temp1, string temp2[4])
{
int x=0;
string mark = ",";
size_t pos = 0;
while ((pos = temp1.find(mark)) != string::npos )
{
temp2[x] = temp1.substr(0,pos);
x++;
temp1.erase(0, pos + mark.length());
} temp2[x] = temp1;
}
vector <course> readData_course(int sem)
{
vector <course> x;
ccounter = 0;
string baris1, baris2[4];
ifstream courFile;
courFile.open("course.txt", ios::in);
while(!courFile.eof())
{
courFile >> baris1;
splitData(baris1,baris2);
x.push_back(course());
x[ccounter].CourseID = baris2[0];
x[ccounter].CourseName = baris2[1];
x[ccounter].CourseSemester = ::atof(baris2[2].c_str());
x[ccounter].CourseSKS = ::atof(baris2[3].c_str());
ccounter++;
}courFile.close();
return x;
}
void printReport_course(vector<course> x, int sem)
{
int num = 0;
cout << " COURSE DATA \n";
cout << "Semester " << sem << endl;
cout << setw(5) << "No.";
cout << setw(5) << "Course ID";
cout << setw(25) << "Course Name";
cout << setw(5) << "Course SKS \n";
for(int i =0;i<ccounter;i++)
{
if(x[i].CourseSemester == sem)
{
cout << setw(5) << ++num;
cout << setw(5) << x[i].CourseID;
cout << setw(25) << x[i].CourseName;
cout << setw(5) << x[i].CourseSKS << "\n";
}
}
}
当我将向量仅更改为temp2 [3]和baris2 [3]时,它可以运行而没有任何错误,但是如果它是temp2 [4]和baris2 [4],则再次显示类似错误。
答案 0 :(得分:0)
将程序减少到演示错误所需的最低限度,并添加几个调试语句
#include <iostream>
#include <string>
using namespace std;
void splitData(string temp1, string temp2[4])
{
int x = 0;
string mark = ",";
size_t pos = 0;
while ((pos = temp1.find(mark)) != string::npos)
{
temp2[x] = temp1.substr(0, pos);
cout << temp2[x] << ": ";
x++;
temp1.erase(0, pos + mark.length());
cout << x << ": " << temp1 <<"\n";
}
if (x >= 4)
{
cout << "Accessing temp2[x] with x = " << x << " when temp2 contains 4 elements\n";
}
temp2[x] = temp1;
}
int main()
{
string temp2[4];
splitData("A,B,C,D,", temp2);
}
我们得到以下输出:
A: 1: B,C,D,
B: 2: C,D,
C: 3: D,
D: 4:
Accessing temp2[x] with x = 4 when temp2 contains 4 elements
清楚地表明x允许超出范围。为什么?计算
中的项目数"A,B,C,D,"
1 2 3 4 5
第五个元素为空。吕克·贝森(Luc Besson)不赞成。
删除输入文件中的尾部逗号。
使用vector
,以便元素的数量无关紧要。
#include <iostream>
#include <string>
#include <vector>
using namespace std;
vector<string> splitData(string temp1)
{
vector<string> tokens;
string mark = ",";
size_t pos = 0;
while ((pos = temp1.find(mark)) != string::npos)
{
tokens.push_back(temp1.substr(0, pos));
temp1.erase(0, pos + mark.length());
}
tokens.push_back(temp1);
return tokens;
}
int main()
{
vector<string> tokens = splitData("A,B,C,D,");
cout << "Number of tokens: " << tokens.size() << '\n';
for (const string & token: tokens)
{
cout << token << '\n';
}
cout << "Done.\n";
}
输出:
Number of tokens: 5
A
B
C
D
Done.
大多数变量的命名是错误的。 string temp1
包含零信息。这使调试更加困难,因为您必须不断查看匿名变量的含义以及它们应该包含的内容。不要对自己这样做。使用描述性名称。另外,如果名称足够好,代码将自行注释。