Issue
I do not understand what is wrong but it seems like the problem lies with the vector, but after searching on the internet I could not solve this issue.
The error from the compiler looks like this:
main.cpp:25:21: error: expected primary-expression before ‘&’ token
25 | getdata(student &s,file);
| ^
main.cpp:26:23: error: expected primary-expression before ‘&’ token
26 | printdata(student &s,file);
| ^
main.cpp: In function ‘void getdata(student&, std::fstream)’:
main.cpp:36:15: error: no match for ‘operator[]’ (operand types are ‘student’ and ‘int’)
36 | cin>>s[i].name;
| ^
main.cpp:38:15: error: no match for ‘operator[]’ (operand types are ‘student’ and ‘int’)
38 | cin>>s[i].sno;
| ^
main.cpp:40:15: error: no match for ‘operator[]’ (operand types are ‘student’ and ‘int’)
40 | cin>>s[i].mark;
| ^
main.cpp:44:34: error: no match for ‘operator[]’ (operand types are ‘student’ and ‘int’)
44 | file<<"Student name: "<<s[i].name<<endl;
| ^
main.cpp:45:36: error: no match for ‘operator[]’ (operand types are ‘student’ and ‘int’)
45 | file<<"Student number: "<<s[i].snum<<endl;
| ^
main.cpp:46:33: error: no match for ‘operator[]’ (operand types are ‘student’ and ‘int’)
46 | file<<"term mark: "<<s[i].mark<<endl;
| ^
#include <iostream>
#include <fstream>
#include <vector>
using namespace std;
struct student
{
string name;
int snum;
double mark;
};
void getdata(student &s,fstream file);
void printdata(student &s,fstream file);
int main ()
{
fstream file;
int number;
vector <student> s(3);
cout<<"Enter number of students to account for: "<<endl;
cin>>number;
getdata(student &s,file);
printdata(student &s,file);
}
void getdata(student &s, fstream file)
{
file.open("Students.txt",ios::out);
for(int i=0; i<3; i++)
{
cout<<"Enter your name: "<<endl;
cin>>s[i].name;
cout<<"Enter your student number: "<<endl;
cin>>s[i].sno;
cout<<"Enter your term mark: "<<endl;
cin>>s[i].mark;
cout<<endl;
file<<"----------------------"<<endl;
file<<"Student name: "<<s[i].name<<endl;
file<<"Student number: "<<s[i].snum<<endl;
file<<"term mark: "<<s[i].mark<<endl;
file<<"----------------------"<<endl;
}
file.close();
}
void printdata(student &s, fstream file)
{
file.open("Students.txt",ios::in);
string read;
while(getline(cin,read))
{
cout<<read<<endl;
}
file.close();
}
Solution
These are not valid function calls:
getdata(student &s,file);
printdata(student &s,file);
They should be:
getdata(s,file);
printdata(s,file);
That will cause other errors because your functions clearly expect a vector<student>
, but only take a student
.
You also cannot pass fstream
objects by value. They must be references.
Change your function parameter lists to look like this:
void getdata(vector<student> &s, fstream &file);
void printdata(vector<student> &s, fstream &file);
There’s also a typo with the member .sno
which you meant .snum
Fix all this and your program will compile, then you can at least start debugging it to see if it even works.
Answered By – paddy
Answer Checked By – David Goodson (BugsFixing Volunteer)