C++ Passing an Array of Struct one by one - so simple but I'm stumpted [closed]
First a big THANK YOU to everyone that helps!
I am trying to pass one variable at a time here and not the whole array. I know I'm close, but I've searched and searched and can't find out what I'm doing wrong.
There error I am getting it says is with
void PrintRecords(studentRec& myRec)
it says: Variable for field 'PrintRecords' declared void
#include <iostream>
using namespace std;
struct studenRec {
string name;
string address;
string city;
string state;
string zip;
char gender;
string myid;
float gpa;
};
void PrintRecords(studenRec& myRec);
int main()
{
studenRec myRec[3];
for (int i = 0; i < 3; i++) {
cout << "Students name: ";
getline(cin, myRec[i].name);
cout << "Students address: ";
getline(cin, myRec[i].address);
cout << "Students city: ";
getline(cin, myRec[i].city);
cout << "Students state: ";
getline(cin, myRec[i].state);
cout << "Students zip: ";
getline(cin, myRec[i].zip);
cout << "Students gender: ";
cin >> myRec[i].gender;
cin.ignore(1000,10);
cout << "Students id: ";
getline(cin, myRec[i].myid);
cout << "Students gpa: ";
cin >> myRec[i].gpa;
cin.ignore(1000,10);
cout << "STUDENT RECORDED SUCCESSFULLY" << endl;
cout << endl;
}
cout << "-------------------------------" << endl;
for (int i = 0; i < 3; i++) {
PrintRecords(myRec[i]);
}
return 0;
} // main
void PrintRecords(studentRec& myRec)
{
cout << "Students name: " << myRec.name << endl;
cout << "Students address: " << myRec.address << endl;
cout << "Students city: " << myRec.city << endl;
cout << "Students state: " << myRec.state << endl;
cout << "Students zip: " << myRec.zip << endl;
cout << "Students gender: " << myRec.gender << endl;
cout << "Students id: " << myRec.myid << endl;
cout << "Students gpa: " << myRec.gpa << endl;
cout << endl;
}
You have declared as struct StudenRecord {};
It seems to be a typo. Change everywhere to StudentRecord
.
Your prototype has an extra comma. This line:
void PrintRecords(studenRec&, myRec);
Should be:
void PrintRecords(studenRec& myRec);
Your function
void PrintRecords(studentRec& myRec)
takes a single reference to the structure studentRec, not an array. So when you use the code
for (int i = 0; i < 3; i++) {
PrintRecords(myRec[i]);
}
You are saying "Loop through all three items in the array and pass each instance one by one into the function PrintRecords" which means that whenever PrintRecords is called it is only accessing one of the instances.
However in the function printRecords you are accessing myRec as if it's an array when actually it is only a single reference to an object that happens to be in an array. The correct function should look like:
void PrintRecords(studentRec& myRec)
{
cout << "Students name: " << myRec.name << endl;
cout << "Students address: " << myRec.address << endl;
cout << "Students city: " << myRec.city << endl;
cout << "Students state: " << myRec.state << endl;
cout << "Students zip: " << myRec.zip << endl;
cout << "Students gender: " << myRec.gender << endl;
cout << "Students id: " << myRec.myid << endl;
cout << "Students gpa: " << myRec.gpa << endl;
cout << endl;
}
精彩评论