开发者

C++ Passing an Array of Struct one by one - so simple but I'm stumpted [closed]

This question is unlikely to help any future visitors; it is only relevant to a small geographic area, a specific moment in time, or an extraordinarily narrow situation that is not generally applicable to the worldwide audience of the internet. For help making this question more broadly applic开发者_StackOverflow社区able, visit the help center. Closed 9 years ago.

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;
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