0

Can someone help me with this? Every time I run the code, it does not display the whole information that is needed. It always has an error at the last information that I type.

I think the error is here:

cout << "Address:";
cin.getline(e.address, 50);
#include <iostream>    
using namespace std;
     
struct Employee {
    char name[50];
    int salary;
    int employmentStatus;
    char Gender[6];
    int Age;
    char address[50];
};
    
int main() {
    
    Employee e;
         
    cout << "Full Name: " ;
    cin.getline(e.name, 50);
    
    cout << "Age : ";
    cin >> e.Age;
    
    cout << "Gender : ";
    cin >> e.Gender;
    
    cout << "Address: ";
    cin.getline(e.address, 50);
    
    cout << "Salary: ";
    cin >> e.salary;
    
    cout << "Employment Status: ";
    cin >> e.employmentStatus;
    
    // Printing employee details 
    
    cout << "\n*** Employee Details ***" << endl;
    cout << "Name : " << e.name << endl << "Salary : " << e.salary << endl << "Age:" << e.Age << endl << "Gender:" << e.Gender << endl;
    cout << "Employment Status: " << e.employmentStatus << endl << "Address : " << e.address;
    
    return 0;
}
Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770
mlky
  • 11
  • 1
  • 1
    What is the error ? – TruthSeeker Feb 02 '22 at 13:52
  • I cannot enter the address and the employment status is diaplaying 'f' only if i enter 'fulltime' in the line employees information – mlky Feb 02 '22 at 13:55
  • What's your hope with the two lines `cout << "\n*** Employee Details` and `***" << endl;` ? – Ted Lyngmo Feb 02 '22 at 13:55
  • Does [Why does std::getline() skip input after a formatted extraction?](https://stackoverflow.com/questions/21567291/why-does-stdgetline-skip-input-after-a-formatted-extraction) answer your question? (`std::getline` is not the same as `std::cin.getline()` but the issue here is the same) – Ted Lyngmo Feb 02 '22 at 14:03
  • Enter pressed after `Gender` is considered as input and hence address might not be taken properly. [DEMO](https://www.godbolt.org/z/8d6n18q8Y) – TruthSeeker Feb 02 '22 at 14:04
  • After i enter all the informations, it should be displaying the whole infos but it only displays the name, age, gender and salary. It did not display the address and employment status always displaying 0 or blank only – mlky Feb 02 '22 at 14:04
  • A demo program that has a link in the above comment solves the problem, it skips the `enter-key` before adress – TruthSeeker Feb 02 '22 at 14:12
  • Why the employment status displaying 0?. Im sorry i have so many questions because i am a beginner and i am trying to study on my own – mlky Feb 02 '22 at 14:18
  • Please go through [this](https://www.godbolt.org/z/8d6n18q8Y) . Employee status is an integer and it is showing valid out put for it. – TruthSeeker Feb 02 '22 at 14:26
  • i figured it out already. thank you so much for helping. this subj giving me always a hard time to finish my laboratories. this is my first time to use stack overflow. – mlky Feb 02 '22 at 15:07

0 Answers0