I'm building a program to calculate the gross wage of employees. it has following fields. emp no, emp name, hours worked, hourly rate and finally calculating gross wage. I used a char for the emp name. In this case once the emp name entered its not allowing to put other hourly rate and hours worked and program stopped. program is below. Kindly tell me what is the reason for char behave like this and if char not suite for this what is the best data type for this. Thanks.
#include <iostream>
using namespace std;
int main()
{
int empno;
char empnm;
float hrw, hrr, grw;
char only holds one character.
If you want it to hold a series of characters you have two options:
- make it a char array: char empnm[100]; //up to 100 characters
- make it a std::string object: std::string empnm; //note that you need to do #include <string> for this
Right now cin is waiting for one character to be put into empnm, but it receives more, so it assigns them to the next cin. Next cin is expecting a float, but receives text - so it errors.
Hope that helps, let us know if you have any further questions.