c++ codeblocks function program help

Hello, I need big help with this program because I simply cannot figure out why it won't work properly. This is the assignment my professor wants us to do:

Use functions to determine the number of letter grades from a given
list of students’ number grades. The requirements are –
1) Initialize the variables, countGradeA, countGradeB, countGradeC,
countGradeD, countGradeF to 0.
2) Read a number grade.
3) If the number grade is in between 100-90, for example, then
increment the countGradeA count and so on and so forth for all other
grades. Letter grades are determined by the following –
90 – 100 A
80-89 B
70-79 C
60-69 D
0-59 F
4) Repeat Steps 2 and 3 for each number in the list.
5) Print out the results which shows the counts for each grade.
The following functions should be defined/implemented and called in
main() –
int getNumberOfStudents(); //make sure to validate number of students
void Initialize(int& countGradeA, int& countGradeB, int& countGradeC,
int& countGradeD, int& countGradeF);
void getNumberGrade(int& grade); //make sure to valid the grade.
void classifyGrades (int grade, int& countGradeA, int& countGradeB,
int& countGradeC, int& countGradeD, int& countGradeF);
void printResults(int countGradeA, int countGradeB, int countGradeC,
int countGradeD, int countGradeF);



Seems simple enough yet it will not output the number of students I input when asked to. For example if I input 4 students and then enter a grade like the program stated to do, it will only spit back one student's grade and not the remanding three.

Here is my code, I USE CODEBLOCKS

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

int numberofstudents();
void initialize(int& gradeCountA, int& gradeCountB, int& gradeCountC, int& gradeCountD, int& gradeCountF);
void getnumbergrade(int& grade);
void classifygrades (int& grade, int& gradeCountA, int& gradeCountB, int& gradeCountC, int& gradeCountD, int& gradeCountF);
void printresults (int gradeCountA, int gradeCountB, int gradeCountC, int gradeCountD, int gradeCountF);



int main(){
int gradeCountA, gradeCountB, gradeCountC, gradeCountD, gradeCountF;
int students;
int grade;
numberofstudents();
initialize(gradeCountA, gradeCountB, gradeCountC, gradeCountD, gradeCountF);
getnumbergrade(grade);
classifygrades(grade, gradeCountA, gradeCountB, gradeCountC, gradeCountD, gradeCountF);
printresults (gradeCountA, gradeCountB, gradeCountC, gradeCountD, gradeCountF);
}
int numberofstudents(){
int students;
int grade;
cout << "Enter number of students: ";
cin >> students;

return 0;
}
void initialize(int& gradeCountA, int& gradeCountB, int& gradeCountC, int& gradeCountD, int& gradeCountF)
{
gradeCountA = 0;
gradeCountB = 0;
gradeCountC = 0;
gradeCountD = 0;
gradeCountF = 0;
}

void getnumbergrade(int& grade)
{
int students;
for(int i = 0; i < grade; i++);
{
cout << "Enter a student's grade: ";
cin >> grade;
if (grade < 0 || grade > 100){
cout << "Invalid grade. Please re-enter: ";


}
}
}

void classifygrades (int& grade, int& gradeCountA, int& gradeCountB, int& gradeCountC, int& gradeCountD, int& gradeCountF)
{
if ((grade >= 90) && (grade <= 100))
gradeCountA++;
else if ((grade >= 80) && (grade <= 89))
gradeCountB++;
else if ((grade >=70) && (grade <= 79))
gradeCountC++;
else if ((grade >= 60) && (grade <= 69))
gradeCountD++;
else
gradeCountF++;
}
void printresults(int gradeCountA, int gradeCountB, int gradeCountC, int gradeCountD, int gradeCountF)
{
cout << "Total students received A: " << gradeCountA << endl;
cout << "Total students received B: " << gradeCountB << endl;
cout << "Total students received C: " << gradeCountC << endl;
cout << "Total students received D: " << gradeCountD << endl;
cout << "Total students received F: " << gradeCountF << endl;
}

numberofstudents is returning 0 right now - this should probably return the number entered in the variable students and this number saved in main. The number of students can control a loop in main to get a grade and classify a grade for however many students were indicated.

