Click here to Skip to main content
15,891,184 members
Please Sign up or sign in to vote.
2.00/5 (1 vote)
See more:
Hi, i have been working on C++ How to Program 8th edition and there is an example in chapter 5 about switch-case.
#include <iostream>
#include <string>
#include <cstdio>

using namespace std;

class GradeBook
{
public:
    GradeBook(string);
    void setCourseName(string);
    string getCourseName();
    void displayMessage();
    void inputGrades();
    void displayGradeReport();
private:
    string courseName;
    int aCount;
    int bCount;
    int cCount;
    int dCount;
    int fCount;
};

GradeBook::GradeBook(string name)
{
    setCourseName(name);
    int aCount=0;
    int bCount=0;
    int cCount=0;
    int dCount=0;
    int fCount=0;
}

void GradeBook::setCourseName(string name)
{
    if(name.length() <=25)
        courseName = name;
    else
    {
        courseName = name.substr(0,25);
        cout<<"Name \""<<name<<"\" exceeds maximum length (25)."<<endl<<"Limiting coursName to first 25 character."<<endl;
    }
}

string GradeBook::getCourseName()
{
    return courseName;
}

void GradeBook::displayMessage()
{
    cout<<"Welcome to the grade book for\n"<<getCourseName()<<endl;
}
void GradeBook::inputGrades()
{
    int grade;


    cout<<"Enter the letter grades."<<endl<<"Enter the EOF character to end input."<<endl;
    while((grade = cin.get()) !=EOF)
    {
        switch ( grade )
        {
            case 'A':
            case 'a':
            ++aCount;
            break;

            case 'B':
            case 'b':
            ++bCount;
            break;

            case 'C':
            case 'c':
            ++cCount;
            break;

            case 'D':
            case 'd':
            ++dCount;
            break;

            case 'F':
            case 'f':
            ++fCount;
            break;

            case '\n':
            case '\t':
            case ' ':
            break;

            default:
            cout<<"Incorrect letter grade entered. Enter a new grade."<<endl;
            break;
        }

    }

}

void GradeBook::displayGradeReport()
{
    cout<<"\n\nNumber of students who received each letter grade:"
    << "\nA: " << aCount
    << "\nB: " << bCount
    << "\nC: " << cCount
    << "\nD: " << dCount
    << "\nF: " << fCount
    <<endl;
}

int main()
{
    GradeBook myGradeBook("CS 101 C++ Programming");

    myGradeBook.displayMessage();
    myGradeBook.inputGrades();
    myGradeBook.displayGradeReport();
}


I am using codeblocks i thought it may cause a problem but its same in the visual studio 2012.

This is the result of the code;

http://imageshack.us/photo/my-images/405/as9q.jpg/[^]

How can i get the right results. Its probably about initialization but i couldnt find the solution.
Posted
Comments
CHill60 3-Nov-13 14:41pm    
Your link doesn't work. Just explain to us in words what the problem is.
Hatredalper 3-Nov-13 15:22pm    
It is solved thanks for interesting.

The problem is in the GradeBook constructor.
C++
GradeBook::GradeBook(string name)
{
    setCourseName(name);
    int aCount=0;
    int bCount=0;
    int cCount=0;
    int dCount=0;
    int fCount=0;
}


What is happening here is that you are not initializing the member variables of the GradeBook class but instead are creating local variables.
Here a read through this to explain the concept of scope[^].

Because the member variables are not initialized you get the 'weird' numbers in your output. When the program start to use them they have an undetermined start value.

To correctly initialize the member variables you need to remove the 'int' from the statements in the constructor.
C++
GradeBook::GradeBook(string name)
{
    setCourseName(name);
    aCount=0;
    bCount=0;
    cCount=0;
    dCount=0;
    fCount=0;
}

You can also use member initialization list
C++
GradeBook::GradeBook(string name)
    : aCount( 0 ),
      bCount( 0 ),
      cCount( 0 ),
      dCount( 0 ),
      fCount( 0 )
{
    setCourseName(name);
}

Here is some information about the initialization list[^].
 
Share this answer
 
v2
Comments
Hatredalper 3-Nov-13 15:21pm    
Yes i see now, so is it means that every time i use "int", it is creating a local variable to use but not for the specific object right ?
André Kraak 3-Nov-13 15:23pm    
Correct.

This explains about declaring variables and using them: http://www.cplusplus.com/doc/tutorial/variables/[^]
Hatredalper 3-Nov-13 15:27pm    
Thank you for your answer and interesting. I got it.
Sergey Alexandrovich Kryukov 3-Nov-13 15:22pm    
Well spotted, a 5.
—SA
André Kraak 3-Nov-13 15:23pm    
Thanks.
class construction only do initialize the local variable.
so u r wrongly wants to declar variables.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900