-1

why does n doesnt get the number i type in the program? I typed 1 and it got 49 :S

Random arrN = new Random();
int[] arr;
arr = new int[100];
int n;
bool game_result = false;

for (int i = 0; i < 100; i++)
{
    arr[i] = arrN.Next(0, 1000);
}

Console.WriteLine("what game do you want to play?\n1- guessing one number.\n2- guessing numbers in range.\n3- exit");
n = Console.Read();
wingerse
  • 3,670
  • 1
  • 29
  • 61
Tom
  • 3
  • 2

2 Answers2

1

Console.Read returns a number representing the character entered so you need to convert it:

var n = Console.Read();
var ch = Convert.ToChar(n);
var value = int.Parse(ch.ToString());
DavidG
  • 113,891
  • 12
  • 217
  • 223
  • isnt there console.read for int, like i tried? – Tom Oct 14 '15 at 14:02
  • No, what happens if the user presses a letter or punctuation? – DavidG Oct 14 '15 at 14:05
  • i came from c++ where it depends on the type of the var, so when my var is int i want to have something like cin which will just get the number to the var. how should i do it in my program? – Tom Oct 14 '15 at 14:07
0

Console.Read returns character code, not the character itself.
This will work:

var ch = (char)Console.Read(); // ch will contain '1'
Dennis
  • 37,026
  • 10
  • 82
  • 150