felipedrivas
felipedrivas

Reputation: 23

C++ Trying to create Gradebook with user input using arrays

I wanted to

  1. Create the class Student. Each student has a name and a grade. Include appropriate member functions to manipulate student objects.
  2. Replace the grades data member with students data member, which is an array of students of Student class. That is, you need to change 1) the type of the array from integer to student, and 2) the name of the array from grades to students.
  3. Make GradeBook class and all its member functions work for the new type of array.**

Gradebook.cpp

#include<iostream>
    #include<string>
    #include"Gradebook.h"
    #include<array>
    using namespace std;

   int main()
{
string courseName;

cout <<"Enter course name:";
cin >> courseName;
cout << "===============================Entering student information===============================" << endl;
cout <<"Enter the name and grade for 10 students"<<endl;
array<int, 10> studentGrades{ 87, 68, 94, 100, 83, 78, 85, 91, 76, 87 };
    GradeBook myGradeBook(courseName,studentGrades);

    myGradeBook.setCourseName(courseName);

    myGradeBook.processGrades();


}


    

Gradebook.h

#pragma once

    #include<string>
    #include<array>

    class GradeBook {
    public:

GradeBook(std::string& cName,std::array<int,10>& sGrades) :
    courseName{ cName }, studentGrades{ sGrades } {

}

std::string getCourseName() const {
    return courseName;
}

void setCourseName(const std::string& cName) {
    courseName = cName;

}

void processGrades() const {
    outputGrades();
    std::cout << "\nClass average: " << getAverage() << std::endl;
    std::cout << "\nClass maximum: " << getMaximum() << std::endl;
    std::cout << "\nClass minimum: " << getMinimum() << std::endl;

    std::cout << "Bar Chart:\n";
    outputBarChart();
}

int getMaximum() const {
    int highGrade{ 0 };

    //range-based for loop
    for (int grade : studentGrades) {
        if (highGrade < grade) {
            highGrade = grade;
        }
    }
enter code here
    return highGrade;
}

int getMinimum() const {
    int lowGrade{ 100 };

    for (int grade : studentGrades) {
        if (lowGrade > grade) {
            lowGrade = grade;
        }
    }
    return lowGrade;
}

double getAverage() const {
    int sum{ 0 };
    for (int grade : studentGrades) {
        sum += grade;
    }
    return static_cast<double>(sum) / studentGrades.size();

}

void outputGrades() const {
    std::cout << "\n The grades are: \n\n";

    for (size_t i{ 0 }; i < studentGrades.size(); ++i)
    {
        std::cout <<"Student #"<< i + 1 << " grade: "<< studentGrades.at(i) << std::endl;
    }

}
void outputBarChart() const {
    std::cout << "\nGrade distribution:\n";
    std::array<int, 11> frequency{};

    for (int grade : studentGrades) {
        ++frequency[grade / 10];
    }

    for (size_t i{ 0 }; i < frequency.size(); ++i)
    {
        if (i == 0) {
            std::cout << "  0-9:";
        }
        else if (i == 10) {
            std::cout << "  100:";
        }
        else {
                std::cout << i * 10 << "-" << (i*10) + 9 << ":";
        }

        for (unsigned stars{ 0 }; stars < frequency[i]; ++stars) {
            std::cout << '*';
        }

       std::cout << std::endl;
        
    }

}

    private:
std::string courseName;
std::array<int, 10> studentGrades;
std::array<int, 10> studentNames;
    };

Desired Output

Upvotes: 0

Views: 1302

Answers (1)

Thomas Matthews
Thomas Matthews

Reputation: 57678

The secret is in the encapsulation of functionality in the Student class:

class Student
{
  std::string m_name;
  std::vector<int> m_grades;
  public:
    void input_with_prompt();
};

void Student::input_with_prompt()
{
  std::cout << "Enter Student Name: ";
  std::getline(std::cin, name);
  std::cout << "Enter 10 grades:\n";
  for (int i = 0; i < 10; ++i)
  {
     int grade;
     std::cin >> grade;
     m_grades.push_back(grade);
  }
}

You could use this in the main function or elsewhere as:

Student s;
s.input_with_prompt();

You can also augment the Student class with min, max, and avg methods. This will help out with the Grade Book class:

std::array<Student, 10> grade_book;
Student s;
for (int i = 0; i < 10; ++i)
{
  s.input_with_prompt();
  grade_book[i] = s;
}
int min = grade_book[0].minimum_grade();
for (int i = 0; i < 10; ++i)
{
  int min_grade = grade_book[i].minimum_grade();
  if (min > min_grade) min = min_grade;
}

The maximum and average functionalities can be implemented in a similar manner. This is one implementation. There are many others.

Upvotes: 0

Related Questions