Dear All,

If i run the code below it will let me input a name. "John Doe" but it will only print out "John".

I know how to create a char array[] and I also know how to use cin.getline but can the char array be used with the doctor methods below so I can print out the full name "John Doe" when I call cout<<Array->get_Name();


#include <iostream>
#include <stdlib.h>

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

class doctor
{

protected:
string newname;


public:

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


}; //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;
}

	
  

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

doctor* newdoctor;
newdoctor = new doctor();
string doctorname;
	
cout<<"Enter doctor name"<<endl;
cin >> doctorname;


newdoctor->set_Name(doctorname);
	
Array[i]=newdoctor;

cout<<Array[i]->get_Name();


  system("PAUSE");	
  return 0;
}

Recommended Answers

All 7 Replies

See The Changes...cin terminates with the first white character...don't mix C-headers with C++ one's....and why were you including iostream twice?

#include <iostream>
#include <cstdlib>
#include <string>
using namespace std;

class doctor
{

protected:
string newname;
public:
doctor(); //constructor
virtual ~doctor();
virtual void set_Name(string itsname);
virtual string get_Name();
}; //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;
}

int main(int argc, char *argv[])
{

int i=0;
doctor* Array[5];
doctor* newdoctor;

newdoctor = new doctor();
//string doctorname;
char doctorname[20];
	
cout<<"Enter doctor name"<<endl;
cin.getline(doctorname,20,'\n');

newdoctor->set_Name(doctorname);
	
Array[i]=newdoctor;

cout<<Array[i]->get_Name();

  return 0;
}

Also, anything you create with 'new' needs to be 'deleted'

Thanks for the solution.

As for "and why were you including iostream twice?" the answer to that is I had my methods and class in another header file and I just copied them all into one main and forgot to delete it.

Oh the dilemma, I checked the forum earlier and found the solution with the help of sunnypalsingh. I implemented the solution and it worked perfectly. However when I checked back later winbatch advised me to use "delete" the "new". I did a google search and sure enough most of the websites give the same advice as winbatch.

So I was faced with the option of keeping my working implementation or try and use the “delete “new. After trying to implement both suggestions I have hit a block wall.

I have commented out the “delete on the code below.

But if I run it it will only print out the last cin << 3 times.

Any suggestions how I can use the delete and still get the code to work properly????

Regards

tim

#include <iostream>
#include <cstdlib>
#include <string>
using namespace std;

class doctor
{

protected:
string newname;
public:
doctor(); //constructor
virtual ~doctor();
virtual void set_Name(string itsname);
virtual string get_Name();
}; //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;
}

int main(int argc, char *argv[])
{


doctor* Array[5];
doctor* newdoctor;

newdoctor = new doctor();
char doctorname[20];

    for(int a=0;a<3;a++)
    {
    cout<<"Enter doctor name"<<endl;
    cin.getline(doctorname,20,'\n');

    newdoctor->set_Name(doctorname);
    Array[a]=newdoctor;

    //delete newdoctor;
    }




for(int i=0;i<3;i++)
{
cout<<"\n"<<endl;
cout<<Array[i]->get_Name()<<endl;;

}

  system("PAUSE");	
  return 0;
}

You don't need pointers here at all to begin with, if you are predetermining your maximum number of doctors at 5. (Also, if you are asking for 5 doctors, why are you only looping from 0 to 2? You should loop from 0 - 4.

#include <iostream>
#include <cstdlib>
#include <string>
using namespace std;
 
class doctor
{
 
protected:
string newname;
public:
doctor(); //constructor
virtual ~doctor();
virtual void set_Name(string itsname);
virtual string get_Name();
}; //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;
}
 
int main(int argc, char *argv[])
{
 
 
doctor Array[5];
 
char doctorname[20];
 
for(int a=0;a<3;a++)
{
cout<<"Enter doctor name"<<endl;
cin.getline(doctorname,20,'\n');
doctor newdoctor;
newdoctor.set_Name(doctorname);
Array[a]=newdoctor;
 
}
 
 
 
 
for(int i=0;i<3;i++)
{
cout<<"\n"<<endl;
cout<<Array[i].get_Name()<<endl;;
 
}
 
system("PAUSE");	
return 0;
}

Thanks, winbatch. That did the trick nicely.

BUT

if I were to use pointers do you know how I sort out the above problem. the reason I ask is part of project is to use pointers. If I get it working for inputing a doctor name I can implement the rest of the methods myself.

if you want to use delete then use before program ends or before the local block in which memory is allocated but make sure you don't access that pointer after that

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.