C++ C++;一个接一个地传递结构数组-非常简单,但我';我被难住了
首先,向所有帮助我的人表示衷心的感谢 我试图在这里一次传递一个变量,而不是整个数组。我知道我很接近,但我已经找了又找,找不出我做错了什么 有一个错误,我得到它说是与 作废打印记录(studentRec和myRec) 它说:“PrintRecords”字段的变量声明为voidC++ C++;一个接一个地传递结构数组-非常简单,但我';我被难住了,c++,arrays,data-structures,C++,Arrays,Data Structures,首先,向所有帮助我的人表示衷心的感谢 我试图在这里一次传递一个变量,而不是整个数组。我知道我很接近,但我已经找了又找,找不出我做错了什么 有一个错误,我得到它说是与 作废打印记录(studentRec和myRec) 它说:“PrintRecords”字段的变量声明为void #include <iostream> using namespace std; struct studenRec { string name; string address; string cit
#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;
}
#包括
使用名称空间std;
结构studenRec{
字符串名;
字符串地址;
字符串城市;
字符串状态;
拉链;
性别;
字符串myid;
浮动gpa;
};
作废打印记录(studenRec和myRec);
int main()
{
studenRec-myRec[3];
对于(int i=0;i<3;i++){
cout您的原型有一个额外的逗号。这一行:
void PrintRecords(studenRec&, myRec);
应该是:
void PrintRecords(studenRec& myRec);
你的职能
void PrintRecords(studentRec& myRec)
只引用结构studentRec,而不是数组
for (int i = 0; i < 3; i++) {
PrintRecords(myRec[i]);
}
for(int i=0;i<3;i++){
打印记录(myRec[i]);
}
您说的是“遍历数组中的所有三个项,并将每个实例逐个传递到函数PrintRecords”,这意味着每当调用PrintRecords时,它只访问其中一个实例
但是,在函数printRecords中,您访问myRec时就像它是一个数组,而实际上它只是对恰好位于数组中的对象的单个引用。正确的函数应该如下所示:
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;
}
void打印记录(studentRec和myRec)
{
您可以声明为struct StudenRecord{};
这似乎是一个打字错误。将所有位置更改为StudentRecord
您看到的行为是什么?崩溃?意外行为?编译器错误?打印出什么?也请标记为“家庭作业”如果这是家庭作业。在输入cin>>性别;
后,您是否面临任何问题?(这是提交一些char
变量时scanf()的常见问题。)我发现3个错误都与void PrintRecords有关…它说studentRec未在此范围内声明,myRec未在此范围内声明,变量或字段“PrintRecords”已声明为void