I want to be able to determine if there are less than 9 values of 'S' but it always returns a count larger than it should be. The array is board[7][7].

Edit: The cout for sheepCount is for debugging purposes.

int sheepCount;
sheepCount = 0;
for (int i = 7; i>0; i--)
{
     for (int j =0; j<7; j++)
     {
         if(board[i][j] == 'S')
         {
          sheepCount++;
          cout<< board[i][j];
          cout << "\nsheepCount ->" << sheepCount;
         }

     }
}

Edited 5 Years Ago by mistereff: n/a

  1. If it's a 7 x 7 array, your indexes should be 0,1,2,3,4,5,6. You use array index 7 in line 3. That's out of bounds.
  2. What do you mean "return"? Is this a function?

It looks like the problem is with your outer for() loop because you are starting from 7 and going down to 1 (since its >0). Index 7 is out of range and 0 is used so you will be missing information and getting junk info.

#include <iostream>
using namespace std;

int main()
{
	int sheepCount = 0;
	char board[][7]= {{' ',' ','S','S',' ','S',' '},
			{' ',' ','S','S',' ','S',' '},
			{' ',' ','S','S',' ','S',' '},
			{' ',' ','S','S',' ','S',' '},
			{' ',' ','S','S',' ','S',' '},
			{' ',' ','S','S',' ','S',' '},
			{' ',' ','S','S',' ','S',' '}};



	for( int i = 0; i < 7; i++ )
	{
		for( int j = 0; j < 7; j++ )
		{
			if( board[i][j] == 'S' )
			{
				sheepCount++;
			}
		}
	}
	cout << "sheepCount -> " << sheepCount << endl;


	return 0;
}

I have ran it with that for() loop and it returned 13 as sheepCount when it should have been 9.

Edit: @Vernon it is part of a function the whole function is

int gameOver()
{
    int sheepCount;
    sheepCount = 0;
for( int i = 0; i < 7; i++ )
	{
		for( int j = 0; j < 7; j++ )
		{
			if( board[i][j] == 'S' )
			{
				sheepCount++;
			}
		}
	}
    cout << "\nsheepCount ->" << sheepCount;
    if(sheepCount <= 8)
    {
        return 1;
    }
    else if(board[4][2] == 'S' && board[4][3] == 'S' && board[4][4] == 'S' &&
            board[5][2] == 'S' && board[5][3] == 'S' && board[5][4] == 'S' &&
            board[6][2] == 'S' && board[6][3] == 'S' && board[6][4] == 'S')
    {
        return 2;
    }
    else{return 0;}
}

Edited 5 Years Ago by mistereff: n/a

sfuo's code looks fine to me. Perhaps replace his board with your board and run his program. See if you still get bad results. If so, post it. If not, the problem must lie elsewhere. Perhaps board[][] does not contain what you think it contains. I don't know.

I posted my entire function and board[][] contains because I print it in main before I do this check. The 13 that it returns is constant I've changed the number of Ss to 6 and it still says 13


This is how it's called in main:

if(gameOver() == 1)
{
    cout << "\nThe game is over. The foxes win.\n";
    return 1;
}
else if(gameOver() == 2)
    {
        cout << "\nThe game is over. The sheep win.\n";
        return 1;
    }

Edited 5 Years Ago by mistereff: n/a

That is slightly more complicated the user inputs a board and it gets placed into my array. This is the board after it is updated

board[0][2] = one[0];
board[0][3] = one[1];
board[0][4] = one[2];

board[1][2] = two[0];
board[1][3] = two[1];
board[1][4] = two[2];

board[2][0] = three[0];
board[2][1] = three[1];
board[2][2] = three[2];
board[2][3] = three[3];
board[2][4] = three[4];
board[2][5] = three[5];
board[2][6] = three[6];

board[3][0] = four[0];
board[3][1] = four[1];
board[3][2] = four[2];
board[3][3] = four[3];
board[3][4] = four[4];
board[3][5] = four[5];
board[3][6] = four[6];

