Click here to Skip to main content
15,121,851 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello smartens!
Maybe this question is fiftieth (SORRY) and it may repeat, but when i found an information or examples about my problem, I have not understand. Topic about char type topic are hell in my beginner learning. In simple main, char type works fine, but in classes it is going to error. Here is my example and I try to show that is works incorrect.

C++
// ppl.h
class cPeople
{
	private:
			char hisName[33];
	public:
			char getName() const { return *hisName; }
			void setName(char name) { hisName[33] = name; }
};


// main.cpp
#include <iostream>
#include "ppl.h"

int main()
{
	cPeople man;

	char myName;
        std::cout << "Please enter your name: ";
	std::cin >> myName;

	man.setName(myName);
	std::cout << "The name is: " << man.getName() << std::endl;

	return 0;
}


I entered name "John". Do you know, what i get in output screen? I got a smile simbol, lol.
Any solution?
Thanks for the patience.
Posted
Updated 4-Feb-12 5:45am
v2
Comments
Mohibur Rashid 4-Feb-12 19:15pm
   
learn more about pointer, function call by value, function call by reference, your misconception leads you to this problem
MNMR 5-Feb-12 6:39am
   
thanks...

You seem to be confused as to what a char actually is: char is a single character. 'A' or 'B' or 'C' or ...
hisName is an array of 33 single character objects, so "John" would occupy 4 of those "slots" (in fact 5, as there is a null character on the end to signify the end. So: *hisname is a single character, not a name. If your code had worked, getName would return 'J'.
But it doesn't. Because setName doesn't work either as hisName[33] is also a single character, and worse, one that is outside the range of values assigned to the array in the firstplace, which will have indexes between 0 and 32

To make matters worse, your cin is set to read a single character anyway: 'J' because myName is declared as a single character.

Seriously, you need to go right back to basics, and start reading your course from the very beginning. There are just too many concepts you haven't understood yet.
   
It is a beginner question. But everbody was a beginner once. In C and C++ pointers are essential. And your problem has to do with pointers. You should read a good book for beginners or look for an online lesson for beginners.

I will show you your errors:
C++
// This is a single char variable.
// You would probably want a buffer that can hold multiple characters.
char myName;

// This will return the first character of hisName
char getName() const { return *hisName; }

// This will assign the character name to the 34th character of hisName
//  which is not allowed:
// The size of hisName is 33 chars. Accessing the 34th element (index 33)
// will corrupt your memory.
// Your intention here is to copy a string. 
// See the functions strcpy() and strncpy().
void setName(char name) { hisName[33] = name; }
   
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++ ...

C++
// ppl.h
#ifndef PPL_H_INCLUDED
#define PPL_H_INCLUDED
#include <cstring>

class cPeople
{
public:
    static const unsigned max_namelength = 32; //magic number defined only once
    const char* getName() const 
    { return hisName; } //< array decay into pointer
    void setName(const char* name)
    {
        //this copies all 'name' into hisName up to max_length and...
        std::strncpy(hisName,name,max_namelength);
        //...ensure the proper null-termination
        hisName[max_namelength] = '\0';
    }
private:
    char hisName[max_namelength+1]; //+1 for the terminating null char 
};

#endif // PPL_H_INCLUDED

C++
// main.cpp
#include <iostream>
#include <iomanip>
#include "ppl.h"

int main()
{
    static const unsigned max_read_buffer = 40; //< max read chars
    cPeople man;

    char myName[max_read_buffer+1]; // +1 or the terminating null char
    std::cout << "Please enter your name: ";
    //read up to (but no more than) the buffer capacity
    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:

C++
// ppl.h
#ifndef PPL_H_INCLUDED
#define PPL_H_INCLUDED

#include <string>
#include <utility>

class cPeople
{
public:
    //will not copy on return
    const std::string& getName() const
    { return hisName; }

    //use move semantics where it can
    void setName(std::string name)
    { hisName = std::move(name); }

private:
    std::string hisName;
};

#endif // PPL_H_INCLUDED

C++
// main.cpp
#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.
   
Comments
MNMR 11-Feb-12 12:25pm
   
A big thank to you Emilio! Now it's clearer! Your hard work to explain is really value!!! By the way, string library and string type work perfect!
Modified code,

C++
class cPeople
{
    private:
            char hisName[33];
    public:

            char* getName() const { return hisName; }
            void setName(char name) { hisName[33] = name; }
};


See if it works.
   
Comments
Emilio Garavaglia 6-Feb-12 2:41am
   
What about setName?

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900