I was just trying to play with structs and classes to see the difference..
I wrote two programs that behave the same... the struct one doesn't work when I am trying to call the function..
how do i fix it ?
here're the two programs...
PROGRAM WITH CLASS:
Code:
#include <iostream.h>
#include <stdio.h>
class person
{
public:
char name[20];
int age;
void getdata();
};
void person :: getdata()
{
cout << "Enter name: ";
cin >> name;
cout << "Enter age: ";
cin >> age;
}
main()
{
int i;
person record[2];
for (i=0; i<=1; i++)
{
record[i].getdata();
}
for (i=0; i<=1; i++)
{
cout << record[i].name << endl << record[i].age << endl << endl;
}
getchar();
}
PROGRAM WITH STRUCT (this one doesn't work)
Code:
#include <iostream.h>
#include <stdio.h>
struct person
{
char name[20];
int age;
void getdata();
};
void getdata()
{
char name[20];
int age;
cout << "Enter name: ";
cin >> name;
cout << "Enter age: ";
cin >> age;
}
main()
{
int i;
person record[2];
for (i=0; i<=1; i++)
{
record[i].getdata() ;
}
for (i=0; i<=1; i++)
{
cout << record[i].name << endl << record[i].age << endl << endl ;
}
getchar();
getchar();
}
Can anyone tell me how I fix it ?
http://mahurshi.tripod.com/mainframes.htm