1
2
3
4
5
6
7
8
int numberofstudents(){
int students;
int grade;
cout << "Enter number of students: ";
cin >> students;

return 0;
}


You do have a loop here, but right now it's controlled by grade instead of student. I think the way the prototypes have been set up, this function should just get a single grade and validate it's within the correct range.

1
2
3
4
5
6
7
8
9
10
11
12
void getnumbergrade(int& grade)
{
int students;
for(int i = 0; i < grade; i++);
{
cout << "Enter a student's grade: ";
cin >> grade;
if (grade < 0 || grade > 100){
cout << "Invalid grade. Please re-enter: ";
}
}
}
I still can't figure out why it won't work. I've tried changing some things around in the functions like eliminating the for loop in the getnumbergrade function because I realized that I don't need it in there, but putting it in the numberofstudents function doesn't seem to make a difference. Do I need to maybe put a do while loop in the numberofstudents function?
Scratch that do while didn't work... can anyone else help? Am I missing something in the main function besides the extra functions I made? Am I inputting the for loop wrong? I'm inputting it as such: for(int i = 0; i < student; i++); but I feel that it is wrong.
numberofstudents returns an int - save this number in a variable in main and use it to control a loop to get and classify grades in main

1
2
3
4
5
6
7
main
  initialize // initialize counters to 0
  students = numberofstudents() // get number of students, save in main 
  loop on students // for each student, do the following
    getnumbergrade // get the grade
    classifygrades // increment the appropriate grade counter
  printresults
closed account (48T7M4Gy)
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
#include <iostream>
#include <string>

using namespace std;

int numberofstudents();
void initialize(int&, int&, int&, int&, int&);
int getnumbergrade(); // <--
void classifygrades (int&, int&, int&, int&, int&, int&);
void printresults (int, int, int, int, int);

int main(){
    int gradeCountA, gradeCountB, gradeCountC, gradeCountD, gradeCountF;
    initialize(gradeCountA, gradeCountB, gradeCountC, gradeCountD, gradeCountF);
    
    int students = numberofstudents();
    
    int grade = 0;
    for (int i = 0; i < students; ++i)
    {
        grade = getnumbergrade();
        classifygrades(grade, gradeCountA, gradeCountB, gradeCountC,
                       gradeCountD, gradeCountF);
    }
    
    printresults (gradeCountA, gradeCountB, gradeCountC,
                  gradeCountD, gradeCountF);
    
    return 0;
}

int numberofstudents(){
    int students;
    cout << "Enter number of students: ";
    cin >> students;
    return students;
}

void initialize(int& gradeCountA, int& gradeCountB, int& gradeCountC,
                int& gradeCountD, int& gradeCountF)
{
    gradeCountA = 0;
    gradeCountB = 0;
    gradeCountC = 0;
    gradeCountD = 0;
    gradeCountF = 0;
}

int getnumbergrade()
{
    int aGrade = 0;
    cout << "Enter a student's grade: ";
    cin >> aGrade;
    if (aGrade < 0 || aGrade > 100){
        cout << "Invalid grade. Please re-enter: ";
    }
    return aGrade;
}


void classifygrades (int& grade, int& gradeCountA, int& gradeCountB,
                     int& gradeCountC, int& gradeCountD, int& gradeCountF)
{
    if ((grade >= 90) && (grade <= 100))
        gradeCountA++;
    else if ((grade >= 80) && (grade <= 89))
        gradeCountB++;
    else if ((grade >=70) && (grade <= 79))
        gradeCountC++;
    else if ((grade >= 60) && (grade <= 69))
        gradeCountD++;
    else
        gradeCountF++;
}
void printresults(int gradeCountA, int gradeCountB, int gradeCountC, 
int gradeCountD, int gradeCountF)
{
    cout << "Total students received A: " << gradeCountA << endl;
    cout << "Total students received B: " << gradeCountB << endl;
    cout << "Total students received C: " << gradeCountC << endl;
    cout << "Total students received D: " << gradeCountD << endl;
    cout << "Total students received F: " << gradeCountF << endl;
}
Enter number of students: 5
Enter a student's grade: 23
Enter a student's grade: 99
Enter a student's grade: 78
Enter a student's grade: 56
Enter a student's grade: 89
Total students received A: 1
Total students received B: 1
Total students received C: 1
Total students received D: 0
Total students received F: 2
Program ended with exit code: 0
Last edited on
Please, use the code tags. It will make your code much more readable to others.

