카테고리 없음
[C++] 전화번호 관리 프로그램
s뽈록이s
2013. 8. 31. 12:20
struct tell_number { char name[20]; char number[20]; }typedef tell_number; void main() { tell_number tell[10] = {0}; int cmd; int i; while(1) { cout << "입력[0] 삭제[1] 수정[2] 출력[3] 종료[-1]" << endl; cout << "원하는 동작을 선택해 주세요 : "; cin >> cmd; if(cmd == 0) { for(i = 0; i < 10; i++)if(tell[i].name[0] == 0)break; if(i == 10) { cout << "더 이상 저장할 공간이 없습니다." << endl << endl; continue; } cout << "이름을 입력해 주세요 : "; cin >> tell[i].name; cout << "전화번호를 입력해 주세요 : "; cin >> tell[i].number; cout << "저장되었습니다." << endl << endl; } else if(cmd == 1) { cout << "삭제할 번호의 index를 입력해 주세요 : "; cin >> i; if(tell[i].name[0] == 0) { cout << "저장되어 있지 않습니다." << endl << endl; continue; } for(i = 0; i < 10; i++)tell[i].name[i] = 0; for(i = 0; i < 13; i++)tell[i].number[i] = 0; cout << "삭제되었습니다." << endl << endl; } else if(cmd == 2) { cout << "수정할 번호의 index를 입력해 주세요 : "; cin >> i; if(tell[i].name[0] == 0) { cout << "저장되어 있지 않습니다." << endl << endl; continue; } cout << "이름을 입력해 주세요 : "; cin >> tell[i].name; cout << "전화번호를 입력해 주세요 : "; cin >> tell[i].number; cout << "수정되었습니다." << endl << endl; } else if(cmd == 3) { for(i = 0; i < 10; i++) { if(tell[i].name[0] != 0) { cout << i << "번째" << endl; cout << "이름 : " << tell[i].name << endl; cout << "전화번호 : " << tell[i].number; } } } else if(cmd == -1) break; else cout << "다시 입력해 주세요" << endl << endl; } }
프로그램 실행화면
전화번호를 관리해 주는 프로그램이다. 그냥 흉내만 내본 것이다. 정말로 활용할 수 있게 할려면 MFC와 MYSQL을 사용해서 구현해야 할 것이다.
C++을 연습삼아 하는 것이니 이정도에서 만족하도록 하자. -1 ~ 3의 숫자를 이용해 원하는 동작을 하도록 한다. 0은 입력, 1은 삭제, 2는 수정, 3은 출력, -1은 종료의 동작을 하게 된다.
if - elseif 문에서 cmd에 따라서 어떻게 동작하는지 보면 쉽게 이해될 것이다.