Click here to Skip to main content
15,897,273 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have this code and it returns the member function as written but return only the last character of emp_name.
any assistance is highly appreciated.

What I have tried:

C++
#include <iostream>

using namespace std;

class emp {
public:

	int getInfo(char emp_name, int emp_id_number, int emp_age) {
		cout << "Employee Name: " << emp_name << "\n";
		cout << "Employee Id Number: " << emp_id_number << "\n";
		cout << "Employee Age: " << emp_age;
	}
};
int main() {
	emp emp1;
	emp1.getInfo('Felix', 635932, 23);
}
Posted
Updated 10-Oct-17 1:28am
Comments
nv3 10-Oct-17 5:54am    
What makes you think that type char for emp_name could hold an entire character string? It holds just a single character. Open your C++ text book and start reading the first 10 pages. Then try again.
Felix Mensah 10-Oct-17 6:10am    
thanks for the adv.

dont use calling by value, try to use calling by reference.
Your function waits a char value not a string.

C++
int getInfo(char* emp_name, int emp_id_number, int emp_age) 
 
Share this answer
 
C++
#include <iostream>

using namespace std;

class emp {
public:

	int getInfo(char* emp_name, int emp_id_number, int emp_age) {
		cout << "Employee Name: " << emp_name << "\n";
		cout << "Employee Id Number: " << emp_id_number << "\n";
		cout << "Employee Age: " << emp_age;
	}
};
int main() {
	emp emp1;
	emp1.getInfo("Felix", 635932, 23);
}
 
Share this answer
 
You probably want to pass a string a to getInfo() but you are actually passing a single character.

Change these two lines:
// Must be char* here
int getInfo(char *emp_name, int emp_id_number, int emp_age)

// Must pass a string here using double quotes
emp1.getInfo("Felix", 635932, 23);


You have defined getInfo() to return an int but you have no return statement.

I suggest to use a high warning level when compiling (-Wall with G++, /W4 in the project settings with VS). Then you would have got at least two warnings for your code.
 
Share this answer
 
Comments
Felix Mensah 10-Oct-17 6:13am    
it worked thanks
#include <iostream>

using namespace std;

class emp {
public:

int getInfo(char* emp_name, int emp_id_number, int emp_age) {
cout << "Employee Name: " << emp_name << "\n";
cout << "Employee Id Number: " << emp_id_number << "\n";
cout << "Employee Age: " << emp_age;
}
};
int main() {
emp emp1;
emp1.getInfo("Felix", 635932, 23);
}
C++
int getInfo(char emp_name, int emp_id_number, int emp_age) {

You have declared the emp_name parameter as a single character, rather than a character string. It should be:
C++
int getInfo(char* emp_name, int emp_id_number, int emp_age) { // char*
 
Share this answer
 
Comments
Felix Mensah 10-Oct-17 6:14am    
it worked thanks. I had to also enclose Felix in double quotes
#include <iostream>

using namespace std;

class emp {
public:

int getInfo(char* emp_name, int emp_id_number, int emp_age) {
cout << "Employee Name: " << emp_name << "\n";
cout << "Employee Id Number: " << emp_id_number << "\n";
cout << "Employee Age: " << emp_age;
}
};
int main() {
emp emp1;
emp1.getInfo("Felix", 635932, 23);
}

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