왜 실행하면 메뉴 1번부터 6번까지 다 프로세스가 종료되는지 모르겠습니다... 도와주세요!!!

조회수 761회
#include<iostream>
#include<string>
#include <iomanip>
#include <string.h>
#include <stdio.h>

#pragma warning(disable:4996)

using namespace std;

int Menu;
int StdNum;

struct Subject
{
    string Name;    // 과목 이름
    int Grade;      // 과목 학점
    string Rate;    // 과목 등급
    float GPA;      // 과목 평점
};                  // 과목 정보

struct Student
{
    string Name;                        // 학생 이름
    int Number;                         // 학번
    int SubNum;
    Subject* pSubjectList = nullptr;    // 과목
};
// char[]들을 string들로 수정함.
inline void InputValue(string& str)
{
    getline(cin, str);
}

/* upgraded.cpp 는 개인적으로 조금 최적화를 시킨 파일입니다.
    다만 교수님이 지정한 함수 구조와 다를 수 있으니 잘 보고 사용해주세요.

*/
inline void InputValue(int& i)
{
    // getline은 \n 조차도 입력을 받아버립니다.
    // cin >> i 로 정수를 받은 후 그 줄에 남은 \n을 다음 번의 getline이 읽게 되면,
    // 의도와 다르게 입력 될 수 있으므로, 숫자를 받은 후 \n을 없애기 위하여 getline을 한번 수행한다.
    string line_cleaner;
    getline(cin, line_cleaner);
}

inline void PrintMenu(void);
void InputData(Student* pSt, int StudentNum);
float CalcAveGPA(Subject* pSj, int SubjectNum);
void PrintOneData(const Student& rSt);
void PrintAllData(const Student* pSt, int StudentNum);
Student* StdSearch(Student* pSt, int StudentNum);
void PrintAllStdInfo(Student* pSt, int StudentNum);
void Modify(Student* pSt);
float ConvertRateToScore(string Name);



Subject* SubSearch(const Student* pSt); //added.
void InputStudentData(Student* pSt);    //특정 학생의 학번과 이름등을 입력받는 함수
void InputStudentSubject(Student* pSt); //특정 학생이 수강 과목 정보를 입력받는 함수

int main()
{
    Student* pStudentList = nullptr;    // 구조체 포인터

    cout.setf(ios::fixed, ios::floatfield);
    cout.precision(2);

    while (Menu != 6)  // 메뉴출력부분
    {
        PrintMenu();
        InputValue(Menu);
        cout << endl;

        if (Menu == 1)      // 학생정보 입력부분
            InputData(pStudentList, StdNum);
        else if (Menu == 2) // 학생정보 출력부분
            PrintAllData(pStudentList, StdNum);
        else if (Menu == 3) // 특정학생 검색부분
        {
            Student* pResult = StdSearch(pStudentList, StdNum);

            if (pResult == nullptr)
            {
                cout << "\t\t학생을 찾을 수 없습니다." << endl;
                cout << endl;
            }
            else
            {
                cout << "\t\t학생을 찾았습니다." << endl;

                PrintOneData(*pResult);
            }
        }
        else if (Menu == 4)
            PrintAllStdInfo(pStudentList, StdNum);
        else if (Menu == 5)
            Modify(pStudentList);
        else
            break;
    }

    if (pStudentList != nullptr)
    {
        for (int i = 0; i < StdNum; i++)
        {
            if (pStudentList[i].pSubjectList != nullptr)
                delete[] pStudentList[i].pSubjectList;
        }

        delete[] pStudentList;
    }

    return 0;
}

void PrintMenu(void)    // 메뉴출력 함수
{
    cout << "===== 메뉴 =====" << endl;
    cout << "1. 학생 성적 입력" << endl;
    cout << "2. 전체 학생 성적 보기" << endl;
    cout << "3. 학생 이름 검색" << endl;
    cout << "4. 전체 학생 목록 보기" << endl;
    cout << "5. 학생 정보 수정" << endl;
    cout << "6. 프로그램 종료" << endl << endl;
    cout << "원하는 기능을 입력하세요 : ";
}

void InputStudentData(Student* pSt) {
    //new
    cout << "이름 : ";
    InputValue(pSt->Name);
    cout << "학번 : ";
    InputValue(pSt->Number);
    cout << endl;
}
void InputStudentSubject(Student* pSt) {
    //new
    cout << "수강한 과목 수를 입력 : ";
    InputValue(pSt->SubNum);
    cout << endl;

    pSt->pSubjectList = new Subject[pSt->SubNum];

    cout << "* 수강한 과목" << pSt->SubNum << "개와 각 교과목명, 과목학점, 과목등급을 입력하세요." << endl;

    for (int j = 0; j < pSt->SubNum; j++)
    {
        cout << "교과목명 : ";
        InputValue(pSt->pSubjectList[j].Name);
        cout << "과목학점 : ";
        InputValue(pSt->pSubjectList[j].Grade);
        cout << "과목등급(A+ ~ F) : ";
        InputValue(pSt->pSubjectList[j].Rate);
        cout << endl;

        pSt->pSubjectList[j].GPA = pSt->pSubjectList[j].Grade * ConvertRateToScore(pSt->pSubjectList[j].Rate);
    }
    cout << endl;
}
void InputData(Student* pSt, int StudentNum)    // 반복문으로 성적 입력
{

    cout << "입력할 학생 수를 입력 : ";
    InputValue(StdNum);
    cout << endl;

    pSt = new Student[StdNum];
    for (int i = 0; i < StdNum; i++)
    {
        cout << "* " << i + 1 << " 번째 학생 이름과 학번을 입력하세요." << endl;

        InputStudentData(pSt + i);
        InputStudentSubject(pSt + i);
    }
}

