help with structures please!!!
Feb 17, 2012 at 12:49am UTC
So I am trying to make a simple structure which will read and write the members of a family but I am getting this errors and I dont seem to get where the mistake is :(
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45
#include <iostream>
#include <string>
#include <new>
using namespace std;
struct data {
int nr;
string fname;
string lname;
};
void create(data& a, int n) {
int i;
for (i=0;i<n;i++) {
cout <<"Number: " ;
cin >>a[i].nr;
cout<<endl;
cin.ignore();
cout <<"First Name: " ;
getline(cin,a[i].fname);
cout <<"Last Name: " ;
getline(cin,a[i].lname);
}
}
void show(data a, int n) {
int i;
for (i=0;i<n;i++) {
cout <<"\nNumber: " <<a[i].nr<<endl;
cout <<"First Name: " <<a[i].fname<<endl;
cout <<"Last name: " <<a[i].lname<<endl;
cout <<"_______________________________" <<endl;
}
}
int main () {
int n,i;
cout <<"Number of persons in family: " ;
cin >>n;
data *a=new (nothrow) data[n];
if (a==0) cout <<"Error, please try again" ;
else {
create(a,n);
show(a,n);
}
delete [] a;
system("pause" );
return 0;
}
it says "In function `void create(data&, int)
no match found for operator[] in a'[i]'
and other errors :(
Please help!
Feb 17, 2012 at 1:41am UTC
Seeing as you are passing a pointer to an array of class objects to the create/show functions that is what they need to accept as their parameters.
Changing the functions to (data* a, int n) {
should solve the problem.
Feb 17, 2012 at 2:26am UTC
damn it worked, THANKS MAN!
its always the small mistakes that ruin a big program :P
Topic archived. No new replies allowed.