Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

c++ Everytime you press enter it is added to the array as an empty element. How can I stop my code from doing that? ..

c++ Everytime you press enter it is added to the array as an empty element. How can I stop my code from doing that?

.. // ......................................... // Create GradeBook object, input grades and display grade report. // GradeBook.cpp // Member-function definitions for class GradeBook that // uses a switch statement to count A, B, C, D and F grades. #include #include "GradeBook.h" // include definition of class GradeBook #include using namespace std;

// constructor initializes courseName with string supplied as argument; // initializes counter data members to 0 GradeBook::GradeBook(string name) : aCount(0), // initialize count of A grades to 0 bCount(0), // initialize count of B grades to 0 cCount(0), // initialize count of C grades to 0 dCount(0), // initialize count of D grades to 0 fCount(0), // initialize count of F grades to 0 eCount(0) // this statement calls getCourseName to get the { cout << "The Grade Book Constructor is called" << endl; // informs that constructor is opened initializeData(); // sets everything to 0 setCourseName(name); // sets course name and limits character length displayGradeReport(); // a report of all the data collected cout << " *****The end of Grade Book Constructor.***** " << endl; } // end GradeBook constructor

// function to set the course name; limits name to 25 or fewer characters void GradeBook::setCourseName(string name) { if (name.size() <= 30) // if name has 30 or fewer characters courseName = name; // store the course name in the object else // if name is longer than 30 characters { // set courseName to first 25 characters of parameter name courseName = name.substr(0, 30); // select first 25 characters cerr << setw(14) << "Name \"" << name << setw(19) << " exceeds maximum length (30). " << setw(30) << " Limiting courseName to first 30 characters. " << endl;

cout << "\tWelcome to the grade book for" << endl << "\t" << courseName << "! " // displays the shortened name << "The Grade Book, " << courseName << ", contains "; } // end if...else } // end function setCourseName

// function to retrieve the course name string GradeBook::getCourseName() const { return courseName; } // end function getCourseName

// display a welcome message to the GradeBook user void GradeBook::displayMessage() const { // name of the course this GradeBook represents cout << "\t Welcome to the grade book for " << "\t " << getCourseName() << "! " << endl; } // end function displayMessage

// input arbitrary number of grades from user; update grade counter void GradeBook::inputGrades() {

cout << " \tEnter letter grades" << endl << "\t-->\tOr Enter the EOF character to end input." << endl << "\t(Use Ctl + D, or Ctl + Z)" << endl; for (int x = 0; ((letterGrades[x] = cin.get()) != EOF) && (x <= 99); x++) {

// determine which grade was entered switch (letterGrades[x]) // switch statement nested in while {

this->letterGrades[x] = getchar();

case 'A': // grade was uppercase A case 'a': // or lowercase a countGrades[0]++; ++aCount; // increment aCount break; // necessary to exit switch

case 'B': // grade was uppercase B case 'b': // or lowercase b countGrades[1]++; ++bCount; // increment bCount break; // exit switch

case 'C': // grade was uppercase C case 'c': // or lowercase c countGrades[2]++; ++cCount; // increment cCount break; // exit switch

case 'D': // grade was uppercase D case 'd': // or lowercase d countGrades[3]++; ++dCount; // increment dCount break; // exit switch

case 'F': // grade was uppercase F case 'f': // or lowercase f countGrades[4]++; ++fCount; // increment fCount break; // exit switch case ' ': // ignore newlines, case '\t': // tabs, case ' ': // and spaces in input case NULL: // tring to get rid of empty elements std::fill( //attempt to get rid of empty elements std::remove_if(&letterGrades[0], &letterGrades[100], [](char const c) {return c == '\0'; }), &letterGrades[100],0);

break; // exit switch

default: // catch all other characters cout << "\tXXXX An incorrect letter grade entered. XXXX \t\"" // informs user that they entered an invalid grade << letterGrades[x] << "\" is not a propper letter grade. " << endl; // tells user what they entered countGrades[5]++; ++eCount; break; // optional; will exit switch anyway

} // end switch cout << " \tEnter letter grades.2" << endl << "\t-->\tOr Enter the EOF character to end input." << endl << "\t(Use Ctl + D, or Ctl + Z)" << endl; } // end for

} // end function inputGrades

void GradeBook::displayInputs() { cout << " The data entered is listed as the following: "; for (int x = 0; letterGrades[x] != EOF && (x <= 99); x++) // loops until the end of the array is reached { cout << setw(0) << "[" << x << "] -> " << letterGrades[x] << "\t"; // displays array element

if ((x + 1) % 4 == 0) { cout << endl; // } } cout << endl; // spacer } // end function displayInputs

double GradeBook::calculateGrade() const { double numGrades = 0; for (int x = 0; x <= 4; x++) // counts valid grades { numGrades += countGrades[x]; // adds up all valid grades } // end for

double gpa = 0; // if (numGrades == 0) { numGrades = 1; } // end if gpa = ((countGrades[0] *4.0 ) + (countGrades[1] *3.0 ) + (countGrades[2] *2.0 ) + (countGrades[3] )) / numGrades;

return gpa; // returns GPA } // end function GradeBook

GradeBook::~GradeBook() { cout << " Destructor is called " << endl; }

// display a report based on the grades entered by user void GradeBook::displayGradeReport() const { int grades = 0; // counter for # of grades entered int data = 0; // grades + errors for (int x = 0; x <= 4; x++) { grades += countGrades[x]; }

data = (grades + countGrades[5]); cout << " The total number of data entered is " << data; cout << " The total number of students receive grades is " << grades;

cout << " Number of students who received each letter grade:" << " A: " << aCount // display number of A grades << " B: " << bCount // display number of B grades << " C: " << cCount // display number of C grades << " D: " << dCount // display number of D grades << " F: " << fCount // display number of F grades << " Error: " << eCount // display number of error << endl << endl;

cout << "The class average is: " << fixed << setprecision(1) << calculateGrade() << endl; } // end function displayGradeReport

void GradeBook::initializeData() { for (int x = 0; x <= 5; x++) // loop to initialize data { countGrades[x] = 0; }

for (int x = 0; x <= 100; x++) // loop to initialize data/length grade { this->letterGrades[x] = NULL; }

courseName = "NULL"; // initialize course name

}

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image_2

Step: 3

blur-text-image_3

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Concepts Of Database Management

Authors: Philip J. Pratt, Joseph J. Adamski

4th Edition

0619064625, 978-0619064624

More Books

Students also viewed these Databases questions