board[4][0] = five[0];
board[4][1] = five[1];
board[4][2] = five[2];
board[4][3] = five[3];
board[4][4] = five[4];
board[4][5] = five[5];
board[4][6] = five[6];

board[5][2] = six[0];
board[5][3] = six[1];
board[5][4] = six[2];

board[6][2] = seven[0];
board[6][3] = seven[1];
board[6][4] = seven[2];

Honestly that doesn't tell us anything. I was looking for something with sheep in it and some fox so we could run it and see where the problem is.

well I input this

F.F
  ...
.......
.......
SS.S...
  SSS
  SSS
d3-d4

And everyone but the d3-d4 gets put into the boards[][]. I am sure that it is in the board[][] properly because I print the inputted board before I do anything else.

Edited 5 Years Ago by mistereff: n/a

How about simplifying your code a little?

On the assumption that there are 49 elements in the array (i.e. it is a 7 x 7 array)

int numberOfSheep = 0;
for (int i=0;i<49;i++)
{
  if (*board[i] == 'S') numberOfSheep++;
}

Edit: This is not a complete programme. It needs a main statement and all the rest of it as well to be compiled and run.

Edited 5 Years Ago by Moschops: Extra information explaining why it doesn't run.

That doesn't run.

Of course it doesn't run. It's a code snippet. It needs a main statement and all the rest of it as well.

No I mean when I put the snippet into my program it does not compile. The error is at the if statement.

Here's a complete working version.

#include<iostream>

int main(void)
{

  char board[7][7];
  board[1][1]='S';
  char* b = &board[0][0];

 
int numberOfSheep = 0;
for (int i=0;i<49;i++)
{
  if (b[i] == 'S')
   { numberOfSheep++;
     std::cout << b[i] << std::endl;}
 
}

 std::cout << numberOfSheep;

 return 0;
}

Here is what I threw together to load in the board, print the board, count the sheep and remove sheep so you can test to see what is going on on the board.

Based on the fact that you are inputting 6 sheep and always getting 13 tells me that you aren't actually modifying the original board array or something else is going wrong.

data.txt

x x F . F x x
x x . . . x x
. . . . . . .
. . . . . . .
S S S S S S S
x x S S S x x
x x S S S x x

main.cpp

#include <iostream>
#include <fstream>
using namespace std;

void LoadBoard(char board[][7]) //loads the board info in from data.txt
{
	ifstream in("data.txt");
	for( int i = 0; i < 7; i++ )
		for( int j = 0; j < 7; j++ )
			in >> board[i][j];
	in.close();
}

void PrintBoard(char board[][7]) //prints the board
{
	for( int i = 0; i < 7; i++ )
	{
		for( int j = 0; j < 7; j++ )
			cout << board[i][j] << " ";
		cout << endl;
	}
	cout << endl;
}

void CountSheep(char board[][7]) //counts how many sheep are on the board
{
	int sheepCount = 0;

	for( int i = 0; i < 7; i++ )
		for( int j = 0; j < 7; j++ )
			if( board[i][j] == 'S' )
				sheepCount++;
	cout << "sheepCount -> " << sheepCount << endl << endl;
}

void KillSheep(int xloc, int yloc, char board[][7]) //removes a sheep from a location
{
	if( (xloc >= 7 || xloc < 0) || (yloc >= 7 || yloc < 0) )
		return;
	if( board[yloc][xloc] == 'S' ) //x and y are switched because it goes rows then cols
		board[yloc][xloc] = '.';
}

int main()
{
	char board[7][7]; //initialize board
	LoadBoard(board); //load in the board from file

	PrintBoard(board); //print the board
	CountSheep(board); //count sheep (should be 13)

	KillSheep(2,4,board); //remove a sheep
	PrintBoard(board); //reprint board
	CountSheep(board); //recount sheep (should be 12)

	return 0;
}
This article has been dead for over six months. Start a new discussion instead.