Calling a Function with arrays

I don't know why my program isn't working. Can anyone help me?

Write your question here.
Write a program that will input letter grades (A, B, C, D, F), the number of
which is input by the user (a maximum of 50 grades). The grades will be read
into an array. A function will be called five times (once for each letter grade)and will return the total number of grades in that category. The input to the function will include the array, number of elements in the array and the letter category (A, B, C, D or F). The program will print the number of grades that are A, B, etc.
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
#include <iostream>
using namespace std;
int countGrade(char letterCategory, int size, char array[]);
const int LENGTH= 5;
char letterCategory[LENGTH] = {'A', 'B', 'C', 'D', 'F'};
char userLetters[50];
int main()
{
    int numberOfGrades = 0;
    int gradeNumbersA = countGrade('A', numberOfGrades, userLetters);
    int gradeNumbersB = countGrade('B', numberOfGrades, userLetters);
    int gradeNumbersC = countGrade('C', numberOfGrades, userLetters);
    int gradeNumbersD = countGrade('D', numberOfGrades, userLetters);
    int gradeNumbersF = countGrade('F', numberOfGrades, userLetters);
    
    cout << "Please input the number of grades to be read in. (1-50): ";
    cin >> numberOfGrades;
    
    if(numberOfGrades < 1 || numberOfGrades > 50)
    {
        cout << "Error! Invalid Input. Please enter a number between 1 and 50.\n";
        cin >> numberOfGrades;
    }
    while(numberOfGrades < 1 || numberOfGrades > 50);
    cout << "All grades must be upper case A B C D or F.\n";
    
    {
        for(int i = 0; i < numberOfGrades; i++)
        {
            cout << "Input a grade: ";
            cin >> userLetters[i];
        }
    }
    cout << "Number of A: " << gradeNumbersA << endl;
    cout << "Number of B: " << gradeNumbersB << endl;
    cout << "Number of C: " << gradeNumbersC << endl;
    cout << "Number of D: " << gradeNumbersD << endl;
    cout << "Number of F: " << gradeNumbersF << endl;
    return 0;
}
int countGrade(char letterCategory, int size, char array[]) {
    int total = 0;
    for (int count = 0; count < size; count++) {
        if (array[count] == letterCategory) {
            total++;
        }
    }
    return total;
}

hello create some space between line 33 and 34 then move line 10 to 14 to the newly created space, it should work.
Your program begins by calculating the number of grades A, B, C, D, F in an empty array. So all zero.

Then your program has the user enter some values.

Then your program outputs those calculations you did at the start, before the user entered any data.

See anything odd about that?
THANKS YOLANDA
Hello cash,

Lines 10 - 14 are calling function using an uninitialized array that has no values stored in it except what might be there when the array was first created. These function calls need to be done after the while loop to enter grades.

That is what I see for now. You could make use of the enter key more often and break up the code for easier reading. I have more in about an hour or so.

Hope that helps,

Andy
hello CASH, i can see errors in your code, line 19 - 25, what are they achieving?

thought i could help clear your code.

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
#include <iostream>
#include <cctype>

int countGrade(char letterCategory, int size, char array[]);
bool valid_grade(const char&);

int main()
{
    char userLetters[50]{};///avoid declaring variables in global scope.
    int numberOfGrades = 0;

    int gradeNumbersA{},gradeNumbersB{},gradeNumbersC{},gradeNumbersD{},gradeNumbersF{};///init variables as ANDY SUGGESTED.

    std::cout << "Please input the number of grades to be read in. (1-50): ";
    std::cin >> numberOfGrades;

    while(numberOfGrades < 1 || numberOfGrades > 50) /// the loop you needed.
    {
        std::cout << "Error! Invalid Input. Please enter a number between 1 and 50.\n";
        std::cin  >> numberOfGrades;
    }

    std::cout << "Enter a grade, any in the set {A,B,C,D and F}.\n";

    for(int i = 0; i < numberOfGrades; i++)
     {
         char Letter= ' ';
         std::cout << "Input a grade: ";
         std::cin  >> Letter;

         while(!valid_grade(Letter))///check if input is correct
         {
           std::cout << "Error, Enter a valid grade: ";
           std::cin  >> Letter;
         }

         userLetters[i] = Letter;
     }

    gradeNumbersA = countGrade('A', numberOfGrades, userLetters);
    gradeNumbersB = countGrade('B', numberOfGrades, userLetters);
    gradeNumbersC = countGrade('C', numberOfGrades, userLetters);
    gradeNumbersD = countGrade('D', numberOfGrades, userLetters);
    gradeNumbersF = countGrade('F', numberOfGrades, userLetters);


    std::cout << "Number of A: " << gradeNumbersA << std::endl;
    std::cout << "Number of B: " << gradeNumbersB << std::endl;
    std::cout << "Number of C: " << gradeNumbersC << std::endl;
    std::cout << "Number of D: " << gradeNumbersD << std::endl;
    std::cout << "Number of F: " << gradeNumbersF << std::endl;

    return 0;
}

int countGrade(char letterCategory, int size, char array[]) {
    int total = 0;
    for (int count = 0; count < size; count++) {
        if (array[count] == letterCategory) {
            total++;
        }
    }
    return total;
}

bool valid_grade(const char& c)
{
   char temp = std::toupper(c);
   return (temp=='A' || temp == 'B' || temp == 'C' || temp == 'D' || temp == 'F');
}
Topic archived. No new replies allowed.