Thread: Two Struct & Class Programs that behave the same

  1. #1
    Unregistered
    Guest

    Question Two Struct & Class Programs that behave the same

    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

  2. #2
    Unregistered
    Guest

    got it..

    got it...

    I used the double colons before decaring the function.. :-)

    http://mahurshi.tripod.com/mainframes.htm

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. const elements of struct / class not accessible?
    By talz13 in forum C# Programming
    Replies: 2
    Last Post: 03-24-2006, 05:05 PM
  2. Binary Search Trees Part III
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 10-02-2004, 03:00 PM
  3. towers of hanoi problem
    By aik_21 in forum C Programming
    Replies: 1
    Last Post: 10-02-2004, 01:34 PM
  4. structure vs class
    By sana in forum C++ Programming
    Replies: 13
    Last Post: 12-02-2002, 07:18 AM
  5. Warnings, warnings, warnings?
    By spentdome in forum C Programming
    Replies: 25
    Last Post: 05-27-2002, 06:49 PM