직원(employee) 구조체 만들고 입력받기
c++ 언어 사용
문제 :
직원 정보를 저장하기 위한 employee 구조체(struct)를 만들고, 최대 100명의 정보를 입력할 수 있도록 구현하라. 정상적으로 입력이 되는지 10명이내로 입력시킨 후, 해당 데이터를 출력시키는 테스트코드도 함께 작성하라.
풀이 코드 :
#include<iostream>
using namespace std;
#define MAX_EMP 100
struct employee{
int number;
char name[10];
char group[20];
char indate[8];
int money;
};
void prtpeople(employee people, int num);
int main(){
employee people[MAX_EMP];
int i=0;
char op;
cout << "자료를 입력해주세요." << endl;
while(i<10){
cout << "1) 사원번호 : "; cin >> people[i].number;
cout << "2) 이름 : "; cin >> people[i].name;
cout << "3) 근무부서 : "; cin >> people[i].group;
cout << "4) 입사일 : "; cin >> people[i].indate;
cout << "5) 급여 : "; cin >> people[i].money;
i++;
cout << "계속해서 자료를 입력하시겠습니까? (y,n)";
cin >> op;
cout << endl;
if(op=='n'){
break;
}
}
cout << "총 " << i << "명의 사용자 정보를 출력합니다." << endl;
for(int j=0; j<i; j++)
prtpeople(people[j], j+1);
return 0;
}
void prtpeople(employee people, int num){
cout << num << "번째 정보 :" << endl;
cout << "1) 사원번호 : " << people.number << endl;
cout << "2) 이름 : " << people.name << endl;
cout << "3) 근무부서 : " << people.group << endl;
cout << "4) 입사일 : " << people.indate << endl;
cout << "5) 급여 : " << people.money << "원" << endl;
cout << "6) 연봉 : " << people.money*12 << "원" << endl;
}
실행 결과 :
자료를 입력해주세요.
1) 사원번호 : 1200113
2) 이름 : 홍길동
3) 근무부서 : 프로젝트1팀
4) 입사일 : 2019-07-19
5) 급여 : 300
계속해서 자료를 입력하시겠습니까? (y,n)y
1) 사원번호 : 1200114
2) 이름 : 조커
3) 근무부서 : 프로젝트2팀
4) 입사일 : 2019-05-12
5) 급여 : 350
계속해서 자료를 입력하시겠습니까? (y,n)n
총 2명의 사용자 정보를 출력합니다.
1번째 정보 :
1) 사원번호 : 1200113
2) 이름 : 홍길동
3) 근무부서 : 프로젝트1팀
4) 입사일 : 2019-07-19,
5) 급여 : 300원
6) 연봉 : 3600원
2번째 정보 :
1) 사원번호 : 1200114
2) 이름 : 조커
3) 근무부서 : 프로젝트2팀
4) 입사일 : 2019-05-12^
5) 급여 : 350원
6) 연봉 : 4200원