int gameTurns = 12;
do
{
gameTurns -= 1;
Console.WriteLine(" You have " + (gameTurns) + " attempts left.");
Console.WriteLine();
Console.WriteLine();
string userEnteredPassword = "";
Console.WriteLine("Enter a password of 4 digits ");
userEnteredPassword = Console.ReadLine();
for (int i = 0; i < numbersToGuess.Length; i++)
{
numbersFromPlayer[i] = Convert.ToInt16(userEnteredPassword[i]);
Console.WriteLine(numbersFromPlayer[i]);
}
Note that numbersToGuess was declared like this:
for (int i = 0; i <= 3; i++)
{
numbersToGuess[i] = Convert.ToInt16(sequence.Next(9));
Console.WriteLine(numbersToGuess[i]);
}
When I run my code with the values: 1, 2, 3, 4, it prints: 49, 50, 51, 52.
Yes, because 49 is the UTF-16 code unit for the character '1'.
If you entered "ABCD", it would show 64, 65, 66, 67.
Convert.ToInt16(char)
is documented as:
Converts the value of the specified Unicode character to the equivalent 16-bit signed integer.
If you want to convert each character so that '0' became 0, '1' became 1 etc, you could use char.GetNumericValue(char)
:
numbersFromPlayer[i] = (short) char.GetNumericValue(userEnteredPassword[i]);
Or if you were happy to trust the player to enter ASCII digits:
numbersFromPlayer[i] = (short) (userEnteredPassword[i] - '0');
See more on this question at Stackoverflow