Search code examples
c++arraysobjectstructure

C++ Structure issues passing variable


This assignment requires me to create a program that takes in student info, firstname lastname and grade on test. I then have to assign the correct letter grade. Then I must output the student's name with the highest grade. Everything works except for the part with the highest grade. I know it is an issue with my "max" variable not getting updated from the findmax function. I just simply don't know why.

#include <iostream>
#include <string>
#include <iomanip>
using namespace std;

struct studentType {
    string studentFName;
    string studentLName;
    int testscore;
    char grade;
};
void getdata(studentType student[]);
void assignment(studentType student[]);
int findmax(studentType student[]);
void print(studentType student[], int max);

int main() {
    int max = 0;
    studentType student[3];
    getdata(student);
    assignment(student);
    findmax(student);
    print(student, max);
}

void getdata(studentType student[]) {
    for (int i = 0; i < 3; i++) {
        cout << "Enter Student First Name: " << endl;
        cin >> student[i].studentFName;
        cout << "Enter Student Last Name: " << endl;
        cin >> student[i].studentLName;
        cout << "Enter Student test score: " << endl;
        cin >> student[i].testscore;
        while (student[i].testscore > 100 || student[i].testscore < 0) {
            cout << "Please input a valid score in the range 0-100" << endl;
            cin >> student[i].testscore;
        }
    }
}
void assignment(studentType student[]) {
    for (int i = 0; i < 3; i++) {
        if (student[i].testscore >= 90 && student[i].testscore <= 100)
            student[i].grade = 'A';
        else if (student[i].testscore >= 80 && student[i].testscore < 90)
            student[i].grade = 'B';
        else if (student[i].testscore >= 70 && student[i].testscore < 80)
            student[i].grade = 'C';
        else if (student[i].testscore >= 60 && student[i].testscore < 70)
            student[i].grade = 'D';
        else if (student[i].testscore < 60)
            student[i].grade = 'F';
    }
    for (int j = 0; j < 3; j++)
        cout << student[j].studentLName << ", "<< setw(10) << student[j].studentFName << ", " << setw(10) << student[j].testscore << endl;
}

int findmax(studentType student[]) {
    int max = 0;
    for (int i = 0; i < 3; i++) {
        if (student[i].testscore > student[i - 1].testscore)
            max = student[i].testscore;
    }
    
    return max;
}


void print(studentType student[], int max) {
    for (int i = 0; i < 3; i++) {
        if (max == student[i].testscore)
            cout << student[i].studentFName << " " << student[i].studentLName << " had the highest score with " << student[i].testscore;

        }
    }

Solution

  • Your findmax() function is wrong. You are comparing student scores to each other, not to the max variable. Try this instead:

    int findmax(studentType student[]) {
        int max = 0;
        for (int i = 0; i < 3; i++) {
            if (student[i].testscore > max)
                max = student[i].testscore;
        }    
        return max;
    }
    

    Alternatively:

    int findmax(studentType student[]) {
        int max = student[0].testscore;
        for (int i = 1; i < 3; i++) {
            if (student[i].testscore > max)
                max = student[i].testscore;
        }    
        return max;
    }
    

    Either way, your main() is ignoring the returned value. you need to do this instead:

    int main() {
        studentType student[3];
        getdata(student);
        assignment(student);
        int max = findmax(student);
        print(student, max);
    }
    

    Personally, I would change findmax() to return an index instead of a score, and then get rid of the loop in print() altogether, eg:

    struct studentType {
        string studentFName;
        string studentLName;
        int testscore;
        char grade;
    };
    
    static const int MaxStudents = 3;
    
    void getdata(studentType students[]);
    void assignment(studentType students[]);
    int findmax(studentType students[]);
    void printMax(studentType &student);
    
    int main() {
        studentType students[MaxStudents];
        getdata(students);
        assignment(students);
        int index = findmax(students);
        printMax(students[index]);
    }
    
    void getdata(studentType students[]) {
        for (int i = 0; i < MaxStudents; ++i) {
            cout << "Enter Student First Name: " << endl;
            cin >> students[i].studentFName;
            cout << "Enter Student Last Name: " << endl;
            cin >> students[i].studentLName;
            cout << "Enter Student test score: " << endl;
            cin >> students[i].testscore;
            while (students[i].testscore > 100 || student[i].testscore < 0) {
                cout << "Please input a valid score in the range 0-100" << endl;
                cin >> students[i].testscore;
            }
        }
    }
    
    void assignment(studentType students[]) {
        for (int i = 0; i < MaxStudents; ++i) {
            if (students[i].testscore >= 90)
                students[i].grade = 'A';
            else if (student[i].testscore >= 80)
                students[i].grade = 'B';
            else if (student[i].testscore >= 70)
                students[i].grade = 'C';
            else if (student[i].testscore >= 60)
                students[i].grade = 'D';
            else
                students[i].grade = 'F';
        }
    
        for (int j = 0; j < MaxStudents; ++j)
            cout << students[j].studentLName << ", " << setw(10) << students[j].studentFName << ", " << setw(10) << students[j].testscore << endl;
    }
    
    int findmax(studentType students[]) {
        int index = 0;
        int max = students[index].testscore;
    
        for (int i = 1; i < MaxStudents; i++) {
            if (students[i].testscore > max) {
                index = i;
                max = students[index].testscore;
            }
        }
        
        return index;
    }
    
    void printMax(studentType &student) {
        cout << student.studentFName << " " << student.studentLName << " had the highest score with " << student.testscore;
    }