Problem with Pointers and Classes

Does anyone have any idea why this code doesn't work.
It takes in the data, but it only spits out the number 0 in the table.
I'm not sure why it's happening.



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
46
#include <iostream>
using namespace std;

class company
{
	public:
		void getData();
		void printData();
	public:
		char emp_name[20];
		int emp_id;
		char city[20];		
};

void company::getData()
{
	cout<<"\tEnter Name: "; cin>>emp_name;
	cout<<"\tEnter ID# : "; cin>>emp_id;
	cout<<"\tEnter city: "; cin>>city; 
}

void company::printData()
{
	
	cout<<emp_id<<"\t"<<emp_name<<"\t"<<city<<endl;
}

int main()
{

	company pMicrosoft = new company;
	for(int i=0;i<3;i++)
	{
		pMicrosoft->getData();
		pMicrosoft++; /
	}
	cout<<"Employee ID\tEmployee Name\tCity"<<endl;
        cout<<"===================================================="<<endl;
	for(int j=0;j<3;j++)
	{
		pMicrosoft->printData();
		pMicrosoft++;  	
	}
	cin.get();
	return 0;			
}
You declared pMicrosoft as an object but you are using it as a pointer
It shouldn't even compile
Last edited on
oh sorry - that was my bad. while i was pasting the code - i was removing some of the comments and i accidently removed that line, rewrote it and forgot to put the * in.
Line 31 reads
company *pMicrosoft = new company ;

Still doesn't work.
Why pMicrosoft++ ? pMicrosoft is not a pointer to an array
In any case you aren't resetting its location after the input loop
And you need to delete everything you new
OH SHOOT - IT WAS SUPPOSED TO BE AN ARRAY!!!! HOW THE HECK DID I miss that!!
Thanks alot, mate!
Topic archived. No new replies allowed.