Write a complete C++ program called example.cpp that uses a while loop to read i
ID: 669596 • Letter: W
Question
Write a complete C++ program called example.cpp that uses a while loop to read
in and count the number of letter grades for a class. For example, the program
would ask the user to enter a valid letter grade: A, B, C, D, or F. Then the program
would keep track of the number of A’s, B’s, C’s, D’s, and F’s that were entered until
either the user indicates he/she is done entering grades (i.e., a prompt to ask the
user if he/she wants to enter another grade) or use of a sentinel value (i.e., the user
enters a predetermined invalid letter grade that signifies the user is done). You may
assume that the user enters valid grades for the number used in the counts and
perhaps a non-valid grade for the sentinel value or in response to the prompt for
another grade. Then, you will print out the number of A’s, B’s, C’s, D’s, and F’s that
were entered.
Explanation / Answer
#include <iostream>
using namespace std;
int main()
{
char grade;
int a[]={0,0,0,0,0};
cout<<"enter a valid letter grade: A, B, C, D, or F OR Q to quit:"<<endl;
cin>>grade;
while(grade!='Q'){
if(grade=='A'){
a[0]++;
}else if(grade=='B'){
a[1]++;
}else if(grade=='C'){
a[2]++;
}else if(grade=='D'){
a[3]++;
}else if(grade=='F'){
a[4]++;
}else if(grade=='Q'){
break;
}
else{
cout<<"INVALID ENTRY:";
}
cout<<"enter a valid letter grade: A, B, C, D, or F OR Q to quit:"<<endl;
cin>>grade;
}
cout<<"Number of A grades: "<<a[0]<<endl;
cout<<"Number of B grades: "<<a[1]<<endl;
cout<<"Number of C grades: "<<a[2]<<endl;
cout<<"Number of D grades: "<<a[3]<<endl;
cout<<"Number of F grades: "<<a[4]<<endl;
return 0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.