I am having trouble developing this program. I know it needs to use a nested if
ID: 3762655 • Letter: I
Question
I am having trouble developing this program. I know it needs to use a nested if statement in a for loop. It will rely upon a parralell array. Can somebody please help? The requirements are below.
Create a program that will accept any number of grades for an exam. The grades will be input as 4 for an A, 3 for a B, 2 for a C, 1 for a D, and 0 for an F. If the user enters an invalid number, reject it and ask again. After all grades have been entered, allow the user to enter -1 to exit. Output the number of grades in each category by using a loop and a parallel array (for the grade names). Do not use the switch command in C++, you are practicing the nested if statement.
Thanks
Explanation / Answer
This Program can be solved without array as follows:
#include<iostream>
using namespace std;
bool findElement(int nums[], int element, int numssize)
{
for(int x = 0; x < numssize; x++)
{
if (nums[x] = element)
{
return 1;
}
}
return 0;
}
int main() {
int a=0,b=0,c=0,d=0,f=0;
bool acc=true;
while (acc==true)
{
cout <<"Enter a grade ";
string grade;
cin >> grade;
if (grade=="4")
{
a=a+1;
}
else if (grade=="3")
{
b=b+1;
}
else if (grade=="2")
{
c=c+1;
}
else if (grade=="1")
{
d=d+1;
}
else if (grade=="0")
{
f=f+1;
}
else if (grade=="-1")
{
acc=false;
}
else
{
cout << "Invalid grade, please re-enter ";
}
}
cout << "Count of Grade A: " << a;
cout << " Count of Grade B: " << b;
cout << " Count of Grade C: " << c;
cout << " Count of Grade D: " << d;
cout << " Count of Grade F: " << f;
}
In the above code replace string with int to accept only int, string is safe since it wont throw an error if user enters a non number.
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.