: hey group
:
: I am trying to practice and learn basic C# statements and syntax, and I fail to figure out how to get a user input from the keyboard. I've notice that
:
: :Console.Write(); //console.WriteLine();
:
: writes to the screen, but when I try to save an input to a variable, it seems to only work for interger numbers.
:
: :Console.Read(); //console.ReadLine();
:
: My problem is I am trying to read characters from the input and save them. I get an error about converting int to char. Is there any command so that I can save an input from the user and save as in a char variable.
:
:
: :using System;
: :class classname {
: : public static void {
: : char discipline;
: : Console.Write("enter A or B");
: : discipline = Console.Read(); // Cannot implicitly convert type
: // 'int' to 'char'
:
: :
:
: -=The Best Has Yet To Come=-
:
You might want to read about types and type casting also :) C# doesn't cast from "bigger" types to smaller ones. Console.Read() returns Int32 which is 32 bit integer and char is 8 bit integer. This means that you'll have to explicitly cast it like this:
int i = 33;
char c = (char)i;
Other way round it works implicitly: i = c;