Dear all,

I have a prog working and printing to screen. But as my program has grown i.e class/method definitions I need to tidy up my main();

my problem is with printing my results to the screen.

///this will print out to screen perfectly. note i left out the class/method definitions as there are too many.

ANY IDEA WHY MY FUNCTIONS BELOW WILL NOT PRINT OUT DETAILS OF ARRAY TO SCREEN.

int main(int argc, char *argv[])
{
  doctor* Array[5];
  doctor* newdoctor;
  int choice;
  int i,age,hrs;
  string names;
    
  
  for(int i=0;i<3;i++)
  {
  cout<<"1.doctor, 2.Junior, 3.Consultant"<<endl;
  cin>>choice;
   if (choice==1)
      newdoctor = new doctor();
  else if (choice==2)
      newdoctor = new junior();
  else
      newdoctor = new consultant();
      
  cout<<"Enter doctor name"<<endl;
  cin>>names;
  newdoctor->set_Name(names);
  cout<<"Enter doctor Age"<<endl;
  cin>>age;
  newdoctor->set_Age(age);
  cout<<"Enter no of hrs worked"<<endl;
  cin>>hrs;
  newdoctor->set_Hours(hrs);
    
  Array[i]=newdoctor;
  
  }
  
  cout<<"\n";
  
  for(i=0;i<3;i++)
  {
  cout<<"Name: "<<Array[i]->get_Name();
  cout<<"\n";
  cout<<"Age: "<<Array[i]->get_Age();
  cout<<"\n";
  cout<<"Holiday Bonus: ";
  Array[i]->get_holidaypay();
  cout<<"\n";
  cout<<"Total: "<<Array[i]->calculate_pay();
  cout<<"\n";
  cout<<"PayRate: "<<Array[i]->get_Payrate();
  cout<<"\n";
  delete Array[i];
  }
  
  
  system("PAUSE");	
  return 0;
}

HOWEVER


//when I try to clean-up main() & call my fuctions it will not print the details of the Array. before I started cleaning the prog the whole lot worked perfectly.

Do I need to pass the doctor Array[] to the void Print_Doctor_Details(); and if i do should void Enter_Doctor_Details(); pass back the array so i can pass it onto void Print_Doctor_Details();

void Enter_Doctor_Details();
void  Print_Doctor_Details();

int main(int argc, char *argv[])
{
  doctor* Array[5];
  
  
  Enter_Doctor_Details(); //this function works
  
  Print_Doctor_Details(); //having trouble getting this to work
  
  
  
  system("PAUSE");	
  return 0;
}

void Enter_Doctor_Details()
{

doctor* newdoctor;
int choice;
string doctorname;
int i,doctorage,hrs;
doctor* Array[5];

for(int i=0;i<2;i++)
  {
      cout<<"1.doctor, 2.Junior, 3.Consultant"<<endl;
      cin>>choice;
               
               if (choice==1)
                   newdoctor = new doctor();
               else if (choice==2)
                   newdoctor = new junior();
               else
                   newdoctor = new consultant();
      
  
        cout<<"Enter doctor name"<<endl;
        cin>>doctorname;
        newdoctor->set_Name(doctorname);
        cout<<"Enter doctor Age"<<endl;
        cin>>doctorage;
        newdoctor->set_Age(doctorage);
        cout<<"Enter no of hrs worked"<<endl;
        cin>>hrs;
        newdoctor->set_Hours(hrs);
  
  
    
        Array[i]=newdoctor;
  
  }
}





void Print_Doctor_Details()
{

doctor* Array[5];
 
  
  for(int i=0;i<2;i++)
  {
  cout<<"Name: "<<Array[i]->get_Name();
  cout<<"\n";
  cout<<"Age: "<<Array[i]->get_Age();
  cout<<"\n";
  cout<<"Holiday Bonus: ";
  Array[i]->get_holidaypay();
  cout<<"\n";
  cout<<"Total: "<<Array[i]->calculate_pay();
  cout<<"\n";
  cout<<"PayRate: "<<Array[i]->get_Payrate();
  cout<<"\n";
  delete Array[i];
  }

}

<< moderator edit: removed garish colors and added [co[u][/u]de][/co[u][/u]de] tags >>

doctor* Array[5];
 
  
  for(int i=0;i<2;i++)

Array isn't initialized. And if it was, you're only looping through two of its five elements.

Do I need to pass the doctor Array[] to the void Print_Doctor_Details();

Yes, unless you want a global variable.

doctor* Array[5];
 
  
  for(int i=0;i<2;i++)

Array isn't initialized. And if it was, you're only looping through two of its five elements.

i thought the Array was initalised (doctor* Array[5];) Is that not an initalised array?. As for looping through the 2 of 5 elements that is ok, i was just trying to get anything to print to screen.

