1

The first if statement enters as suspected. The object calls the getCount() function but the value that is incremented is not the previous count but instead 0. I tried using Count += Count in the function.

int main(){
    .........     
    int displayCount;
    while(!inputfile.eof()) 
    {

        inputfile.get(letter);

            Checker object1;
            if (object1.isValid(letter)))
            {
                displayCount = object1.getCount();
            }
    }
    cout << displayCount;
   .
   .
   .
Checker::Checker() :
    m_Valid(false),
    count(0)
{
}


int Checker::getCount()
{

    if(m_Valid)
    {
        count ++;
    }

    return count;
}

My inclination is that once the value is returned from the function then thats it. It will no longer hold the previous value bc of this (my guess).

user1727433
  • 39
  • 1
  • 6
  • 3
    [Please don't use `eof()` in the loop](http://stackoverflow.com/questions/5605125/why-is-iostreameof-inside-a-loop-condition-considered-wrong). – chris Dec 19 '12 at 04:35
  • `m_Valid(false)` then `if(m_Valid)` always return false? – billz Dec 19 '12 at 04:36
  • Billz, it doesn't always return false because I have a seperate function that returns true when applicable. I stepped through the code and that if statment returns true as expected. – user1727433 Dec 19 '12 at 04:41
  • In the header file, it is declared as a private accessor as "int count" – user1727433 Dec 19 '12 at 04:45

2 Answers2

3

You just need to move Checker object1 outside of the loop; a new instance is being created with a count of zero for each pass as the code stands.

Keith
  • 6,756
  • 19
  • 23
0

As your comment, count is not staic member of Check class, Checker object1; always creates a new object and initialize count to 0. make count static should make your getCount() rolling.

class Checker
{
   static int count;
};

initialize count in Checker.cpp

int Check::count = 0;
billz
  • 44,644
  • 9
  • 83
  • 100