NumberGuessing can't check "O".......

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
#include <iostream>
#include <ctime>

using namespace std;
void prompt (int);
void shuffle (int* const, int);
void play (int* const, int);
bool check (int* const, int*, int, int);


int main()
{
	int n;
	cout << "Enter total number: ";
	cin >> n;
	int* arr = new int[n];
	prompt(n);
	shuffle (arr, n);
	play (arr, n);
	system ("pause"); 
	return 0;
}

void prompt (int n)
{
	cout << "Number Guessing " << endl;
	cout <<"Enter " << n << " digits (1- " << n << ") separated by a space " << endl;
	cout << "---------------------------------------------------------------";
}

void shuffle (int* const randNoArr, int size )
{
	srand ((unsigned int)time(NULL));
	for( int i=0; i<(size-1); i++ )
	{
		int r = i + (rand() % (size-i));
		int temp = randNoArr[i];
		randNoArr[i] = randNoArr[r];
		randNoArr[r] = temp;
	}
}

void play (int* const randNoArr, int n)
{
	int* input = new int[n];
	int round=1;
	do
	{
		cout << "\nRound " << round 
			<< "\nEnter Guess:";
		for (int i=0; i<n; i++)
		{
			cin >> input[i];
		}
		round++;
	}
	while (check(randNoArr, input, n,round) == false);
}

bool check (int* const randNoArr, int* input, int n,int round)
{
	int checking = 0;

	for (int i=0; i<n; i++)
	{
		if (randNoArr[i] == input[i])
		{	
			cout << "O ";
			checking += 1;
		}
		else
			cout << "X ";
	}
	cout << "\n----------------------------------------------------------";
	
		if (n != checking)
			return false;
		else
		{
		cout << "\nCongratulations! You win the game in "
		<< round-1 << " steps\n";
		return true;
		}
}


This is my program....
and when I run it, it cannot check "0"
what's wrong is it??

This is my output

Enter total number: 5 
Number Guessing Enter 5 digits (1-5) separated by a space 
----------------------------------------------------------- 
Round 1 
Enter Guess: 1 2 3 4 5 
X X X X X 
----------------------------------------------------------- 
Number Guessing Enter 5 digits (1-5) separated by a space 
----------------------------------------------------------- 
Round 2 Enter Guess: 1 2 4 3 5 
X X X X X 
----------------------------------------------------------- 
Number Guessing 
Enter 5 digits (1-5) separated by a space 
----------------------------------------------------------- 
Round 3 Enter Guess:
Last edited on
You didn't initialize your array, so there is random values in it. It is easy to spot, just modify you check function:
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
bool check (int* const randNoArr, int* input, int n,int round)
{
    int checking = 0;

    for (int i=0; i<n; i++) {
        if (randNoArr[i] == input[i]) {
            cout << "O ";
            checking += 1;
        } else
            cout << "X ";
    }
    cout << "\n----------------------------------------------------------";
//DEBUG
    cout << endl;
    for(int i = 0; i<n; ++i)
        cout << randNoArr[i] << ' ';
//END DEBUG
    if (n != checking)
        return false;
    else {
        cout << "\nCongratulations! You win the game in "
             << round-1 << " steps\n";
        return true;
    }
}
The array is not assigned any initial values before the shuffle?
1
2
3
4
5
    // fill array
    for (int i=0; i< n; i++)
    {
        arr[i] = i+1;
    }
Thank MiiNi and Chervil

I know how to do now
Topic archived. No new replies allowed.