get method prints only first letter in char array..why??
Feb 2, 2014 at 8:38am UTC
Header:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32
class employee
{
int * id;
char * name;
int daysNotPaid;
bank mybank;
int accountnum;
int * datebirth;
double balance;
public :
employee();
employee(int idnum ,char *);
int getid();
char * getname();
int getdaysnotpaid();
bank getbank();
int getdate();
int getaccnum();
double getbalance();
void setid(int );
void setdays(int );
void setbank(bank);
void setaccountnum(int );
void setdatebirth(int );
void setbalance(double );
void work();
void printpersoninfo();
void payment();
bool performshopping(int );
int getage(int );
~employee();
};
cpp:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31
#include <iostream>
#include <cstring>
#include <string>
#include "Employee.h"
using namespace std;
employee::employee()
{
id=new int [9];
datebirth=new int [8];
}
employee::employee(int idnum,char * ename)
{
id=new int [9];
*id=idnum;
this ->name=ename;
datebirth=new int [8];
}
int employee::getid()
{
return *id;
}
char * employee::getname()
{
return name;
}
main:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
#include <iostream>
#include <string>
#include "Employee.h"
using namespace std;
int main()
{
int emp1id;
char emp1name[30];
cout<<"employee #1 name: " ;
cin>>emp1name;
cout<<"employee #1 id: " ;
cin>>emp1id;
employee emp1(emp1id,emp1name);
}
why when im trying to print the employee's name like this:
cout<<"Name: " <<*name<<endl;
it prints only the first letter?
Feb 2, 2014 at 9:13am UTC
That is because saying *name is actually dereferencing the pointer you passed, which is the same as writing *(name+0)
or name[0]
, which gives as a char value the first character. Try just doing this instead: std::cout << "Name: " << name << std::endl;
Topic archived. No new replies allowed.