0

Hey guys, I think I have a pretty simple fix here, I just need somebody to point out my error.
I'm getting a seg fault after writing to an array of char. I need my function to run through the array and find pairs. If every char in the array has a pair, return true. After my program acepts values into the array, it segfaults. Heres my code...

bool allValuesArePaired(char d[], int o)        //int o is equal to const int (EVEN = 6)
{
	int count, tracker, i, j;
	cout << "Fill array to check parity\n";
	
	for (i = 0; i < EVEN; i++) 
	{
		d[i] = 0;
		cin >> d[i];
	}
	
	for (i = 0; i < EVEN; i++)
	{
		for (j = 0; i < EVEN; j++)
		{
			if (d[i] == d[j])
			{
				count++;
				cout << count;
			}
			if (count == 2) 
			{
				count = 0;
				tracker++;
			}
		}
	}
		
		if (tracker >= EVEN / 2) 
		{
			return true;
		}
		else 
		{
			return false;
		}

}
2
Contributors
2
Replies
3
Views
7 Years
Discussion Span
Last Post by Empireryan
1

Line 15: the bound on the loop is wrong.. simple typo, you wrote i where it should be j:

for (j = 0; i < EVEN; j++)
//should be:
		for (j = 0; j < EVEN; j++)
This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.