C++ 我不知道';我得不到正确的数据。我刚得到一个随机数
我的作业是关于数组和结构的。我需要做的第一部分是从输入文件中读取数据。然而,我被困在这一点上。我知道我得到了一些随机数。然而,我找不到它。我花了一个小时才发现那些错误。你们能告诉我哪里弄错了吗?我会很感激的。谢谢你C++ 我不知道';我得不到正确的数据。我刚得到一个随机数,c++,C++,我的作业是关于数组和结构的。我需要做的第一部分是从输入文件中读取数据。然而,我被困在这一点上。我知道我得到了一些随机数。然而,我找不到它。我花了一个小时才发现那些错误。你们能告诉我哪里弄错了吗?我会很感激的。谢谢你 #include <iostream> #include <fstream> #include <string> using namespace std; const int SIZE=15; // Here is my structure
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
const int SIZE=15;
// Here is my structure declaration.
struct BestColleges
{
int rnk; // The rank of college.
string name; //The college name.
int years; // Which years did college establish?
string email; // The address of college.
double TranferRate; // % of students who graduate and transfer
double RetentionRate; // % of students who first year retention.
int low_cost; // The lower cost of attendance
int high_cost; // The higher cose of attendance
double numStudents; // how many students in the college.
};
void getData (BestColleges c[], BestColleges *plast);
void printCollege1 (BestColleges College[],BestColleges *plast);
int main()
{
ifstream inputFile;
int num_college;
cout << " Welcome to my program!!! " << endl;
BestColleges *College;
College = new BestColleges[SIZE];
BestColleges *ptr;
BestColleges *plast = College+ SIZE-1;
ptr= College;
// Here is my function definition
getData( College, plast);
printCollege1( College, plast);
// I'm testing if I got the data from input file
//cout << num_college << endl;
cout << " Thank you for reading my program! See you again!" << endl;
return 0;
}
// Here is my fuction definition
void getData (BestColleges c[], BestColleges *plast)
{
ifstream inputFile;
int num_college;
inputFile.open("CA_BestColleges.txt");
inputFile >> num_college;
for ( BestColleges *p =c; p < plast; p++)
{
inputFile >> p->rnk;
getline(inputFile,p->name);
inputFile >> p->years;
getline(inputFile,p->email);
inputFile >> p->TranferRate;
inputFile >> p->low_cost;
inputFile >> p->high_cost;
inputFile >> p->numStudents;
}
inputFile.close();
}
void printCollege1 (BestColleges c[],BestColleges *plast)
{
for ( BestColleges *ptr = c; ptr < plast; ptr++) // Here is my printing entirely information.
{
cout << " Rank: " << ptr->rnk << endl;
cout << " Name: " << ptr->name << endl;
cout << " Email: " << ptr->email << endl;
cout << " Graduation and Transfer Rate: " << ptr->TranferRate << endl;
cout << " First Year Retention Rate: " << ptr->RetentionRate << endl;
cout << " Cost of Attendance: " << "$" << ptr->low_cost << " - " << "$" << ptr->high_cost << endl;
cout << " Number of Students: " << " approx. " << ptr->numStudents << endl;
cout << endl;
}
}
由于分析不正确,因此会得到错误的结果:
for ( BestColleges *p =c; p < plast; p++)
{
inputFile >> p->rnk;
getline(inputFile,p->name);
inputFile >> p->years;
getline(inputFile,p->email);
inputFile >> p->TranferRate;
inputFile >> p->low_cost;
inputFile >> p->high_cost;
inputFile >> p->numStudents;
}
for(BestColleges*p=c;p>p->rnk;
getline(inputFile,p->name);
输入文件>>p->年;
getline(输入文件,p->email);
输入文件>>p->Transferrate;
输入文件>>p->低成本;
输入文件>>p->高成本;
输入文件>>p->numStudents;
}
您缺少
inputFile>>p->RetentionRate代码>。因此,下一个项目将读取意外数据,并将导致糟糕的结果。您对学院数量进行了一次性读取,但它不在文件中,因此会将排名读入其中。在那之后,其余的文件就不同步了。你能举一个输入文件的例子吗?你是如何解析文本文件的?顺便说一下,这里不是问你作业问题的地方,但我建议你问一个关于作业问题的具体细节,可以帮助你完成作业。我的意思是我知道我有一些垃圾号。然而,我不知道我哪里弄错了。我只是想问问它在哪里。我不是说你需要帮我修。在你的评论中,我们找不到新词,所以我们不知道问题是否存在。您可能需要更改的另一件事是循环条件BestColleges*plast=College+SIZE-1代码>将使其在结束前停止一次。这至少可以解释上一次的糟糕结果。
for ( BestColleges *p =c; p < plast; p++)
{
inputFile >> p->rnk;
getline(inputFile,p->name);
inputFile >> p->years;
getline(inputFile,p->email);
inputFile >> p->TranferRate;
inputFile >> p->low_cost;
inputFile >> p->high_cost;
inputFile >> p->numStudents;
}