### Calling a Function with arrays

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

Write a program that will input letter grades (A, B, C, D, F), the number of
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.
 ``123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051`` ``````#include 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.

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.

 ``1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071`` ``````#include #include 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.