Stuck on this for a while now (code comes to a point where not showing output)

Hello, I've been working on this mini game project , and there's this thing I've been stuck on for a while now , if someone can tell me what's wrong with my code below . (I've used this way to get unique random numbers between -50 and 50 for 2 class templates arrays , our instructor has limited us with the given classes and objects to work with , yes...):

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
#include<iostream>
#include<array>
#include<cstdlib>
#include<iomanip>
#include<ctime>
using namespace std;
bool UniqueNums(array<array<int, 8>, 8>&a, const int col, const int row, int check)
{
	bool result = false;
	for (int i = 0; i < row; i++)
	{
		for (int j = 0; j < col; j++)
		{
			if (check == a[i][j])
				result = true;
		}

	}
	return result;
}
void FillFunction(array<array<int, 8>, 8>&user, int c, int r)
{

	for (int i = 0; i < r; i++)
	{
		int dup;
		for (int j = 0; j < c; j++) 
		{
			do{
				user[i][j] = 50 - rand() % 101;
				dup = user[i][j];

			} while (UniqueNums(user, 8, 8, dup)!=false);
		}
	}
}
void PrintArray(array<array<int, 8>, 8>&user, int c, int r)
{
	int  counter = 0;
	for (int k = 0; k < r; k++)
		for (int h = 0; h < c; h++)
		{
			cout << setw(4.5) << user[k][h] << setw(5);
			counter++;
			if (counter % 8 == 0)
				cout << endl;
		}
	cout << endl;
}
void main()
{
	unsigned int srand(time(0));
	int menu_input;
	int magic_input;
	int magic_number;
	cout << "Game Menu : " << endl;
	cout << "-----------" << endl;
	cout << "1- Play" << endl;
	cout << "2- Quit" << endl;
	cout << endl;
	cout << "Enter your choice : ";
	cin >> menu_input;
	cout << endl;

	if (menu_input == 2)
		cout << "END" << endl;
	else {
		cout << "Magical Number(between -50 & 50):" << endl;
		cout << "*********************************" << endl;
		cout << "1- Pick a number" << endl;
		cout << "2- Let the computer pick a number" << endl;
		cout << endl;
		cout << "Enter your choice : ";
		cin >> magic_input;
		if (magic_input == 2)
		{
			magic_number = 50 - rand() % 101;
			cout << endl;
			cout << "Magical Number is : " << magic_number << endl;
			cout << endl;
		}
		if (magic_input == 1)
		{
			cout << "Enter Magical Number: ";
			cin >> magic_number;
			cout << endl;
		}
		int computer_row_index1;
		int computer_column_index1;
		int user_row_index1;
		int user_column_index1;
		//*Initializing the 2 arrays
		array<array<int, 8>, 8>user_array{};
		array<array<int, 8>, 8>computer_array{};

		if (magic_input == 2 || magic_input == 1) {
			cout << "          " << "User's Grid : " << endl;
			cout << endl;
			//*Filling and Printing user array
			FillFunction(user_array, 8, 8);
			PrintArray(user_array, 8, 8);
			cout << endl;
			cout << endl;
			cout << "        " << "Computer's Grid : " << endl;
			cout << endl;
			//*FIlling and Printing pc array
			FillFunction(computer_array, 8, 8);
			PrintArray(computer_array, 8, 8);
			cout << endl;

		}
		
	}
	system("pause");
}

Thank you :)
Last edited on
Use code tags.
What exactly are you having problems with?

Some obvious issues:

Line 43: You can't do a setw() to a fractional number. Well, you can, but it's meaningless and is going to get truncated.

Line 50: main() must return type int.


Yes , even after fixing those , try running it . I get to the point :"User's Grid " , and then nothing happens , not even the end of the program . (I'm running it on Microsoft visual studio btw )
visual has a debugger built in, but it may be a sledgehammer for a fly... you can also just stick a couple of print statements in (entering fill function, exiting fill function, entering print function, exiting print function should do it here) to see where its getting stuck.

it could just be that you have ~N^4th runtime here. If it *frequently* has duplicates, you have for rows, for cols, for ever until not duplicated, for everything check duplicates... it could just take a long time. That seems unlikely, but it hinges off how often you hit repeats. N here is only 64, but 64^4 is 16 million operations... its a bit of a yikes for what you are trying to do. I would redo the duplicates into a lookup table and eliminate the loops for that part, or better still, make a vector of every possible value, shuffle it, and peel off the first block.

Or it could be that something got stuck, infinite loop or something, or a forgotten cin ...
Last edited on
Topic archived. No new replies allowed.