Function call not working as expected

First I'll provide the code, then I'll explain it:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
#include <iostream>

const int COLS = 3;
const int ROWS = 3;
void is_magic(const int numbers[][COLS], int rows);
void get_user_square(int numbers[][COLS], int rows);
bool verify_input(int);
int main()
{

	int magic_square[ROWS][COLS] = {{1, 2, 3},
									{4, 5, 6},
									{7, 8, 9}};
	int magic_square2[ROWS][COLS] = {{4, 9, 2},
									 {3, 5, 7},
								   	{8, 1, 6}};
	is_magic(magic_square, ROWS);

	is_magic(magic_square2, ROWS);

	int user_square[ROWS] [COLS] = {0};

	get_user_square(user_square, ROWS);

	return 0;
}

void is_magic(const int numbers[][COLS], int rows)
{
	bool magic = false;
	int row1 = 0, row2 = 0, row3 = 0, col1 = 0, col2 = 0, col3 = 0, diag1 = 0, diag2 = 0;

	for(int i = 0;i == 0; i++)
	{
		for(int j = 0; j < COLS; j++)
		{
			row1 += numbers[0][j];
			row2 += numbers[1][j];
			row3 += numbers[2][j];
		}
	}

	for(int i = 0; i == 0; i++)
	{
		for(int j = 0; j < ROWS; j++)
		{
			col1 += numbers[j][0];
			col2 += numbers[j][1];
			col3 += numbers[j][2];
		}
	}

	int k = 2;
	for(int i = 0; i < ROWS; i++)
	{
		for(int j = 0; j == 0; j++)
		{
			diag1 += numbers[i][i];
			diag2 += numbers[i][k--];
		}
	}
	std::cout << "row1: " << row1
			  << "\nrow2: " << row2
			  << "\nrow3: " << row3
			  << "\ncol1: " << col1
			  << "\ncol2: " << col2
			  << "\ncol3: " << col3 
			  << "\ndiag1: " << diag1 
			  << "\ndiag2: " << diag2 << std::endl;

	if(row1 == row2 && row2 == row3 && row3 == col1 && 
	   col1 == col2 && col2 == col3 && col3 == diag1 && diag1 == diag2)
			std::cout << "This Lo Shu Square is magic!\n";
	else
		std::cout << "It ain't magic . . . \n";
}

void get_user_square(int numbers[][COLS], int rows)
{
	for(int i = 0; i < rows; i++)
	{
		for(int j = 0; j < COLS; j++)
		{
			std::cout << "Enter a value for row " << i + 1
					  << " column " << j + 1 << ": ";
			while(!(std::cin >> numbers[i][j]) && verify_input(numbers[i][j]))
			{
				if(!std::cin)
				{
					std::cout << "Numbers only! Re enter: ";
					std::cin.clear();
					std::cin.ignore(10000, '\n');
				}
			}

		}
	}
}

bool verify_input(int number)
{
	std::cout << "Made it!\n";
	const int SIZE = 9;
	int arr[SIZE] = {0};
	
	static int counter = 0;
	std::cout << "Counter = " << counter <<std::endl;
	arr[counter] = number;

	for(int i = 0; i < SIZE; i++)
		std::cout << arr[i] << std::endl;
	if(counter > 0)
	{
		for(int i = 0; i < counter; i++)
			if(arr[i] == arr[counter])
			{
				std::cout << "You already entered " << number << std::endl;
				return false;
			}
	}
	else
	{
		counter++;
		return true;
	}
			

}

This program is designed to use a two-dimensional array to represent a Lo Shu square, which is a three by three square with the digits 1 through 9 placed in different cells-- If you add up the digits in the top row, then the middle, then bottom, and the individual columns, and then diagonally, all the rows, columns, and diagonals add up to the same value. I made a function to allow a user to make their own square by inputting digits. I created a two dimensional array in main, and passed it into the function get_user_square(int [][COLS], int);, then I created nested loops to ask for values from the user. The thing about the Lo Shu square is that it must have the digits 1 - 9, and no two cells in the 3x3 grid can have the same value, so I made the function bool verify_input(int); to verify if the number the user is entering has already been used. I tried to do this by creating a local array and setting it to zero. The value gets passed to the function, put into the array, and a loop verifies if the value has already been used, and if so, returns false to the while loop from the previous function. However, this isn't working, and I don't know why. When I run the program, when the program asks for user input, I enter a digit, and nothing happens-- there is just a blinking text cursor as though it were waiting for more input. I put the line cout << "Made it!"; to see if the function was even executing, but when I put a digit, nothing happens at all. I found that when I input a bad value, like the letter 'f', the function is called, "Made it!" appears, and the previous while loop: while(!(std::cin)) executes. My two questions: why is the function not working as expected, and why when I input bad values does the verify_input function execute first, and then the while(!(std::cin >>)) executes second-- if I input a bad value, shouldn't the verify_input function not be called at all? Just try running the program for yourself if you can. I know I wrote a lot so thanks for anyone who replies.
Last edited on
Maybe part of the problem is that in verify_input() I created a local array, so that when a user inputs a value, the value is sent to the function and placed in the array, but maybe locally created arrays and their values are destroyed when a function exits. Arrays are automatically passed by reference, so I thought an array created in a separate function would keep its values between function calls, but maybe it doesn't. Maybe I can make it static? Will try . . .
yes, static variables in a function never die, they exist for the lifetime of the program.