can you get this code to complie?

#include <iostream>
#include <stdlib.h>
#include <string.h>
using namespace std;

void Enter_Doctor_Details();
void  Print_Doctor_Details();

class doctor{

    protected:
    string newname;
    string address; 
    int payrate; 
    int hours;
    int age;   
       
    public:
    
    doctor();   //constructor
    virtual ~doctor();
    virtual void set_Name(string itsname);  
	virtual string get_Name();  
	
	virtual void set_Address(string itsaddress);
    virtual string get_Address();    
	
    virtual void set_Age(int itsage);  
    virtual int get_Age();   
	
	virtual void set_Hours(int itshours);  
	virtual int get_Hours();  
	
    //virtual void set_Payrate();
    virtual int get_Payrate();  
    
    virtual void get_holidaypay();
    virtual int calculate_pay();
    
    };          //end of class declaration
	         
    doctor::doctor()
    {    
    } 
    
    doctor::~doctor()
    {
    cout<<"Doctor Destructer called"<<endl;
    }                                         
                                                                                                                       
	void doctor::set_Name(string itsname){
    newname=itsname;
     }
	string doctor::get_Name(){
    return newname;
    }
	
	void doctor::set_Address(string itsaddress){
    address=itsaddress;
    }
    
	string doctor::get_Address(){
    return address;
    }
    
	void doctor::set_Age(int itsage){
    age=itsage;
    }
    
	int doctor::get_Age(){
    return age; 
	}
	
	void doctor::set_Hours(int itshours){
    hours=itshours;
    }
    
	int doctor::get_Hours(){
    return hours;
    }
	
	//void doctor::set_Payrate(int itspay){
    //payrate=itspay;
    //}
    
	int doctor::get_Payrate(){
    return payrate=100;
    }
	
    void doctor::get_holidaypay(){
    cout<<"You Have weekend off "<<endl;
    }
    
    int doctor::calculate_pay()
	{
	return (get_Payrate()*hours);
	}
	
	
	//Junior Doctor Class 
	
    class junior: virtual public doctor
    {
    public:
    junior(){}
    virtual ~junior();
    virtual void get_holidaypay();
    virtual int get_Payrate();
    
      
    }; 
    
    void junior::get_holidaypay()
    {
    cout<<"You Have 2months off "<<endl;
    }
    
    int junior::get_Payrate(){
    return payrate=200;
    }
    
    junior::~junior()
    {
    cout<<"junior destroctor called"<<endl;
    }
    
   
    //Consultant Class
    class consultant:virtual public doctor
    {
    public:
    virtual void get_holidaypay();
    consultant(){}  
    virtual ~consultant();
    virtual int get_Payrate();
                      
    };                   
   
   consultant::~consultant()
   {
   cout<<"Consultant Destructor"<<endl;
   }
   
   int consultant::get_Payrate(){
   return payrate=600;
   }
   
   void consultant::get_holidaypay()
   {
   cout<<"You Have 6 months off "<<endl;
   }
   


int main(int argc, char *argv[])
{
  doctor* Array[5];
  
  
  Enter_Doctor_Details(); //this function works
  
  Print_Doctor_Details(); //having trouble getting this to work
  
  
  
  system("PAUSE");	
  return 0;
}

void Enter_Doctor_Details()
{

doctor* newdoctor;
int choice;
string doctorname;
int i,doctorage,hrs;
doctor* Array[5];

for(int i=0;i<2;i++)
  {
      cout<<"1.doctor, 2.Junior, 3.Consultant"<<endl;
      cin>>choice;
               
               if (choice==1)
                   newdoctor = new doctor();
               else if (choice==2)
                   newdoctor = new junior();
               else
                   newdoctor = new consultant();
      
  
        cout<<"Enter doctor name"<<endl;
        cin>>doctorname;
        newdoctor->set_Name(doctorname);
        cout<<"Enter doctor Age"<<endl;
        cin>>doctorage;
        newdoctor->set_Age(doctorage);
        cout<<"Enter no of hrs worked"<<endl;
        cin>>hrs;
        newdoctor->set_Hours(hrs);
  
  
    
        Array[i]=newdoctor;
  
  }
}





void Print_Doctor_Details()
{

doctor* Array[5];
 
  
  for(int i=0;i<2;i++)
  {
  cout<<"Name: "<<Array[i]->get_Name();
  cout<<"\n";
  cout<<"Age: "<<Array[i]->get_Age();
  cout<<"\n";
  cout<<"Holiday Bonus: ";
  Array[i]->get_holidaypay();
  cout<<"\n";
  cout<<"Total: "<<Array[i]->calculate_pay();
  cout<<"\n";
  cout<<"PayRate: "<<Array[i]->get_Payrate();
  cout<<"\n";
  delete Array[i];
  }

}

