I tried to input string inside the function, getline(cin, worker->Name)
works for the first time input but for the next input it skips or got ignored. it works for the integer but it doesn't work for the string, what should I do?
Code:
#include <iostream>
using namespace std;
struct Worker {
string Name;
int Salary;
int Status;
int Child;
};
void InputWorkerData(Worker *worker) {
cout << "Nama: ";
getline(cin, Worker->Name);
cout << "Gaji per bulan: ";
cin >> worker->Salary;
cout << "status (menikah = 1, single = 0): ";
cin >> worker->Status;
if(worker->Status == 1) {
cout << "jumlah anak: ";
cin >> worker->Child;
} else {
worker->Child = 0;
}
cout << endl;
}
int main() {
Worker worker1, worker2, worker3;
InputWorkerData(&worker1);
InputWorkerData(&worker2);
InputWorkerData(&worker3);
return 0;
}
Output:
Nama: michael jordan
Gaji per bulan: 7000
status (menikah = 1, single = 0): 1
jumlah anak: 3
Nama: Gaji per bulan: 5000
status (menikah = 1, single = 0): 0
Nama: Gaji per bulan: 9000
status (menikah = 1, single = 0): 1
jumlah anak: 2