samedi 28 février 2015

C++ Assign GradeBook

Hi and thanks in advanced. I'm working on this H.W. and I'm beginner. I keep on running on in M.S. VS Professional but keeps returning too many errors and I fix them, yet same issues not sure if what I had added to the code is wrong. The results should display its takes no more than 30 char. it should display total number of students received a grade, the average, and class average in one digit double value and GPA. Here is the code:



#include <iostream>
#include "GradeBook.h" // include definition of class GradeBook
using namespace std;

// constructor initializes courseName with string supplied as argument;
// initializes counter data members to 0
GradeBook::GradeBook(string name)
{
cout << "The Grade Book Constructor is called" << endl;
setCourseName(name); // validate and stores courseName
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
displayMessage();
cout << "The Grade Book," << getCourseName() << "Contains" << endl << endl < endl;
displatGradeReport(0);
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 30 characters of parameter name
courseName = name.substr( 0, 30 ); // select first 25 characters
cerr << "Name \"" << name << "\" exceeds maximum length (30).\n"
<< "Limiting courseName to first 30 characters.\n" << endl;
} // end if...else
} // end function setCourseName

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

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

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

cout << "Enter the letter grades." << endl;
cout << "Enter the EOF character to end input." << endl;
cout << "Use Ctl + D, or Ctl + Z)" << endl;

// loop until user types end-of-file key sequence
while ( ( grade = cin.get() ) != EOF )
{
// determine which grade was entered
switch ( grade ) // switch statement nested in while
{
case 'A': // grade was uppercase A
case 'a': // or lowercase a
++aCount; // increment aCount
break; // necessary to exit switch

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

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

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

case 'F': // grade was uppercase F
case 'f': // or lowercase f
++fCount; // increment fCount
break; // exit switch

case '\n': // ignore newlines,
case '\t': // tabs,
case ' ': // and spaces in input
break; // exit switch

default: // catch all other characters
cout << "****Incorrect letter grade entered.****" << endl;
cout << " Enter a new grade." << endl;
break; // optional; will exit switch anyway
} // end switch
} // end while
} // end function inputGrades

// display a report based on the grades entered by user
void GradeBook::displayGradeReport()
{
// output summary of results
// total grades
int gradeCount = aCount + bCount + cCount + dCount + fCount;
cout << "\n\nThe total number of students receive grade is" << gradeCount << endl;
cout << "Number of students who received each letter grade:"
<< "\nA: " << aCount // display number of A grades
<< "\nB: " << bCount // display number of B grades
<< "\nC: " << cCount // display number of C grades
<< "\nD: " << dCount // display number of D grades
<< "\nF: " << fCount // display number of F grades
<< endl;
} // end calculate number of grades received

// display class average
// if user entered at least one grade
if (gradeCount != 0)
{
// calculate total grades
int gradeTotal = 4 * aCount + 3 * bCount + 2 * cCount + 1 * dCount;

// set floating-point number format
cout << fixed << setprecision(1);

// compute and display class GPA with 1 digit of precision
cout << "\nThe class average is: "
<< static_cast< double > (gradeTotal) / gradeCount
<< endl << endl << endl;
} // end if
} // end function displayGradeReport
void GradeBook::displayGradeReport(int n)
{
// display summary of results
// calculate total grades
int gradeCount = aCount = bCount = cCount = dCount = fCount = n;
cout << "The total number of students receive grades is " << gradeCount << endl;
cout << "Number of students who received each letter grade:"
<< "\nA: " << aCount // display number of A grades
<< "\nB: " << bCount // display number of B grades
<< "\nC: " << cCount // display number of C grades
<< "\nD: " << dCount // display number of D grades
<< "\nF: " << fCount // display number of F grades
<< endl << endl;

// calculate total grades


// display class average
// calculate total grades
int gradeTotal = 4 * aCount + 3 * bCount + 2 * cCount + 1 * dCount;

// set floating-point number format
cout << fixed << setprecision(1);

// compute and display class GPA with 1 digit of precision
if (gradeCount != 0)
{
cout << "\nThe class average is: "
<< static_cast< double > (gradeTotal) / gradeCount
<< endl << endl << endl;
}
else
{
cout << "\nThe class average is: 0.0" << endl << endl << endl;
}
} // end function displayGradeReport


Here is the .h



#include <string> // program uses C++ standard string class
using namespace std;

// GradeBook class definition
class GradeBook
{
public:
GradeBook( string ); // initialize course name
void setCourseName( string ); // set the course name
string getCourseName(); // retrieve the course name
void displayMessage(); // display a welcome message
void inputGrades(); // input arbitrary number of grades from user
void displayGradeReport(); // display report based on user input
private:
string courseName; // course name for this GradeBook
int aCount; // count of A grades
int bCount; // count of B grades
int cCount; // count of C grades
int dCount; // count of D grades
int fCount; // count of F grades
}; // end class GradeBook


And here is the file I'm executing from in the project


include // program uses C++ standard string class


using namespace std;



// GradeBook class definition
class GradeBook
{
public:
GradeBook( string ); // initialize course name
void setCourseName( string ); // set the course name
string getCourseName(); // retrieve the course name
void displayMessage(); // display a welcome message
void inputGrades(); // input arbitrary number of grades from user
void displayGradeReport(); // display report based on user input
private:
string courseName; // course name for this GradeBook
int aCount; // count of A grades
int bCount; // count of B grades
int cCount; // count of C grades
int dCount; // count of D grades
int fCount; // count of F grades
}; // end class GradeBook

Aucun commentaire:

Enregistrer un commentaire