It is difficult here to propose the "right way" since there are essentially two ways to solve the problem.
The first is correct the confusion between char, char* and char[] you did. The result is a program that's not C++, bust just C with classes +iostream.
An I wonder why in 2011 this is still the way the most of teachers tech C++ ...
#ifndef PPL_H_INCLUDED
#define PPL_H_INCLUDED
#include <cstring>
class cPeople
{
public:
static const unsigned max_namelength = 32; const char* getName() const
{ return hisName; } void setName(const char* name)
{
std::strncpy(hisName,name,max_namelength);
hisName[max_namelength] = '\0';
}
private:
char hisName[max_namelength+1]; };
#endif // PPL_H_INCLUDED
#include <iostream>
#include <iomanip>
#include "ppl.h"
int main()
{
static const unsigned max_read_buffer = 40; cPeople man;
char myName[max_read_buffer+1]; std::cout << "Please enter your name: ";
std::cin >> std::setw(max_read_buffer) >> myName;
man.setName(myName);
std::cout << "The name is: " << man.getName() << std::endl;
return 0;
}
The other solution uses C++ facilities more congruently:
#ifndef PPL_H_INCLUDED
#define PPL_H_INCLUDED
#include <string>
#include <utility>
class cPeople
{
public:
const std::string& getName() const
{ return hisName; }
void setName(std::string name)
{ hisName = std::move(name); }
private:
std::string hisName;
};
#endif // PPL_H_INCLUDED
#include <iostream>
#include <string>
#include "ppl.h"
int main()
{
cPeople man;
std::string myName;
std::cout << "Please enter your name: ";
std::cin >> myName;
man.setName(myName);
std::cout << "The name is: " << man.getName() << std::endl;
return 0;
}
Note that there is anymore the need to take care of buffer lengths. memory management and variable length objects (like strings) are left to the standard library.