I\'ve been working on this assignment in C++. Below are the assignment details a
ID: 3748033 • Letter: I
Question
I've been working on this assignment in C++. Below are the assignment details and then my code that I'm trying to use to solve this problem. When submitting the code, I hit an error saying that the code generates too much output (see attached image for exact error message). Please help me fix this and get past this error
Assignment Details:
Professor Dolittle has asked some computer science students to write a program that will help him calculate his final grades. Professor Dolittle gives two midterms and a final exam. Each of these is worth 100 points. In addition, he gives a number of homework assignments during the semester. Each homework assignment is worth 100 points.
At the end of the semester, Professor Dolittle wants to calculate the median score on the homework assignments for the semester. He believes that the median score is a good measure of the student's overall performance on the homework assignments. The median is found by putting the homework scores in order, and then taking the score that is at the midpoint of this list. If there are an odd number of assignments, the median is the score exactly in the middle of the list. If there are an even number of assignments, the median is the average of the two scores closest to the midpoint of the data.
Once the median score is known, professor Dolittle takes the sum of the exam scores and the median homework score and uses this value to compute a letter grade. Letter grades are assigned based on the following table:
Your program should work as follows:
1. All user input should be tested to be sure that it is a valid integer value in the range 0 to 100.
2. It should ask the user to enter in the score for the first midterm. Then this value is read in and saved.
3. It should ask the user to enter in the score for the second midterm. Then this value is read in and saved.
4. It should ask the user to enter in the score for the final exam. Then this value is read in and saved.
5. The program then asks the user to enter in the scores for the homework assignments. Any number of scores can be entered in. You will test for a -1 entry to stop adding to the vector.
6. Store the homework scores in a vector.
7. Once all of the data has been entered, the program calls a function that you have written to find the median homework score.
8. The program then calls a function that you have written to calculate and return the letter grade. The return grade is based upon the total of the three exams plus the median homework score (total points).
9. Finally, display the median homework score, the total point count, and the letter grade.
Example output:
Tested Output will be:
abc 97 65 85 abc 200 99 65 78 80 -1
Code that I've been using to try and get the program to work:
#include <iostream>
#include <algorithm>
#include <vector>
#include <string>
using namespace std;
// Function to take exam grade as an input
void take_input(int * grade, string exam){
cin >> (*grade);
while(1)
{
//test for string
if (cin.fail()){
cout << " Sorry, your input must be an integer. Please try again." << endl;
cout << "Please enter in the score for the " << exam << " exam: ";
}
// test for grade range between 0 and 100
else if ((*grade)>100 || (*grade<0)){
cout << " Sorry, your input must be between 0 and 100 Please try again." << endl;
cout << "Please enter in the score for the " << exam << " exam: ";
}
// if input (grade) is in range, then break the loop.
else
break;
cin.clear();
cin.ignore(256, ' ');
cin >> (*grade);
}
}
int main () {
int examGrade1;
int examGrade2;
int examGrade3;
unsigned int i = 0;
int hwMedian;
// int hwAvg;
int totalScore;
string shwGrade;
int hwGrade;
vector<int> hwScores;
cout << "Dr. DoLittle's Grading Program ....." << endl;
cout << " Please enter in the score for the first exam: ";
take_input(&examGrade1,"first");
cout << " Please enter in the score for the second exam: ";
take_input(&examGrade2,"second");
cout << " Please enter in the score for the final exam: ";
take_input(&examGrade3,"final");
//HOMEWORK FUNCTION
cin.ignore(10000, ' ');
while(1)
{
cout << " Enter the score for a homework assignment: ";
getline(cin, shwGrade);
// check if it is blank line.
if (shwGrade == "")
break;
// try and catch block to validate to convert string to integer (homework assignment grade).
try
{
size_t pos ;
hwGrade = stoi( shwGrade, addressof(pos) ) ;
if( pos == shwGrade.size() ) {
if(hwGrade > 100 || hwGrade < 0){
cout << " Please enter an integer between 0 and 100. Please try again";
}
else if(hwGrade > 0 && hwGrade <= 100){
hwScores.push_back(hwGrade);
}
}
else cout << " Sorry, your input must be an integer. Please try again.";
}
catch( const invalid_argument& ) { cout << " Sorry, your input must be an integer. Please try again." ; }
catch( const out_of_range& ) { cout << " Sorry, your input must be an integer. Please try again." ; }
}
sort(hwScores.begin(), hwScores.end());
// if size of homework assignment grades is even
if (hwScores.size()%2==0)
hwMedian = (hwScores.at(hwScores.size()/2-1) + hwScores.at(hwScores.size()/2)) / 2;
else
hwMedian = hwScores.at((hwScores.size()-1)/2);
//calculate the total score and store in variable
totalScore = examGrade1 + examGrade2 + examGrade3 + hwMedian;
cout << " The median homework score was " << hwMedian << endl;
cout << "The total points earned was " << totalScore << endl;
//calculate the letter grade
if(totalScore <= 400 && totalScore >=380){
cout << "The letter calculated letter grade is A" << endl;
}
if(totalScore < 380 && totalScore >=360){
cout << "The letter calculated letter grade is A-" << endl;
}
if(totalScore < 360 && totalScore >=340){
cout << "The letter calculated letter grade is B+" << endl;
}
if(totalScore < 340 && totalScore >=320){
cout << "The letter calculated letter grade is B" << endl;
}
if(totalScore < 320 && totalScore >=300){
cout << "The letter calculated letter grade is B-" << endl;
}
if(totalScore < 300 && totalScore >=280){
cout << "The letter calculated letter grade is c+" << endl;
}
if(totalScore < 280 && totalScore >=260){
cout << "The letter calculated letter grade is C" << endl;
}
if(totalScore < 260 && totalScore >=240){
cout << "The letter calculated letter grade is C-" << endl;
}
if(totalScore < 240 && totalScore >=220){
cout << "The letter calculated letter grade is D+" << endl;
}
if(totalScore < 220 && totalScore >=200){
cout << "The letter calculated letter grade is D" << endl;
}
if(totalScore < 200 && totalScore >=180){
cout << "The letter calculated letter grade is D-" << endl;
}
if(totalScore < 180){
cout << "The letter calculated letter grade is F" << endl;
}
return 0;
}
Error I get when I try to submit this code:
Total Score Letter Grade 381 - 400 A 361 - 380 A- 341 - 360 B+ 321 - 340 B 301 - 320 B- 281 - 300 C+ 261 - 280 C 241 - 260 C- 221 - 240 D+ 201 - 220 D 181 - 200 D- 180 and below F Program errors displayed here Program generated too much output. Output restricted to 50000 characters. Check program for any unterminated loops generating output.Explanation / Answer
There was too much redundant code and logical error so i have written fresh new code for that is more modular.
compiler used c++11
//code.cpp
#include <iostream>
#include <vector>
#include <string>
#include <algorithm>
using namespace std;
//method for validating the input
//it has one default args for valid the -1 for homework
int get_valid_no(string msg, bool ishomework = false)
{
int n;
while (1)
{
cout << msg << ": ";
cin >> n;
cout << endl;
if (cin.fail())
{
cout << "Sorry, your input must be an integer. Please try again." << endl;
cin.clear();
cin.ignore(100, ' ');
continue;
}
//in case of homework returning -1
if (ishomework && n == -1)
return -1;
else if (n > 100 || n < 0)
{
cout << "Sorry, your input must be between 0 and 100 Please try again." << endl;
}
else
{
return n;
}
}
}
//method for getting the grade as per given marks
string get_grade(int totalScore)
{
if (totalScore >= 380)
return "A";
else if (totalScore < 380 && totalScore >= 360)
return "A-";
else if (totalScore < 360 && totalScore >= 340)
return "B+";
else if (totalScore < 340 && totalScore >= 320)
return "B";
else if (totalScore < 320 && totalScore >= 300)
return "B-";
else if (totalScore < 300 && totalScore >= 280)
return "C+";
else if (totalScore < 280 && totalScore >= 260)
return "C";
else if (totalScore < 260 && totalScore >= 240)
return "C-";
else if (totalScore < 240 && totalScore >= 220)
return "D+";
else if (totalScore < 220 && totalScore >= 200)
return "D";
else if (totalScore < 200 && totalScore >= 180)
return "D-";
else
return "F";
}
int get_homework_median(vector<int> home_work_marks)
{
//number of hmework assignmenst
size_t no_of_assignments = home_work_marks.size();
//inca se of zero
if (no_of_assignments == 0)
{
return 0;
}
else
{
//sorting the marks
sort(home_work_marks.begin(), home_work_marks.end());
if (no_of_assignments % 2 == 0)
{
return (int)(home_work_marks[no_of_assignments / 2 - 1] + home_work_marks[no_of_assignments / 2]) / 2;
}
else
{
return (int)home_work_marks[no_of_assignments / 2];
}
}
}
int main()
{
vector<int> homework;
int first_exam, second_exam, final_exam, temp = 0;
//getting all three exams marks
first_exam = get_valid_no("Please enter in the score for the first exam");
second_exam = get_valid_no("Please enter in the score for the second exam");
final_exam = get_valid_no("Please enter in the score for the final exam");
//getting the homework marks
while (1)
{
temp = get_valid_no("Enter the score for a homework assignment", true);
if (temp == -1)
break;
else
homework.push_back(temp);
}
//finding the mean of homework mark
int homework_median = get_homework_median(homework);
int totalScore = (first_exam + second_exam + final_exam + homework_median);
cout << "The median homework score was " << homework_median << endl;
cout << "The total points earned was " << totalScore << endl;
cout << "The letter calculated letter grade is " << get_grade(totalScore) << endl;
}
//OUTput
Please enter in the score for the first exam: abc
Sorry, your input must be an integer. Please try again.
Please enter in the score for the first exam: 97
Please enter in the score for the second exam: 65
Please enter in the score for the final exam: 85
Enter the score for a homework assignment: abc
Sorry, your input must be an integer. Please try again.
Enter the score for a homework assignment: 200
Sorry, your input must be between 0 and 100 Please try again.
Enter the score for a homework assignment: 99
Enter the score for a homework assignment: 65
Enter the score for a homework assignment: 78
Enter the score for a homework assignment: 80
Enter the score for a homework assignment: -1
The median homework score was 79
The total points earned was 326
The letter calculated letter grade is B
//please do let me know if u have any concern...
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.