'How can I print the trash value as the correct value?

I pressed 1 and entered information related to students. Then press 2 to get the calculations that fit the information I entered, but the trash values come out.

#include <iostream>
#include <cstring>
using namespace std;

struct Subject {    //과목 정보
    char Subname[30];      //과목이름
    int Hakjum;            //과목학점
    char Grade[10];        //과목등급
    float GPA;             //과목평점
};

struct Student {    //학생정보
    char StdName[30];      //학생이름
    int Hakbun;            //학번
    Subject Sub[3];        //과목
    float AveGPA;          //교과목 평균 평점
};

int main()
{
    cout.precision(2);
    cout << fixed;

    int n = 0;
    struct Student Stu[10];  //구조체변수 선언

    while (n < 3)                               //반복문을 통해 1또는 2를 눌렀을 때 메뉴판으로 돌아오기
    {
        cout << "===== 메뉴 =====\n";            //메뉴판
        cout << "1. 학생 성적 입력\n";
        cout << "2. 전체 학생 성적 보기\n";
        cout << "3. 프로그램 종료\n";
        cout << "\n";
        cout << "원하는 기능을 입력하세요 : ";
        cin >> n;


        if (n <= 1) {                            //1번 선택하였을 경우
            for (int t = 0; t <= 1; t++)
            {
                cout << "*" << t + 1 << " 번째 학생 이름과 학번을 입력하세요.\n";     //이름과 학번을 입력 받음
                cout << "이름 : ";
                cin >> Stu[t].StdName;
                cout << "학번 : ";
                cin >> Stu[t].Hakbun;
                cout << "\n";
                cout << "\n";
                cout << "* 수강한 과목3개와 각 교과목명, 과목학점, 과목등급을 입력하세요.\n";     //과목과, 학점, 등급을 입력받음
                for (int a = 0; a < 3; a++)
                {
                    cout << "교과목명 : ";
                    cin >> Stu[t].Sub[a].Subname;
                    cout << "과목학점수 : ";
                    cin >> Stu[t].Sub[a].Hakjum;
                    cout << "과목등급<A+ ~ F> : ";
                    cin >> Stu[t].Sub[a].Grade;

                    cout << "\n";

                }
                cout << "\n\n\n";
            }


        }

        else if (n <= 2) {                                              //2번 선택하였을 경우
            cout << "\n\t\t전체 학생 성적 보기\n";
            cout << "===================================================================\n";
            for (int t = 0; t <= 1; t++) {
                cout << "이름 : " << Stu[t].StdName << "\t학번 : " << Stu[t].Hakbun << "\n";          //학생의 개인정보 출력
                cout << "===================================================================\n";
                cout << "\t\t과목명 \t    과목학점 \t과목등급 \t과목평점\n";
                cout << "===================================================================\n";
                for (int a = 0; a < 3; a++) {                                   //문자열 비교 연산자를 사용하여 등급을 평점으로 바꿈

                    if (Stu[t].Sub[a].Grade, "A+" == 0) {
                        Stu[t].Sub[a].GPA = 4.5 * Stu[t].Sub[a].Hakjum;
                    }

                    else if (Stu[t].Sub[a].Grade, "A0" == 0) {
                        Stu[t].Sub[a].GPA = 4.0 * Stu[t].Sub[a].Hakjum;
                    }

                    else if (Stu[t].Sub[a].Grade, "B+" == 0) {
                        Stu[t].Sub[a].GPA = 3.5 * Stu[t].Sub[a].Hakjum;
                    }

                    else if (Stu[t].Sub[a].Grade, "B0" == 0) {
                        Stu[t].Sub[a].GPA = 3.0 * Stu[t].Sub[a].Hakjum;
                    }

                    else if (Stu[t].Sub[a].Grade, "C+" == 0) {
                        Stu[t].Sub[a].GPA = 2.5 * Stu[t].Sub[a].Hakjum;
                    }

                    else if (Stu[t].Sub[a].Grade, "C0" == 0) {
                        Stu[t].Sub[a].GPA = 2.0 * Stu[t].Sub[a].Hakjum;
                    }

                    else if (Stu[t].Sub[a].Grade, "D+" == 0) {
                        Stu[t].Sub[a].GPA = 1.5 * Stu[t].Sub[a].Hakjum;
                    }

                    else if (Stu[t].Sub[a].Grade, "D0" == 0) {
                        Stu[t].Sub[a].GPA = 1.0 * Stu[t].Sub[a].Hakjum;
                    }

                    else if (Stu[t].Sub[a].Grade, "F" == 0) {
                        Stu[t].Sub[a].GPA = 0.0 * Stu[t].Sub[a].Hakjum;
                    }

                    Stu[t].AveGPA = (Stu[t].Sub[0].GPA + Stu[t].Sub[1].GPA + Stu[t].Sub[2].GPA) / (Stu[t].Sub[0].Hakjum + Stu[t].Sub[1].Hakjum + Stu[t].Sub[2].Hakjum);    //평균 평점을 구함


                    cout << "\t\t" << Stu[t].Sub[a].Subname << "\t\t" << Stu[t].Sub[a].Hakjum << "\t   " << Stu[t].Sub[a].Grade << "\t\t   " << Stu[t].Sub[a].GPA << "\n";  //과목명, 과목학점, 과목등급, 과목평점 출력
                }
                cout << "===================================================================\n";
                cout << "\t\t\t\t평균평점\t" << Stu[t].AveGPA << "\n\n";
            }
        }

    }
    return 0;
}

The output

enter image description here

I get a trash value in the "subject grade".

Expected

I want it to be calculated according to the number I enter.

I hope the "과목평점" is right.

c++


Solution 1:[1]

This is garbage:

                if (Stu[t].Sub[a].Grade, "A+" == 0) {

What it does is use comma operator. So first it will get Stu[t].Sub[a].Grade and... do nothing with it. Then it will get "A+" == 0, which is comparing string literal (const char* pointer) to 0, which is same as nullptr, which is false because string literal is not going to be nullptr. So this condition is always false. It's the last experssion with the comman operator, so this false result will be used for the if. Same for the rest of them.


It should be

                if (std::strcmp(Stu[t].Sub[a].Grade, "A+") == 0) {

The function std::strcmp is declared in cstring header, which you already have included, and since you have using namespace std there, you can leave the std:: out if you feel confident you know std:: namespace functions well enough.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1