problem with initialization


why does the line "this->name=name" leads to an error and how can i solve it plz?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#include <iostream>
#include <string>

using namespace std;

class Employee {
	char name[20];
	unsigned int salary;
	
public:
	void init(string, unsigned int);
	void print();
};

void Employee::init(string name, unsigned int salary)
{
	this->name=name;
	this->salary=salary;
}
Last edited on
Because, the name in void Employee::init(string name, unsigned int salary) is of type string and in the Employee class its of a type char[] therefore it causes an error. They are of different types. To solve it, change char name[20]; to string name.

One more thing change void init(string, unsigned int); to void init(string name, unsigned int salary); in line# 11.

Cheers!

-Stormboy
Last edited on
but the problem is that i need to limit the employee name to 20 characters...how can i do that? thx 4 ur hlp
try:
1
2
3
4
void Employee::init (string name, unsigned int salary) {
     this->name = name.c_str();
     this->salary = salary;
}


btw, what do you mean by "write your question here"?
didnt help...i just didnt notice i didnt delete this sentence before publishing my question..
Last edited on
closed account (j3Rz8vqX)
Write your question here.

(Speculation)

I believe it is the preset data in the text box before users enter questions, using "http://www.cplusplus.com/".
I've seen it a few times now on different posts.

It should be implemented so that it becomes deleted when the text box is selected/focused. Maybe the web admin will get to that eventually. Or unless it has something to do with the users browser/device. =D
Last edited on
> i need to limit the employee name to 20 characters...how can i do that?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#include <string>

class  Employee {
	// char name[20];
        std::string name ;
	unsigned int salary;

public:
	void init( std::string, unsigned int );
	void print();

	// tip: use constructor(s) to initialize objects
};

void Employee::init( std::string name, unsigned int salary )
{
	this->name = name.substr( 0, 20 ) ; // max 20 chars
	this->salary=salary;
}
The C++ std::string is safer than using a character array. But in either case, if you want to limit the length of the employee name to 20 characters, you need to add extra checks of some sort. Also the array length needs to be 21 to allow for the null terminator.

You could do this:
strcpy(this->name, name.c_str());
strcpy is one of the many functions for manipulating c-strings (#include <cstring> header). But if the input is longer than 20 characters, it will overflow beyond the end of the array and corrupt other areas of memory.
You could use strncpy() instead. That gives protection against overwriting other areas of memory. But you need to be sure to add the null terminator just in case.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#include <iostream>
#include <string>
#include <cstring>

using namespace std;

class Employee {
    char name[21];                    // allow up to 20 chars + null terminator
    unsigned int salary;
    
public:
    void init(string, unsigned int);
    void print();
};

void Employee::init(string iname, unsigned int isalary)
{
    strncpy(name, iname.c_str(), 20); // copy maximum of 20 characters
    name[20] = 0;                     // add null terminator
    salary   = isalary;
}

http://www.cplusplus.com/reference/cstring/strcpy/
http://www.cplusplus.com/reference/cstring/strncpy/
is there a way to keep the "char name[20]" in the code and initialize this array using one of the class's function's?? (not the constructor)
is there a way to keep the "char name[20]"
It would be better to use char name[21] since your requirement is "i need to limit the employee name to 20 characters"

char name[20] would limit the employee name to a maximum of 19 characters using a conventional null-terminated string.

and initialize this array using one of the class's function's?? (not the constructor)
Yes - that's exactly what I posted above.
true...thx very much for your help!
Topic archived. No new replies allowed.