If you want to do the detail entering and the detail printing seperately, you should define a doctor array inside the main function, and pass that function name to the two functions you are using ( Enter_Doctor_Details(), and Print_Doctor_Details() ). Better if you look up for, global scope, local scope, and passing variables to functions in your C++ book.
I have done some correcting and posted it here.

#include <iostream>
#include <stdlib.h>
#include <string.h>
using namespace std;


class doctor
{

protected:
string newname;
string address;
int payrate;
int hours;
int age;

public:

doctor(); //constructor
virtual ~doctor();
virtual void set_Name(string itsname);
virtual string get_Name();

virtual void set_Address(string itsaddress);
virtual string get_Address();

virtual void set_Age(int itsage);
virtual int get_Age();

virtual void set_Hours(int itshours);
virtual int get_Hours();

//virtual void set_Payrate();
virtual int get_Payrate();

virtual void get_holidaypay();
virtual int calculate_pay();

}; //end of class declaration

doctor::doctor()
{
}

doctor::~doctor()
{
cout<<"Doctor Destructer called"<<endl;
}

void doctor::set_Name(string itsname){
newname=itsname;
}
string doctor::get_Name(){
return newname;
}

void doctor::set_Address(string itsaddress){
address=itsaddress;
}

string doctor::get_Address(){
return address;
}

void doctor::set_Age(int itsage){
age=itsage;
}

int doctor::get_Age(){
return age;
}

void doctor::set_Hours(int itshours){
hours=itshours;
}

int doctor::get_Hours(){
return hours;
}

//void doctor::set_Payrate(int itspay){
//payrate=itspay;
//}

int doctor::get_Payrate(){
return payrate=100;
}

void doctor::get_holidaypay(){
cout<<"You Have weekend off "<<endl;
}

int doctor::calculate_pay()
{
	return (get_Payrate()*hours);
}


//Junior Doctor Class

class junior: virtual public doctor
{
	public:
	junior(){}
	virtual ~junior();
	virtual void get_holidaypay();
	virtual int get_Payrate();


	};

	void junior::get_holidaypay()
	{
	cout<<"You Have 2months off "<<endl;
	}

	int junior::get_Payrate(){
	return payrate=200;
	}

	junior::~junior()
	{
	cout<<"junior destroctor called"<<endl;
}


//Consultant Class
class consultant:virtual public doctor
{
	public:
	virtual void get_holidaypay();
	consultant(){}
	virtual ~consultant();
	virtual int get_Payrate();

	};

	consultant::~consultant()
	{
	cout<<"Consultant Destructor"<<endl;
	}

	int consultant::get_Payrate(){
	return payrate=600;
	}

	void consultant::get_holidaypay()
	{
	cout<<"You Have 6 months off "<<endl;
}

void Enter_Doctor_Details( doctor* Array[ 5 ]);
void Print_Doctor_Details(doctor* Array[ 5 ]);


int main(int argc, char *argv[])
{
	doctor* Array[5];


	Enter_Doctor_Details(Array); //this function works

	Print_Doctor_Details(Array); //having trouble getting this to work



	system("PAUSE");
	return 0;
}

void Enter_Doctor_Details( doctor* Array[ 5 ])
{

	doctor* newdoctor;
	int choice;
	string doctorname;
	int i,doctorage,hrs;
	//doctor* Array[5];

	for(int i=0;i<2;i++)
	{
		cout<<"1.doctor, 2.Junior, 3.Consultant"<<endl;
		cin>>choice;

		if (choice==1)
			newdoctor = new doctor();
		else if (choice==2)
			newdoctor = new junior();
		else
			newdoctor = new consultant();


		cout<<"Enter doctor name"<<endl;
		cin >> doctorname;
		newdoctor->set_Name(doctorname);
		cout<<"Enter doctor Age"<<endl;
		cin>>doctorage;
		newdoctor->set_Age(doctorage);
		cout<<"Enter no of hrs worked"<<endl;
		cin>>hrs;
		newdoctor->set_Hours(hrs);
		Array[i]=newdoctor;

	}
}





void Print_Doctor_Details(doctor* Array[ 5 ])
{
	//doctor* Array[5];
	for(int i=0;i<2;i++)
	{
		cout<<"Name: "<<Array[i]->get_Name();
		cout<<"\n";
		cout<<"Age: "<<Array[i]->get_Age();
		cout<<"\n";
		cout<<"Holiday Bonus: ";
		Array[i]->get_holidaypay();
		cout<<"\n";
		cout<<"Total: "<<Array[i]->calculate_pay();
		cout<<"\n";
		cout<<"PayRate: "<<Array[i]->get_Payrate();
		cout<<"\n";
		delete Array[i];
	}
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.