You can pass an array into and out of the function, or return an array from the function, but you can't just put it in the function and rely on it being there next time.

I am happy to say I was able to fix this program myself! If anyone with a similar problem comes along and wants to know how I did it, here is the revised code:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
#include <iostream>

const int COLS = 3;
const int ROWS = 3;
void is_magic(const int numbers[][COLS], int rows);
void get_user_square(int numbers[][COLS], int rows);
bool verify_input(int);
int main()
{

	int magic_square[ROWS][COLS] = {{1, 2, 3},
									{4, 5, 6},
									{7, 8, 9}};
	int magic_square2[ROWS][COLS] = {{4, 9, 2},
									 {3, 5, 7},
								   	{8, 1, 6}};
	is_magic(magic_square, ROWS);

	is_magic(magic_square2, ROWS);

	int user_square[ROWS] [COLS] = {0};

	get_user_square(user_square, ROWS);

	is_magic(user_square, ROWS);

	return 0;
}

void is_magic(const int numbers[][COLS], int rows)
{
	bool magic = false;
	int row1 = 0, row2 = 0, row3 = 0, col1 = 0, col2 = 0, col3 = 0, diag1 = 0, diag2 = 0;

	for(int i = 0;i == 0; i++)
	{
		for(int j = 0; j < COLS; j++)
		{
			row1 += numbers[0][j];
			row2 += numbers[1][j];
			row3 += numbers[2][j];
		}
	}

	for(int i = 0; i == 0; i++)
	{
		for(int j = 0; j < ROWS; j++)
		{
			col1 += numbers[j][0];
			col2 += numbers[j][1];
			col3 += numbers[j][2];
		}
	}

	int k = 2;
	for(int i = 0; i < ROWS; i++)
	{
		for(int j = 0; j == 0; j++)
		{
			diag1 += numbers[i][i];
			diag2 += numbers[i][k--];
		}
	}
	std::cout << "row1: " << row1
			  << "\nrow2: " << row2
			  << "\nrow3: " << row3
			  << "\ncol1: " << col1
			  << "\ncol2: " << col2
			  << "\ncol3: " << col3 
			  << "\ndiag1: " << diag1 
			  << "\ndiag2: " << diag2 << std::endl;

	if(row1 == row2 && row2 == row3 && row3 == col1 && 
	   col1 == col2 && col2 == col3 && col3 == diag1 && diag1 == diag2)
			std::cout << "This Lo Shu Square is magic!\n";
	else
		std::cout << "It ain't magic . . . \n";
}

void get_user_square(int numbers[][COLS], int rows)
{
	for(int i = 0; i < rows; i++)
	{
		for(int j = 0; j < COLS; j++)
		{
			std::cout << "Enter a value for row " << i + 1
					  << " column " << j + 1 << ": ";
			while(!(std::cin >> numbers[i][j]) || (numbers[i][j] < 0 || numbers[i][j] > 9) || !(verify_input(numbers[i][j])))
			{
				if(!std::cin)
				{
					std::cout << "Numbers only! Try again: ";
					std::cin.clear();
					std::cin.ignore(10000, '\n');
				}
				else if(numbers[i][j] < 0 || numbers[i][j] > 9)
				{
					std::cout << "Only numbers 1 - 9. Try again: ";
				}
				else
					std::cout << "You've already entered " << numbers[i][j] << ". Try a different number: ";
			}

		}
	}
}

bool verify_input(int number)
{
	const int SIZE = 9;
	static int arr[SIZE] = {0};
	
	static int counter = 0;

	arr[counter] = number;

	for(int i = 0; i < SIZE; i++)
		std::cout << arr[i] << std::endl;
	if(counter > 0)
	{
		for(int i = 0; i < counter; i++)
			if(arr[i] == arr[counter])
			{
				std::cout << "You already entered " << number << std::endl;
				return false;
			}
			
	}
	counter++;
	return true;
}

Topic archived. No new replies allowed.