Problem with Function code

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#include <iostream>
#include <string.h>
using namespace std;
string checker()
{
	for(int i=0;i<8;i++)
	{
		for(int j=0; j<8;j++)
		{
			if((j+i)%2==0)
				return "{}";
			else
				return "[]";
		}
		cout<<endl;
	}
}
int main()
{
	cout<<checker<<endl;
	return 0;
}

This function's purpose is to print a checkerboard but instead of doing so, it is printing out a 1! I need help asap if possible thanks!
Last edited on
it obviously interprets the provided function pointer as a boolean expression.

To make it more clear:

cout<<checker<<endl; // This provides a function pointer

cout<<checker()<<endl; // This is a function call


Btw: the loops are useless due to the return statements.
Last edited on
closed account (3qX21hU5)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#include <iostream>
#include <string.h>
using namespace std;
string checker() // Change this to void checker() since we won't be returning.
{
	for(int i=0;i<8;i++)
	{
		for(int j=0; j<8;j++)
		{
			if((j+i)%2==0)
				return "{}"; // Change this to cout
			else
				return "[]"; // Change this to cout
		}
		cout<<endl;
	}
}
int main()
{
	cout<<checker<<endl; // Remove the cout. Only have checker();
	return 0;
}


I not really sure what your function was suppose to do but I'm assuming that it is printing out the checkers board. Let me know if I am wrong or if you have more information that could help me give you a better answer.

If you don't understand anything also let me know and I will try and help to the best of my ability.
Last edited on
zereo, i did as you said and it produces an error: void value not ignored as it ought to be.
coder777, it still doesnt print the whole checkerboard. so confused just started making functions
So do as Zereo suggested:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#include <iostream>
#include <string.h>
using namespace std;
string void checker() // Change this to void checker() since we won't be returning.
{
	for(int i=0;i<8;i++)
	{
		for(int j=0; j<8;j++)
		{
			if((j+i)%2==0)
				return cout<< "{}"; // Change this to cout
			else
				return cout<< "[]"; // Change this to cout
		}
		cout<<endl;
	}
}
int main()
{
	checker();
	return 0;
}
Oh i see why the error was produced earlier. Thank you! I have a better understanding now, thank you all again.
Topic archived. No new replies allowed.