Hey guys. I hate to have to ask for help but I've run out of ideas. The program I'm trying to write is supposed to write a random list of numbers 0 - 4 and then count the occurrences of each number. The only thing I can't get to work is the countValue function.

//count the number of occurrences of each number in the linked list
Node* countValue(Node *pHead) //<------HERE'S THE PROBLEM
{
      int count0;
      int count1;
      int count2;
      int count3;
      int count4;
      Node *pNode;
      for(pNode = pHead; pNode->next != NULL; pNode = pNode->next)
      {
          if(pNode->data == 0)
          {
              count0++;
          }
          if(pNode->data == 1)
          {
              count1++;
           
          }
          if(pNode->data == 2)
          {
              count2++;

          }
          if(pNode->data == 3)
          {
              count3++;
              
          }
          if(pNode->data == 4)
          {
              count4++;
          }
              
	  } 
	  cout << "0: " << count0 << endl;
	  cout << "1: " << count1 << endl;
	  cout << "2: " << count2 << endl;
	  cout << "3: " << count3 << endl;
	  cout << "4: " << count4 << endl;
     return (pNode);
}

Edited 6 Years Ago by Broseph: n/a

The only thing I can't get to work is the countValue function.

...
//count the number of occurrences of each number in the linked list
Node* countValue(Node *pHead) //<------HERE'S THE PROBLEM
{
...

So you don't think actually explaining what the problem is could be important? Pointing to a line is enough?

Please reconsider...

So you don't think actually explaining what the problem is could be important? Pointing to a line is enough?

Please reconsider...

It's not counting. I thought that I could run it through a loop and increment count as each node value passes through, but the numbers come out to be whack.

And it's not just a single line, the whole function doesn't work properly.

This question has already been answered. Start a new discussion instead.