Strange Symbols

I am writing a number guessing program. I have been debugging along the way to ensure that everything is running smoothly as I progress. Unfortunately, somewhere along the way something has gone terribly wrong. When any of my char arrays are displayed in output it is not displaying the numbers as it had when I started. Instead, each time several strange symbols are displayed. Any reason why this is happening now?

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

using namespace std;

int main()
{
	srand(static_cast<unsigned int>(time(NULL)) );

	char number[5];
	char guess[5];
	char answer[5];

	string userNumber;

	for (int i = 0; i < 5; i++)
	{
		number[i] = rand() % 10;
		cout << number[i];
	}

	cout << "Greetings! Please guess a 5 digit number: " << endl;
	cin >> userNumber;

	for (int j = 0; j < 5; j++)
	{
		guess[j] = userNumber.at(j);
	}

    while (guess != number)
	{
		for (int k = 0; k < 5; k++)
		{
			if (guess[k] == number[k])
			{
				answer[k] = 1;
			}
			else if (guess[k] == number[0] || guess[k] == number[1] || guess[k] == number[2] || guess[k] == number[3] || guess[k] == number[4])
			{
				answer[k] = 2;
			}
			else answer[k] = 0;
		}

		cout << "Incorrect!" << endl;
		cout << answer[0], answer[1], answer [2], answer[3], answer[4];
		cout << " 1 = correct number in the correct place" << endl;
		cout << " 2 = correct number in the wrong place" << endl;
		cout << " 0 = incorrect number" << endl;
		cout << "Please guess another 5 digit number: " << endl;
		cin >> userNumber;

		for (int j = 0; j < 5; j++)
		{
			guess[j] = userNumber.at(j);
		}
	}

	cout << "Well done! You guessed the correct number!" << endl;

	system ("pause");
	return 0;
}
You are doing a couple of things wrong here:

Lets take two array:

1
2
char number[5];
char guess[5];


First, If you compare these two arrays like while(guess!=number) then guess will NEVER be equal to the number because in this case != is not checking if the values of both arrays are equal. Instead it is checking that either references (Address Locations) of both arrays are equal or not which in this case CAN NEVER BE EQUAL. So your WHILE LOOP WILL RUN FOREVER.

To Fix it, You need to write a function that will compare arrays character by character like "isEqual()" written below.

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

using namespace std;

bool isEqual(char arr_1[], char arr_2[], int size)
{    
    for(int i=0; i<size; i++)
    {
        if(arr_1[i] != arr_2[i])
            return false;
    }
    return true;
}

int main()
{
	srand(static_cast<unsigned int>(time(NULL)) );

	char number[5] = {'1', '2', '3', '4', '5'};
	char guess[5] = {'1', '2', '3', '4', '5'};
	char new_guess[5] = {'1', '0', '3', '4', '5'};
	
        cout << isEqual(number,guess,5);
        cout << endl;
        cout << isEqual(number, new_guess, 5);
    
        cout << endl;
        cin.ignore();
        return 0;
}


Output:

1
0


Now you can compare elements of two arrays like this: while( !isEqual(number,guess,5) ){.....}

Note:
If content of array is same, the function will return 1
If content of array is different, the function will return 0
Last edited on
Great suggestion, thank you. However I am still getting strange symbols instead of numbers when my char arrays are displayed in the output. The only exception being the users guess. Did I implement it correctly?

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

using namespace std;

bool isEqual(char array_1[], char array_2[], int size);

