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).
#include <iostream>
using namespace std;
int main()
{
int grade;
cout << "Input your grade (0-4): ";
cin >> grade;
cout << endl;
if (grade == 4){
This is as far as i can figure out. I know it is lacking a lot but i am stumped. Please help i am the epitome of a N00B (new to coding). Thank you in advance!
Create a program that will accept any number of grades for an exam.... After all grades have been entered, allow the user to enter -1 to exit.
This tells me I need to create a loop to allow the user to input an undetermined number of grades until they input a sentinel value (-1). Since I don't know how many times I need to run the loop, I would use a while or do while loop, not a for loop.
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.
Once a user inputs a value for grade, I want to check if it's a valid input, >=-1 or <=4. If it's not valid, I want to re-prompt them. I might use a nested while loop to keep ask them to re-input.
Output the number of grades in each category by using a loop and a parallel array (for the grade names).
This tells me as the user enter grades, I need to keep track of how many of each grade type they're entering. It sounds like they want you to use two arrays, one to keep a count of each grade type and then one to hold the text that corresponds to the respective grade count.
1 2 3 4 5 6 7 8 9
//entry
prompt user to enter grade
while sentinel has not been entered
-- check for valid input value
-- while input valid is not valid, prompt user to re-enter grade
-- if grade entered, increment appropriate index of grade count array
-- ask user to enter another grade or -1 to quit
//output
-- for each index of the array, output the grade count and the corresponding grade text
Study this. I added comments to help in the understanding. This is not the most elegant code I've ever written but I tried to not confuse you with C++ language features you haven't learned yet.
#include <iostream>
usingnamespace std;
int main()
{
// create an array of numbers
int nNumberOfGrades [6] = {0, 0, 0, 0, 0, 0};
// I chose the size of 6 because that is the amount
// of grade types you are calculating A B C D E F
// six different possible values to store
// I intialized the array to 0 you could also use a loop to do this
// or another method
int input = 1; // the input varible
// creating an infinate loop that will only exit should the input -1 be entered
for ( ; ; )
{
cout << "Enter the Grade as a numeral " << endl;
cout << "0 = A\n"
<< "1 = B\n"
<< "2 = C\n"
<< "3 = D\n"
<< "4 = E\n"
<< "5 = F\n"
<< "Enter -1 when finished" << endl << endl;
cin >> input;
cout << endl;
// this is the statement that checks for a -1 value and if true
// the break statement will throw us out of loop
if (input == -1)
{
break;
}
// if the value is withing the correct values of a grade
// increment the number stored in the correct grade array
if (input > -1 && input < 6)
{
nNumberOfGrades [input]++;
}
// or inform user of mistake and let the loop continue
else
{
cout << "Invalid number. Try Again" << endl << endl;
}
}
int grade;
// another loop that loops through possible grades and displays the
// number of grades incremented
// in the respective grade slot
for (grade = 0; grade < 6; grade++)
{
if (grade == 0)
{
cout << "Number of grades : " << nNumberOfGrades [grade] << " Letter grade: A" << endl << endl;
}
elseif (grade == 1)
{
cout << "Number of grades :" << nNumberOfGrades [grade] << " Letter grade: B" << endl << endl;
}
elseif (grade == 2)
{
cout << "Number of grades :" << nNumberOfGrades [grade] << " Letter grade: C" << endl << endl;
}
elseif (grade == 3)
{
cout << "Number of grades :" << nNumberOfGrades [grade] << " Letter grade: D" << endl << endl;
}
elseif (grade == 4)
{
cout << "Number of grades :" << nNumberOfGrades [grade] << " Letter grade: E" << endl << endl;
}
elseif (grade == 5)
{
cout << "Number of grades :" << nNumberOfGrades [grade] << " Letter grade: F" << endl << endl;
}
}
system("pause");
return 0;
}
Right, you can do something like this to run a while loop
1 2 3 4 5 6 7 8
cout << "enter value (enter sentinel to quit): ";
cin >> value;
while (value!=sentinel)
{
//do stuff
cout << "enter another value (enter sentinel to quit): ";
cin >> value;
}
Arrays are a collection of data where you access an individual element in the array by its index number. Array indices start at 0 and run up to one less than the size of the array. So in the case of the grades, this works out well because the grade values of 0,1,2,3,4 correspond to the index values of a 5 element array. So if they enter 4, the grade variable will equal 4 and you can use MyArray[grade] to access and increment the array value at index 4.
Edit:
@CodeGoggles - there is no grade E :)
The instructor asks for parallel arrays - so when it comes to the output, there would be a second array holding the text grade values.
That is the easiest part of the problem. Here is the method I use (corny, but my preference):
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
#include <iostream>
constint SENTINEL = -999;
int main()
{
std::cout << "Enter grade (0 - 4) or -999 to quit: ";
int grade;
std::cin >> grade;
while(grade != SENTINEL)
{
// whatever I was needing done
}
// polite good-bye for exiting program if I feel like it
return 0;
}
As for arrays, think of them as nothing more than a list (it can be a list of numbers (int, double, float), list of names (sting), or a list of letters (char)). Parallel arrays are basically two arrays (lists) where array1[index] and array2[index] are for the same thing. So, using the first post, you could have two arrays char grades[5] = {'A', 'B', 'C', 'D', 'F'}; and int gradeCount[5];. You would then keep track of each time a grade was given and increment the value in gradeCount[index] the just make a for loop to loop through both arrays and print them out.
Sorry, I'm trying to explain this while being distracted.
#include <iostream>
usingnamespace std;
int main()
{
// create an array of numbers
int nNumberOfGrades [6] = {0, 0, 0, 0, 0, 0};
// I chose the size of 6 because that is the amount
// of grade types you are calculating A B C D E F
// six different possible values to store
// I intialized the array to 0 you could also use a loop to do this
// or another method
int input = 1; // the input varible
// creating an infinite loop that will only exit should the input -1 be entered
while ( 1 )
{
cout << "Enter the Grade as a numeral " << endl;
cout << "0 = A\n"
<< "1 = B\n"
<< "2 = C\n"
<< "3 = D\n"
<< "4 = E\n"
<< "5 = F\n"
<< "Enter -1 when finished" << endl << endl;
cin >> input;
cout << endl;
// this is the statement that checks for a -1 value and if true
// the break statement will throw us out of loop
if (input == -1)
{
break;
}
// if the value is within the correct values of a grade
// increment the number stored in the correct grade array
if (input > -1 && input < 6)
{
nNumberOfGrades [input]++;
}
// or inform user of mistake and let the loop continue
else
{
cout << "Invalid number. Try Again" << endl << endl;
}
}
int grade = 0;
// another loop that loops through possible grades and displays the
// number of grades incremented
// in the respective grade slot
while ( grade < 6)
{
if (grade == 0)
{
cout << "Number of grades : " << nNumberOfGrades [grade] << " Letter grade: A" << endl << endl;
}
elseif (grade == 1)
{
cout << "Number of grades :" << nNumberOfGrades [grade] << " Letter grade: B" << endl << endl;
}
elseif (grade == 2)
{
cout << "Number of grades :" << nNumberOfGrades [grade] << " Letter grade: C" << endl << endl;
}
elseif (grade == 3)
{
cout << "Number of grades :" << nNumberOfGrades [grade] << " Letter grade: D" << endl << endl;
}
elseif (grade == 4)
{
cout << "Number of grades :" << nNumberOfGrades [grade] << " Letter grade: E" << endl << endl;
}
elseif (grade == 5)
{
cout << "Number of grades :" << nNumberOfGrades [grade] << " Letter grade: F" << endl << endl;
}
grade++; // Increments grade
}
system("pause");
return 0;
}
You could take the menu output, out of the loop to stop it repeating or even add a extra conditional check to display menu again.
I know you solved it, but it appears you guys skipped a part of the requirements.
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).
#include <iostream>
constint SENTINEL = -1;
// swap function to help keep track of
// the number of times a grade is entered
void swap(int *gradeCount, int index)
{
int tempCount = 0;
tempCount = gradeCount[index];
tempCount += 1;
gradeCount[index] = tempCount;
}
int main()
{
char gradeLetter[5] = { 'A', 'B', 'C', 'D' , 'F' }; // letter grades
int gradeCount[5] = { 0, 0, 0, 0, 0 }; // number of times a grade is entered
std::cout << "Enter your grade (0-4) or -1 to quit: ";
int grade;
std::cin >> grade;
while(grade != SENTINEL)
{
switch(grade)
{
case 0:
swap(gradeCount, 0);
break;
case 1:
swap(gradeCount, 1);
break;
case 2:
swap(gradeCount, 2);
break;
case 3:swap(gradeCount, 3);
break;
case 4:
swap(gradeCount, 4);
break;
default:
std::cout << "Invalid option\n";
break;
}
if(grade != SENTINEL)
{
std::cout << "Enter your grade (0-4) or -1 to quit: ";
std::cin >> grade;
}
}
for(int i = 0; i <= 4; i++)
{
std::cout << "There are " << gradeCount[i] << " of letter grade " << gradeLetter[i] << std::endl;
}
return 0;
}