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');
}
|