code:
#include <iostream>
using namespace std;
struct employee{
int id;
char role;
float salary;
};
int main(){
struct employee henry;
henry.id= 101;
henry.role= 'programmer';
henry.salary=1200000;
cout<<henry.role;
return 0;
}
output:
01pr.cpp:89:17: warning: character constant too long for its type
henry.role= 'programmer';
^~~~~~~~~~~~
01pr.cpp: In function 'int main()':
01pr.cpp:89:17: warning: overflow in conversion from 'int' to 'char' changes value from '1835885938' to ''r'' [-Woverflow]
CodePudding user response:
Of course you cannot, you should declare role like string role; to assign it a string since "programmer" is a string not a single character.
CodePudding user response:
In your given example, the data member role is of type char.
You can/should solve this problem by making the role data member to be of type std::string as shown below:
#include <iostream>
#include <string>
struct employee{
int id;
std::string role; //role is of type std::string
float salary;
};
int main(){
struct employee henry;
henry.id= 101;
henry.role= "programmer"; //note the double quotes here instead of single quotes
henry.salary=1200000;
std::cout<<henry.role;
return 0;
}
The output of the program is:
programmer
which can be seen here.
