Compilation error: cannot convert argument 1 from 'student[20]' to 'student'
Compilation error: cannot convert argument 1 from 'student[20]' to 'student'
我写了一段代码,应该接收数据文件并将数据放入结构数组中。我已经将数据放入各自的位置,但我无法找到确定文件中学生数量的方法。这是我的代码:
#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;
}
这是我将使用的数据文件示例:
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
我想编辑我的代码,以便我可以使用 Read_List 函数来查找文件中的学生人数。在此示例中,大小将变为 3。
我不确定示例中代码 size = Read_List(u[20], 20);
的用途,但是 u[20]
部分,其中“20”是 "u" 变量数组位置的索引.因此,索引不能超过数组的大小,这意味着 u[20]
中的“20”必须小于 20(例如 19)。
并且传递给函数u[i]的参数readit(student& temp, ifstream& fin)
必须是指针(或数组)的地址但是调用readit()
似乎传递了指针的值。
我写了一段代码,应该接收数据文件并将数据放入结构数组中。我已经将数据放入各自的位置,但我无法找到确定文件中学生数量的方法。这是我的代码:
#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;
}
这是我将使用的数据文件示例:
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
我想编辑我的代码,以便我可以使用 Read_List 函数来查找文件中的学生人数。在此示例中,大小将变为 3。
我不确定示例中代码 size = Read_List(u[20], 20);
的用途,但是 u[20]
部分,其中“20”是 "u" 变量数组位置的索引.因此,索引不能超过数组的大小,这意味着 u[20]
中的“20”必须小于 20(例如 19)。
并且传递给函数u[i]的参数readit(student& temp, ifstream& fin)
必须是指针(或数组)的地址但是调用readit()
似乎传递了指针的值。