float CalcAveGPA(Subject* pSj, int SubjectNum)  // 평균평점 계산 함수
{
    int TotalGrade = 0;
    float TotalGPA = 0;

    for (int i = 0; i < SubjectNum; i++)
    {
        TotalGrade += pSj[i].Grade;
        TotalGPA += pSj[i].GPA;
    }

    return TotalGPA / TotalGrade;
}

void PrintOneData(const Student& rSt)   // 결과 출력 함수
{
    cout << "======================================================================" << endl;
    cout << "이름 : " << rSt.Name << "\t학번 : " << rSt.Number << endl;
    cout << "======================================================================" << endl;
    cout.width(20); cout << "과목명";
    cout.width(10); cout << "과목학점";
    cout.width(10); cout << "과목등급";
    cout.width(10); cout << "과목평점" << endl;
    for (int i = 0; i < rSt.SubNum; i++)
    {
        cout.width(20); cout << rSt.pSubjectList[i].Name;
        cout.width(7); cout << rSt.pSubjectList[i].Grade;
        cout.width(10); cout << rSt.pSubjectList[i].Rate;
        cout.width(11); cout << rSt.pSubjectList[i].GPA << endl;
    }
    cout << "======================================================================" << endl;
    cout.width(46); cout << "평균평점 : " << CalcAveGPA(rSt.pSubjectList, rSt.SubNum) << endl;
    cout << endl;
}

void PrintAllData(const Student* pSt, int StudentNum)   // 전체 성적 출력
{
    cout << "\t\t    전체 성적보기" << endl;

    for (int i = 0; i < StudentNum; i++)
    {
        PrintOneData(pSt[i]);
    }
}

Student* StdSearch(Student* pSt, int StudentNum)    // 검색 함수
{
    Student* pResult = nullptr;
    char Name[30];

    cout << "찾고자하는 학생의 이름을 입력해주세요 : ";
    cin >> Name;
    cout << endl;

    for (int i = 0; i < StudentNum; i++)
    {
        if (pSt[i].Name == Name)

            pResult = &pSt[i];
    }

    return pResult;
}

float ConvertRateToScore(string Name)   // 평점처리 함수
{
    char* head = &Name[0];
    *head = toupper(*head);

    float Score = 0;

    if (Name == "A")
        Score = 4.5f;
    else if (Name == "A" || Name == "A0")
        Score = 4;
    else if (Name == "B+")
        Score = 3.5f;
    else if (Name == "B" || Name == "B0")
        Score = 3;
    else if (Name == "C+")
        Score = 2.5f;
    else if (Name == "C" || Name == "C0")
        Score = 2;
    else if (Name == "D+")
        Score = 1.5f;
    else if (Name == "D" || Name == "D0")
        Score = 1;
    else if (Name == "F")
        Score = 0;

    return Score;
}

void PrintAllStdInfo(Student* pSt, int StudentNum)
{
    cout << "===================================" << endl;
    cout.width(10); cout << "학번";
    cout.width(10); cout << "이름" << endl;
    cout << "===================================" << endl;

    for (int i = 0; i < StudentNum; i++)
    {
        cout.width(10); cout << pSt[i].Number;
        cout.width(10); cout << pSt[i].Name << endl;
    }

    cout << "===================================" << endl << endl;
}

void Modify(Student* pSt)
{
    Student* St = StdSearch(pSt, StdNum);
    if (St != NULL) {
        string operation;
        InputValue(operation);
        cout << "* (" << St->Name << ", " << St->Number << ")의 정보를 수정하세요." << "" << endl;
        cout << "* 수정할 정보를 입력하세요. [학생정보/과목정보]" << endl;
        if (operation == "학생정보") {
            InputStudentData(pSt);
        }
        else if (operation == "과목정보") {
            InputStudentSubject(pSt);
        }
        else {
            cout << "입력이 잘못 되었습니다." << endl;
        }
    }
    cout << endl << endl;
}
Subject* SubSearch(const Student* pSt) {
    string subject_title;
    cout << " * 검색할 과목 명을 입력하세요. " << endl;
    cout << " > ";
    InputValue(subject_title);
    for (int i = 0; i < pSt->SubNum; i++) {
        if (pSt->pSubjectList[i].Name == subject_title) {
            return (pSt->pSubjectList + i);
        }
    }
    return NULL;
}

이미지

이미지

이미지

이미지

이미지

이미지

1 답변

  • inline void InputValue(int& i)
    {
        // getline은 \n 조차도 입력을 받아버립니다.
        // cin >> i 로 정수를 받은 후 그 줄에 남은 \n을 다음 번의 getline이 읽게 되면,
        // 의도와 다르게 입력 될 수 있으므로, 숫자를 받은 후 \n을 없애기 위하여 getline을 한번 수행한다.
        string line_cleaner;
        getline(cin, line_cleaner);
    }
    

    일단 한가지 문제는, InputValue 함수를 보면, i 로 입력값이 반환되어야 할텐데, 함수 안에서 i 가 전혀 쓰이고 있지 않아요.

답변을 하려면 로그인이 필요합니다.

프로그래머스 커뮤니티는 개발자들을 위한 Q&A 서비스입니다. 로그인해야 답변을 작성하실 수 있습니다.

(ಠ_ಠ)
(ಠ‿ಠ)