Converting an int to a Char c#?

anonuser

I want to be able to Read a value from the operator M/F for male or female and return seperate blocks of text for each respective entry.

Char gender;
int TempValue;
Console.WriteLine("Please enter your name");
Console.CursorVisible=true;
String name = Console.ReadLine();
Console.WriteLine(" Greetings, faire travler {0} you are about to depart on a rather exciting adventure, but first are you male or female?", name);
Console.WriteLine("(M/F)");

TempValue = Console.Read();
Console.WriteLine("{0}", TempValue);
gender = Char.Parse(TempValue);

Now i get an error saying I "cannot convert from 'INT' to 'string' which I really dont understand. Becuase im trying to parse it into a char not a string.

Dennis_E

Console.Read() returns the ascii value of the character you entered. (In case of 'M' it's 77) char.Parse(TempValue) expects TempValue to be a string and that it contains exactly one character. So, this is what you would use to convert the string "M" to the character 'M'. But you don't have the string "M"; you have the int 77.
In this case, you can simply cast it:

char gender = (char)TempValue;

Alternatively, you can use Console.ReadLine(), which returns a string and then take the first character:

string input = Console.ReadLine();
char gender = input[0];

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Converting an int to a char in C?

From Dev

Converting an int to a char in C

From Dev

Converting int to char C formula

From Dev

Issues with converting int to char in C

From Dev

C Converting Char array (strings) into int array

From Dev

Converting int to char?

From Dev

Converting char[] to Int[]

From Dev

Converting from char to int

From Dev

String converting to Char and then to Int

From Dev

Converting int to char?

From Dev

Converting an int to a char in PHP has a different output in C#

From Dev

C: Converting unsigned char array to signed int (vice versa)

From Dev

Converting an int to a char in PHP has a different output in C#

From Dev

Converting from a std::vector<int> to a char[] C++

From Dev

converting unsigned char > 255 to int

From Dev

Converting unsigned char to int and short

From Dev

Java: converting char arrays to int

From Dev

Converting unsigned char array to int

From Dev

Converting char to int '23' > 23

From Dev

Converting int to char, infinite execution

From Dev

Converting Char * to Uppercase in C

From Dev

Binary to char converting in c

From Dev

Converting Const char * to Unsigned long int - strtoul

From Dev

Converting to Char/String from Ascii Int in Swift

From Dev

Converting int to Unicode char in a backwards compatible way

From Dev

Converting string to char and int data types

From Dev

Manually converting a char to an int - Strange behaviour

From Dev

Converting integer to unsigned char* (int 221 to "\xdd")

From Dev

Converting int to Unicode char in a backwards compatible way