Random Grade Generator(while, for loops, if else if)

Hi. I am a beginner to c++, and am taking an introductory class. I was tasked with making a program that calculates random numbers for students in a class, and displays the letter grades in total of all the students. Everything is working fine except one part, where i have a for loop nested inside a while loop. The for loop is supposed to generate ten random numbers and get the average, then display it for each individual student. However it isn't doing that. Suggestions?

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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
  #include <iostream>
#include <ctime>
#include <cstdlib>

using namespace std;

int main()
{
    //VARIABLES
    int studentNum, finExam, midterm, progEx; //HOLD VALUES FOR NUMBER OF STUDENTS AND GRADE CATEGORIES
    double progExAvg,
           numericGrade;  //HOLDS VALUE FOR NUMERIC GRADE OF EACH STUDENT


    //counters
    int totalProgEx = 0;  //ACCUMULATOR FOR AVERAGE OF TOTAL PROGRAMMING EXCERCISES
    int counter = 0;      //COUNTER FOR NUMBER OF STUDENTS
    int countA=0,         //COUNTERS FOR EACH LETTER GRADE
        countB=0,
        countC=0,
        countD=0,
        countF=0;

    //RANDOM GENERATOR FUNCTION
    srand( time(0));      //This will ensure a really randomized number by help of time.


    //USER PROMPT
    cout << "Hello. Welcome to Random Grade generator." << endl;
    cout << "Please enter how many students are in the class: " << endl;
    cin >> studentNum;



    // COMPUTES RANDOM GRADES
    while(counter < studentNum)
    {
      //COMPUTES RANDOM GRADES FOR TEN PROGRAMMING EXCERCISES
      for (int i=0; i< 10; i++)
        {
          progEx  = rand()%100+1; // Randomizing the number between 1-100.


        }
      totalProgEx = totalProgEx + progEx;
      progExAvg = totalProgEx/10;
      midterm = rand()%100+1; // Randomizing the number between 1-100.
      finExam = rand()%100+1; // Randomizing the number between 1-100.



      counter++;


      // DISPLAYS GRADES
      cout << "\nStudent " << counter << " Grades\n" << endl;
      cout << "Grade Average for 10 Programming Exercises: " << progExAvg << endl;

      cout << "Grade for Midterm: " << midterm << endl;

      cout << "Grade for Final exam: " << finExam << endl;


      //COMPUTES NUMERIC GRADE
      numericGrade = (progEx *.70) + (midterm*.15) + (finExam*.15);


        //COMPUTES RANGES FOR LETTER GRADES


      if ((numericGrade >= 0) && (numericGrade <= 59))
                {
                    countF++;
                }
      else if ((numericGrade >=60) && (numericGrade <= 69))
                {
                    countD++;
                }
      else if ((numericGrade >= 70) && (numericGrade <= 79))
                {
                    countC++;
                }
      else if ((numericGrade >= 80) && (numericGrade <=89))
                {
                    countB++;
                }
      else if ((numericGrade >=100) && (numericGrade <=90))
                {
                    countA++;
                }









    }
    // display class performance
    cout << "\n\nNumber of students who received each letter grade:\n " << endl;
    cout << countA << " A" << endl;
    cout << countB << " B" << endl;
    cout << countC << " C" << endl;
    cout << countD << " D" << endl;
    cout << countF << " F" << endl;












    return 0;
}
1
2
3
4
5
6
7
8
9
      //COMPUTES RANDOM GRADES FOR TEN PROGRAMMING EXCERCISES
      totalProgEx = 0;
      for (int i=0; i< 10; i++)
        {
          progEx  = rand()%100+1; // Randomizing the number between 1-100.
          totalProgEx = totalProgEx + progEx;

        }
      totalProgEx = totalProgEx + progEx;

Additionally:
else if ((numericGrade >=100) && (numericGrade <=90)) //←WAT?
In addition to what MiiNiPaa pointed out:

Where did you store the programming exercise average?

1
2
3
4
5
6
7
8
9
10
      //COMPUTES RANDOM GRADES FOR TEN PROGRAMMING EXCERCISES
      for (int i=0; i< 10; i++)
        {
          progEx  = rand()%100+1; // Randomizing the number between 1-100.


        }
      totalProgEx = totalProgEx + progEx;
      progExAvg = totalProgEx/10;
 


How did you calculate the numeric grade?
1
2
3
      //COMPUTES NUMERIC GRADE
      numericGrade = (progEx *.70) + (midterm*.15) + (finExam*.15);

Thanks, i fixed the what MiiNiPaa pointed out and the numeric grade calculation, but i'm not sure what you mean by where did i store the programming exercise average pheininger. I'm still having the problem where it is not correctly computing the program exercises average per student.

Edit: Nvm, I get it now. Fixed. Thanks.
Last edited on
Topic archived. No new replies allowed.