Gpa Analyzer need to add grade values in very much a beginner

Hello i am trying to add in the range of the Grades like A = 90-100 B= 80-89 etc. Sorry for the question i am struggling here, and i know it is something simple.


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88





#include <iostream>

class Grades
{
  public:
    Grades()
    {
    }

    void prompt_id()
    {
      std::cout << "Welcome to GPA analyzer" << std::endl;
      while(!valid_id(student_id))
      {
        std::cout << "Enter student ID:    ";
        std::cin >> student_id;
      }
    }

    void prompt_scores()
    {
      int* temp_list = new int[num_grades + 1];
      for (int c = 0; c < num_grades; ++c)
        temp_list[c] = grades_list[c];
      if(num_grades > 0)
        delete[] grades_list;
      grades_list = temp_list;

      while(true)
      {
        std::cout << "Enter score:    ";
        int score;
        std::cin >> score;
        if (std::cin.fail())
          return;
        while(!(score >= 0 && score <= 100))
        {
          std::cout << "Invalid score" << std::endl;
          std::cout << "Enter score:    ";
          std::cin >> score;
        }
        grades_list[num_grades] = score;
        ++num_grades;
      }
    }

    void print_scores()
    {
      std::cout << "#       Score   Grade" << std::endl;
      for(int i = 0; i < num_grades; ++i)
        std::cout<< i << "       " << grades_list[i] << "      " << "put grade here" << std::endl;
    }
    
  private:
    static bool valid_id(int id)
    {
      if (id > 99999999 || id < 10000000)
        return false;
      if (id % 2 == 1)
        return false;
      if (!(id / 100000 == 127 || id / 100000 == 379 || id / 100000 == 833))
        return false;
      return true;
    }

    int* grades_list;
    int num_grades = 0;
    int student_id = 0;

};

int main()
{
  Grades grades;
  grades.prompt_id();
  grades.prompt_scores();
  grades.print_scores();

  if 
  num_grades (0 > )

  return 0;
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
#include <iostream>

struct Grades
{
    static Grades get_id_and_scores_from_stdin()
    {
        Grades g ;
        g.get_id_from_stdin() ;
        g.get_scores_from_stdin() ;
        return g ;
    }

    void print_grades() const
    {
        std::cout << "\nstudent id: " << student_id << "\nscores: \n" ;
        for( int i = 0 ; i < NUM_SCORES ; ++i )
        {
            std::cout << "    " << i+1 << ". " << score_list[i]
                      << "  " << score_to_grade( score_list[i] ) << '\n' ;
        }
    }

    private:

        static constexpr int NUM_SCORES = 5 ;
        int student_id = 0 ;
        int score_list[NUM_SCORES] = {0} ;

        static bool valid_id(int id) { /* if a check fails return false */ return true ; }
        static bool valid_score( int score ) { return score >= 0 && score < 101 ; }

        static char score_to_grade( int score )
        {
            if( score > 89 ) return 'A' ;
            else if( score > 79 ) return 'B' ;
            else if( score > 69 ) return 'C' ;
            else if( score > 59 ) return 'D' ;
            else if( score > 49 ) return 'E' ;
            else return 'F' ;
        }

        void get_id_from_stdin()
        {
            do
            {
                std::cout << "enter student id: " ;
                std::cin >> student_id ;
            }
            while( !valid_id(student_id) ) ;
        }

        void get_scores_from_stdin()
        {
            for( int i = 0 ; i < NUM_SCORES ; ++i )
            {
                do
                {
                    std::cout << "enter score [0-100] #" << i+1 << ": " ;
                    std::cin >> score_list[i] ;
                }
                while( !valid_score( score_list[i] ) ) ;
            }
        }
};

int main()
{
    const Grades g = Grades::get_id_and_scores_from_stdin() ;
    g.print_grades() ;
}
Topic archived. No new replies allowed.