Like this:

[code] code here... [/code]

results in:

 
code here...
Abrogna22 wrote:
1
2
3
4
5
6
7
8
void initialize(int& gradeCountA, int& gradeCountB, int& gradeCountC, int& gradeCountD, int& gradeCountF)
{
    gradeCountA = 0;
    gradeCountB = 0;
    gradeCountC = 0;
    gradeCountD = 0;
    gradeCountF = 0;
}

Instead of having a function to initialize variables like this, you should initialize using what's provided by the core C++ language.
1
2
3
4
5
6
7
8
9
10
int gradeCountA = 0, gradeCountB = 0, gradeCountC = 0, gradeCountD = 0, gradeCountF = 0;
// or
int gradeCountA(0), gradeCountB(0), gradeCountC(0), gradeCountD(0), gradeCountF(0);
// or
int gradeCountA{0}, gradeCountB{0}, gradeCountC{0}, gradeCountD{0}, gradeCountF{0};
// or
int gradeCountA = { 0 }, gradeCountB = { 0 }, gradeCountC = { 0 }, gradeCountD = { 0 }, gradeCountF = { 0 };
// or
int gradeCountA = int(0), gradeCountB = int(0), gradeCountC = int(0), gradeCountD = int(0), gradeCountF = int(0);
// more ways... google "c++11 ways to initialize variables" 
Instead of having a function to initialize variables like this, you should initialize using what's provided by the core C++ language.


I'd usually do that too, but this assignment seems to require a specific initialize function.

1
2
3
4
5
6
7
8
9
10
The following functions should be defined/implemented and called in
main() –
int getNumberOfStudents(); //make sure to validate number of students
void Initialize(int& countGradeA, int& countGradeB, int& countGradeC,
int& countGradeD, int& countGradeF);
void getNumberGrade(int& grade); //make sure to valid the grade.
void classifyGrades (int grade, int& countGradeA, int& countGradeB,
int& countGradeC, int& countGradeD, int& countGradeF);
void printResults(int countGradeA, int countGradeB, int countGradeC,
int countGradeD, int countGradeF);
Thanks everyone for their help! I greatly appreciate it!

According to Kemort's post line 8 I had as a void function instead of an int function. That fixed my problem right away. Along with a few other variations he inputted in the main function. Guess I'll have to let my teacher know void doesnt work when getting grades and int does work.

Thanks everyone again for their help! Cheers!
A void function can work if you're passing the grade variable by reference.
closed account (48T7M4Gy)
It obviously works both ways and because the problems specfies pass by reference the void option should be used, my bad.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
void getnumbergrade(int&); // <--

int main(){
    int grade = 0;
    for (int i = 0; i < students; ++i)
    {
        getnumbergrade(grade); // <--
        classifygrades(grade, gradeCountA, gradeCountB, gradeCountC,
                       gradeCountD, gradeCountF);
    }
}

void getnumbergrade(int &aGrade)
{
    cout << "Enter a student's grade: ";
    cin >> aGrade;
    if (aGrade < 0 || aGrade > 100){
        cout << "Invalid grade. Please re-enter: ";
    }
}


Enter number of students: 5
Enter a student's grade: 23
Enter a student's grade: 99
Enter a student's grade: 78
Enter a student's grade: 56
Enter a student's grade: 89
Total students received A: 1
Total students received B: 1
Total students received C: 1
Total students received D: 0
Total students received F: 2
Program ended with exit code: 0
Last edited on
Usually she tells us when she wants to do call by reference or not. In this assignment she didn't specify thus leading int to work instead.
Topic archived. No new replies allowed.