날짜 : 190516

주제 : 구조체와 문자열 함수를 통해서 학생 관리 시스템을 만든다.

특징 :

cin의 기능과 c의 문자열 함수들을 통해 구조체 문자열에 문자를 입력한다. 

메뉴 기능을 구현한다.

 

#include <iostream>

using namespace std;

#define NAME_SIZE 32
#define ADDRESS_SIZE 128
#define PHONE_SIZE 14 
#define STUDENT_MAX 10

struct _tagStudent
{
	char	name[NAME_SIZE];
	char	address[ADDRESS_SIZE];
	char	phoneNumber[PHONE_SIZE];
	int number;
	int kor;
	int eng;
	int math;
	int total;
	float average;
};

enum MENU
{
	MENU_NONE,
	MENU_INSERT,
	MENU_DELETE,
	MENU_SEARCH,
	MENU_OUTPUT,
	MENU_EXIT
};

int main()
{
	_tagStudent studentArr[STUDENT_MAX] = {};
	int studentCount = 0;
	int studentNum = 1;
	char	searchName[NAME_SIZE] = {};
	char	deleteName[NAME_SIZE] = {};

	while (true)
	{
		system("cls");

		// 메뉴를 출력
		cout << "1. 학생 등록" << endl;
		cout << "2. 학생 삭제" << endl;
		cout << "3. 학생 탐색" << endl;
		cout << "4. 학생 출력" << endl;
		cout << "5. 종료" << endl;
		cout << "메뉴를 선택하세요 : ";
		int menu;
		cin >> menu;

		// cin은 만약 오른쪽에 실수로 문자를 입력할 경우 에러 발생
		// 그렇기 때문에 예외처리를 해준다.
		// 에러가 발생하면 cin 내부의 에러버퍼를 비워주고
		// (입력버퍼 : 입력한 값을 저장해놓고 그 값을 변수에 넣어주는 역할)
		// 이 입력버퍼에 '\n'이 남아있으므로 버퍼를 순회하여 '\n'를 지워준다.
		
		// fail() 입력 에러가 발생했을 경우 true 반환
		if (cin.fail())
		{
			cin.clear(); // 에러버퍼 비우기

			// 입력버퍼에 '/n'이 남아 있으므로 입력버퍼를 검색하여 '\n'를 삭제
			// 첫번째 : 검색하고자 하는 버퍼 크기를 지정. 넉넉히 1024
			// 두번쨰 : 찾고자하는 문자를 넣어준다. 그 문자까지 이동해서 삭제
			cin.ignore(1024, '\n');
			continue;
		}

		if (menu == MENU_EXIT)
			break;

		switch (menu)
		{
		case MENU_INSERT:
			system("cls");
			cout << "========== 학생 추가 ==========" << endl;
			// 학생 수가 모두 찬 경우
			if (studentCount == STUDENT_MAX)
				break;

			// 학생정보를 추가(이름, 주소, 학번, 전화번호)
			// 국어 영어 수학 점수는 입력받는다. 학번, 총점, 평균은 연산된다
			cout << "이름 : ";
			cin >> studentArr[studentCount].name;

			// cin과 cin.getline을 같이 쓸 때 주의해야 한다... 
			// cin이기 때문에 콘솔에서 입력을 하고 엔터를 친 값을 입력한다.
			// 그러면 cin의 eofbit가 unset 상태로 된다.
			// 그리고 cin의 위치는 cin >> studentArr[studentNum].name;의 가장 마지막인
			// 그래서 '\n'을 가르킨다. 이상태에서 getline을 하면 '\n'을 읽으려고 한다.
			// 따라서 '\n'을 찾아서 지워야함.
			// 또는 대신 cin >> std::ws;을 써서 '\n'을 자연스레 넘어가서
			// 다음 오는 getline을 정상적으로 읽는다.
			cin.ignore(1024, '\n');

			cout << "주소 : ";
			// getline()은 뉴라인 문자를 입력스트림에서 버린다.
			cin.getline(studentArr[studentCount].address, ADDRESS_SIZE);

			cout << "전화번호 : " << endl;
			cin.getline(studentArr[studentCount].phoneNumber, PHONE_SIZE);

			cout << "국어 점수 : ";
			cin >> studentArr[studentCount].kor;

			cout << "영어 점수 : ";
			cin >> studentArr[studentCount].eng;

			cout << "수학 점수 : ";
			cin >> studentArr[studentCount].math;

			studentArr[studentCount].total = studentArr[studentCount].kor + studentArr[studentCount].eng + studentArr[studentCount].math;
			studentArr[studentCount].average = studentArr[studentCount].total / 3.f;

			studentArr[studentCount].number = studentNum;

			++studentNum;
			++studentCount;
			break;
		case MENU_DELETE:
			cout << "========== 학생 삭제 ==========" << endl;
			cout << "지울 이름을 입력하세요 : ";
			cin >> std::ws;
			cin.getline(deleteName, NAME_SIZE);

			// 등록되어 있는 학생 수만큼 반복
			for (int i = 0; i < studentCount; ++i)
			{
				// 학생을 찾았을 경우
				if (strcmp(studentArr[i].name, deleteName) == 0)
				{
					for (int j = i; j < studentCount- 1; ++j)
					{
						studentArr[i] = studentArr[i + 1];
					}
					--studentCount;					
					
					cout << "학생 정보가 삭제되었습니다." << endl;
					break;
				}
			}

			break;
		case MENU_SEARCH:
			system("cls");

			cout << "========== 학생 탐색 ==========" << endl;
			
			//cin.ignore(1024, '\n');
			cout << "탐색할 이름을 입력하세요 : ";
			cin >> std::ws;
			
			cin.getline(searchName, NAME_SIZE);

			// 등록되어 있는 학생 수만큼 반복
			for (int i = 0; i < studentCount; ++i)
			{
				// 학생을 찾았을 경우
				if (strcmp(studentArr[i].name, searchName) == 0)
				{
					cout << "이름 : " << studentArr[i].name << endl;
					cout << "전화번호 : " << studentArr[i].phoneNumber << endl;
					cout << "주소 : " << studentArr[i].address << endl;
					cout << "학번 : " << studentArr[i].number << endl;
					cout << "국어 : " << studentArr[i].kor << endl;
					cout << "영어 : " << studentArr[i].eng << endl;
					cout << "수학 : " << studentArr[i].math << endl;
					cout << "총점 : " << studentArr[i].total << endl;
					cout << "평균 : " << studentArr[i].average << endl << endl;
					break;
				}
			}
			break;
		case MENU_OUTPUT:
			system("cls");
			cout << "========== 학생 출력 ==========" << endl;
			// 등록된 학생 수만큼 반복
			for (int i = 0; i < studentCount; ++i)
			{
				cout << "이름 : " << studentArr[i].name << endl;
				cout << "전화번호 : " << studentArr[i].phoneNumber << endl;
				cout << "주소 : " << studentArr[i].address << endl;
				cout << "학번 : " << studentArr[i].number << endl;
				cout << "국어 : " << studentArr[i].kor << endl;
				cout << "영어 : " << studentArr[i].eng << endl;
				cout << "수학 : " << studentArr[i].math << endl;
				cout << "총점 : " << studentArr[i].total << endl;
				cout << "평균 : " << studentArr[i].average << endl << endl;

			}

			break;
		default:
			cout << "메뉴를 잘못 선택했습니다." << endl;
			break;
		}

		system("pause");
	}

	return 0;
}

/* 숙제 : 관리 프로그램
도서 대여 프로그램 만들기
1. 책 등록
2. 책 대여
3. 책 반납
4. 책 목록
4. 종료

책 구조체는 책이름, 대여금액, 책번호, 대여여부
책목록을 선택하면 책 정보를 모두 출력
*/

 

 

 

+ Recent posts