int main()
{
	srand(static_cast<unsigned int>(time(NULL)) );

	char number[5];
	char guess[5];
	char answer[5];

	string userNumber;

	for (int i = 0; i < 5; i++)
	{
		number[i] = rand() % 10;
		cout << number[i];
	}

	cout << "Greetings! Please guess a 5 digit number: " << endl;
	cin >> userNumber;

	for (int j = 0; j < 5; j++)
	{
		guess[j] = userNumber.at(j);
		cout << guess[j];
	}

    while (!isEqual(number, guess, 5) )
	{
		for (int k = 0; k < 5; k++)
		{
			if (guess[k] == number[k])
			{
				answer[k] = 1;
			}
			else if (guess[k] == number[0] || guess[k] == number[1] || guess[k] == number[2] || guess[k] == number[3] || guess[k] == number[4])
			{
				answer[k] = 2;
			}
			else answer[k] = 0;
			cout << answer[k];
		}

		cout << "Incorrect!" << endl;
		cout << " 1 = correct number in the correct place" << endl;
		cout << " 2 = correct number in the wrong place" << endl;
		cout << " 0 = incorrect number" << endl;
		cout << "Please guess another 5 digit number: " << endl;
		cin >> userNumber;

		for (int j = 0; j < 5; j++)
		{
			guess[j] = userNumber.at(j);
		}
	}

	cout << "Well done! You guessed the correct number!" << endl;

	system ("pause");
	return 0;
}

bool isEqual(char array_1[], char array_2[], int size)
{
	for (int i = 0; i < size; i++)
	{
		if (array_1[i] != array_2[i])
		return false;
	}
	return true;
}
That is because by default, when you print a char it prints the character associated with that value. If you want to print numbers, you'll either need to cast it to an int or something first, or make it have the actual value of a number. For example, in line 38-46:
38
39
40
41
42
43
if (guess[k] == number[k])
    answer[k] = '1'; // note the quotation marks
else if (...)
    answer[k] = '2';
else
    answer[k] = '0';


You'll need to do this throughout your code for your char arrays. Alternately, just store them as integers instead - it makes it a lot easier. You'll just need to convert the user's input into numbers rather than characters, which isn't that hard to do.
Thank you, this is what I have been struggling with. I don't know how to get the user's input added into an int array as 5 separate digits, unless I ask for each digit separately. So alternatively I was using char arrays but that hasn't been working either. Any suggestions?
Alright, I have it running smoothly now. Thank you for steering me in the right direction!
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 <string>
#include <ctime>

using namespace std;

bool isEqual(int array_1[], int array_2[], int size);

int main()
{
	srand(static_cast<unsigned int>(time(NULL)) );

	const int MAX = 5;

	int number[MAX];
	int guess[MAX];
	int answer[MAX];

	int userNumber;

	for (int i = 0; i < 5; i++)
	{
		number[i] = rand() % 10;
		cout << number[i];
	}

	cout << endl;

	cout << "Greetings! Please guess a 5 digit number: " << endl;
	cin >> userNumber;

	guess[0] = userNumber / 10000;
	guess[1] = (userNumber % 10000) / 1000;
	guess[2] = ((userNumber % 10000) % 1000) / 100;
	guess[3] = (((userNumber % 10000) % 1000) % 100) / 10;
	guess[4] = (((userNumber % 10000) % 1000) % 100) % 10;

    while (!isEqual(number, guess, 5) )
	{
		for (int k = 0; k < 5; k++)
		{
			if (guess[k] == number[k])
			{
				answer[k] = 1;
			}
			else if (guess[k] != number[0] || guess[k] != number[1] || guess[k] != number[2] || guess[k] != number[3] || guess[k] != number[4])
			{
				answer[k] = 0;
			}
			else answer[k] = 1;
			cout << answer[k];
		}

		cout << endl;
		cout << "Guess again!" << endl;
		cout << " 1 = correct number in the correct place" << endl;
		cout << " 2 = correct number in the wrong place" << endl;
		cout << " 0 = incorrect number" << endl;
		cout << "Please guess another 5 digit number: " << endl;
		cin >> userNumber;
		
		guess[0] = userNumber / 10000;
		guess[1] = (userNumber % 10000) / 1000;
		guess[2] = ((userNumber % 10000) % 1000) / 100;
		guess[3] = (((userNumber % 10000) % 1000) % 100) / 10;
		guess[4] = (((userNumber % 10000) % 1000) % 100) % 10;
	}

	cout << endl;
	cout << "Well done! You guessed the correct number!" << endl;

	system ("pause");
	return 0;
}

bool isEqual(int array_1[], int array_2[], int size)
{
	for (int i = 0; i < size; i++)
	{
		if (array_1[i] != array_2[i])
		return false;
	}
	return true;
}
Topic archived. No new replies allowed.