2011-04-19 4 views
2

처음으로 큰 도움을 주신 모든 분들께 감사드립니다!C++ 구조체 배열을 하나씩 전달 - 너무 단순하지만기만당했습니다.

전체 배열이 아니라 한 번에 하나의 변수를 전달하려고합니다. 나는 내가 가깝다는 것을 알고 있지만, 나는 수색하고 찾았고 내가 잘못하고있는 것을 발견 할 수 없다. 이

내가 말하는 점점 오전 오류는 말한다

무효 PrintRecords (studentRec & myRec)

함께 : 필드 'PrintRecords'에 대한 변수가 무효

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

지금보고있는 동작은 무엇입니까? 추락? 예기치 않은 행동? 컴파일러 오류? 뭐가 나왔어? 숙제 인 경우 "숙제"로 태그하십시오. –

+0

'cin >> gender;를 입력 한 후 문제가 발생합니까? (이것은'char' 변수를 제출할 때'scanf()'에서 흔히 발생하는 문제입니다.) – iammilind

+0

void PrintRecords를 사용하여 3 개의 에러를 얻었습니다 ... studentRec이이 범위에서 선언되지 않았고 myRec이이 범위에서 선언되지 않았다 , 변수 또는 필드 'PrintRecords'가 공백으로 선언되었습니다 – CodingIsAwesome

답변

3

오타 것 같다 : 올바른 기능과 같아야합니다. 도처에서 StudentRecord으로 변경하십시오.

+0

그랬어! 고맙습니다!!! – CodingIsAwesome

2

선언 프로토 타입에 쉼표가 추가되었습니다. 이 라인 :

void PrintRecords(studenRec&, myRec); 

가되어야한다

void PrintRecords(studenRec& myRec); 
+0

감사합니다. – CodingIsAwesome

2

함수

void PrintRecords(studentRec& myRec) 

는 구조 studentRec 아닌 배열 단일 참조 걸린다. 당신이 "배열의 세 가지 항목을 통해 루프와 기능 PrintRecords에 의해 각 인스턴스 하나를 통과"당신이 말하는

for (int i = 0; i < 3; i++) { 
    PrintRecords(myRec[i]); 
} 

코드를 사용할 때 그래서 때마다 PrintRecords 것은 단지 중 하나에 액세스라는 것을 의미한다 인스턴스.

그러나 실제로 printRecords 함수는 myRec이 배열 인 것처럼 객체에 대한 단일 참조 만 myRec에 액세스합니다. 당신은 struct StudenRecord {};로 선언 한

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

고맙습니다. – CodingIsAwesome

관련 문제