C++ 编译错误:无法将参数1从';学生[20]';至';学生';
我编写了一部分代码,该代码应该接收一个数据文件并将数据放入一个结构数组中。我已经得到了他们各自位置的数据,但我很难找到一种方法来确定文件中的学生数量。这是我的密码:C++ 编译错误:无法将参数1从';学生[20]';至';学生';,c++,arrays,struct,C++,Arrays,Struct,我编写了一部分代码,该代码应该接收一个数据文件并将数据放入一个结构数组中。我已经得到了他们各自位置的数据,但我很难找到一种方法来确定文件中的学生数量。这是我的密码: #include <iostream> #include <string> #include <fstream> #include <iomanip> using namespace std; struct student{ string fname, lname, id, c
#include <iostream>
#include <string>
#include <fstream>
#include <iomanip>
using namespace std;
struct student{
string fname, lname, id, classname[20];
char grade[20];
int units[20], unitstaken, unitscompleted, numberofclasses;
float gpa, points[20], avg;
};
void doesitrun(ifstream& fin, string& filename);
void readit(student& temp, ifstream& fin);
int Read_List(student child, int size);
int main(){
int i = 0, size = 0;
ifstream fin;
string filename;
student u[20];
doesitrun(fin, filename);
size = Read_List(u[20], 20);
for (i; i < 3; i++){
readit(u[i], fin);
}
cout << size << endl;
cout << u[2].classname[11];
cout << u[2].grade[11] << endl;
}
void doesitrun(ifstream& fin, string& filename){
bool trueorfalse;
cout << "Enter file name along with location: ";
getline(cin, filename);
cin.ignore(20, '\n');
fin.open(filename.c_str());
if (fin.fail())
trueorfalse = false;
else
trueorfalse = true;
while (trueorfalse == false){
cout << "File does not run. Please try again. " << endl;
fin.close();
cout << "Enter file name along with location: ";
getline(cin, filename);
fin.open(filename.c_str());
if (fin.fail())
trueorfalse = false;
else
trueorfalse = true;
}
}
void readit(student& temp, ifstream& fin){
int i = 0;
getline(fin, temp.lname, ',');
cin.ignore();
getline(fin, temp.fname);
fin >> temp.id;
fin >> temp.numberofclasses;
fin.ignore(10, '\n');
for (i = 0; i < temp.numberofclasses; i++){
getline(fin, temp.classname[i]);
fin >> temp.grade[i];
fin >> temp.units[i];
fin.ignore(10, '\n');
}
}
int Read_List(student child, int size)
{
ifstream fin;
int i = 0;
readit(child, fin);
while (!fin.eof())
{
i++;
if (i >= size)
{
cout << "Array is full.\n";
break;
}
readit(child, fin);
}
fin.close();
return i;
}
我想编辑我的代码,这样我就可以使用Read_List函数来查找文件中的学生人数。在本例中,大小为3。我不确定代码
size=Read\u列表的用途(u[20],20)
在示例中,但部分u[20]
中的“20”是“u”变量数组位置的索引。因此,索引不能超过数组的大小,这意味着u[20]
中的“20”必须小于20(例如19)
将u[i]传递给函数
readit(student&temp,ifstream&fin)
的参数必须是指针(或数组)的地址,但调用readit()
似乎传递指针的值 u[20]
只是一个越界元素。我应该将其更改为什么?你[我]?u[]?u?学生u[20]
;创建一个包含20个元素的数组,这些元素可以通过索引0-19访问。代码中有很多问题。我认为你最好从一个较小的项目开始。在添加更多功能之前让它工作。@Ammar,u
如果你想通过整个过程,但这里很难说。我把它改为19,i,[],但没有一个给我数字3,这就是我想要的。我的Read_List函数可能有问题吗?@Ammar您可能需要声明一个指向struct基址的指针(例如student*stnt;stnt=new student[10],然后调用size=Read_List(stnt,20)。您还需要修改函数Read_List()获取指向结构指针而不是结构指针的地址。希望这有帮助。
Smith Jr., Joe
111-22-3333 3
Physics I
A 5
English 1A
B 4
English 1B
F 4
Jones, Bill
111-11-1111 4
Physics I
A 5
Chemistry 1A
B 5
Computer Science 1
A 4
Chemistry 1 Lab
B 1
Brown, Nancy
222-11-1111 13
Physics I
A 5
English 1A
B 4
English 1B
F 4
Physics II
A 5
Chemistry 1A
B 5
Computer Science 1
A 4
Chemistry 1A Lab
B 1
Physics I Lab
A 1
Physics II Lab
A 1
Physics III
A 5
Physics III Lab
A 1
Chemistry 1B
A 5
Chemistry 1B Lab
A 1