❔ how do i convert a char to an int

i tried a bunch of different methods and none worked
62 Replies
Angius
Angius14mo ago
Just cast
MODiX
MODiX14mo ago
Angius
REPL Result: Success
(int)'a'
(int)'a'
Result: int
97
97
Compile: 322.110ms | Execution: 28.185ms | React with ❌ to remove this embed.
Angius
Angius14mo ago
Unless you want to get 7 from '7', then
MODiX
MODiX14mo ago
Angius
REPL Result: Success
char.GetNumericValue('7')
char.GetNumericValue('7')
Result: double
7
7
Compile: 508.428ms | Execution: 37.904ms | React with ❌ to remove this embed.
Aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
oh wait im stupid i dont need to just convert to int i need to delete the last value and then convert to int u know how?
Angius
Angius14mo ago
What do you mean "delete the last value"?
Aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
if the char is r i want to make it nothing convert it to int and then assign a value
Angius
Angius14mo ago
what
Aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
i want to make a char with the value r to an int with the value 1
Angius
Angius14mo ago
But... the value of r is not 1
MODiX
MODiX14mo ago
Angius
REPL Result: Success
(int)'r'
(int)'r'
Result: int
114
114
Compile: 371.133ms | Execution: 24.331ms | React with ❌ to remove this embed.
Angius
Angius14mo ago
It's 114
Aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
i want to make a char that is r to 1 and an int oh wait wait im stupipd i think wait
Angius
Angius14mo ago
I have no idea what you're saying Do you want to, like, create a dictionary of chars? A list of them so different chars are at different indexes?
Aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
nvm dont mind that i was stupid but i ahve another question
Random random = new Random();

string R = "R";
string P = "P";
string S = "S";

Console.WriteLine("Enter R (Rock), P (Paper) Or S (Scissors)");
char PlayerChoiceChar = Char.Parse(Console.ReadLine());
int PlayerChoiceInt;

int ComputerChoice = random.Next(1, 4);


if (PlayerChoiceChar.Equals(R))
{
PlayerChoiceInt = 1;
}

else if (PlayerChoiceChar.Equals(P))
{
PlayerChoiceInt = 2;
}

else if (PlayerChoiceChar.Equals(S))
{
PlayerChoiceInt = 3;
}


if (PlayerChoiceInt- == ComputerChoice)
{
Console.WriteLine("Its A Draw!");
}
Random random = new Random();

string R = "R";
string P = "P";
string S = "S";

Console.WriteLine("Enter R (Rock), P (Paper) Or S (Scissors)");
char PlayerChoiceChar = Char.Parse(Console.ReadLine());
int PlayerChoiceInt;

int ComputerChoice = random.Next(1, 4);


if (PlayerChoiceChar.Equals(R))
{
PlayerChoiceInt = 1;
}

else if (PlayerChoiceChar.Equals(P))
{
PlayerChoiceInt = 2;
}

else if (PlayerChoiceChar.Equals(S))
{
PlayerChoiceInt = 3;
}


if (PlayerChoiceInt- == ComputerChoice)
{
Console.WriteLine("Its A Draw!");
}
how do i fix it the last if doesnt work
Angius
Angius14mo ago
Why's that - there?
ZacharyPatten
ZacharyPatten14mo ago
you don't need to parse the console input into a char
Aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
doesnt matter now and it doesnt work without it
ZacharyPatten
ZacharyPatten14mo ago
it does matter because it is in your code as you shared it currently
Aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
oh its a mistake and it still doesnt work doesnt work without it
ZacharyPatten
ZacharyPatten14mo ago
then fix it 🙂
Aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
bro i dont need to fix it because its not a problem if i just add the char.parse are you stupid @ZZZZZZZZZZZZZZZZZZZZZZZZZ do you know how to fix it? the last if
Angius
Angius14mo ago
What's wrong with it?
Aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
it says CS0165 Use of unassigned local variable 'PlayerChoiceInt'
Buddy
Buddy14mo ago
Don't be rude.
Aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
but he is
Angius
Angius14mo ago
Yeah, because what will PlayerChoiceInt be if PlayerChoiceChar is ?
Aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
because its not a problem and if it is he didnt even told me how to fix it what?
Angius
Angius14mo ago
Exactly what I said
Aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
yea sorry i just dont understand that
Angius
Angius14mo ago
What will PlayerChoiceInt be, if the input char isn't R, isn't P, and isn't S, but rather ? What will it be if the input is T?
Aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
im gonna fix it later
ZacharyPatten
ZacharyPatten14mo ago
using System;

Console.Write("Enter R (Rock), P (Paper) Or S (Scissors): ");
string playerChoice = Console.ReadLine()!.Trim().ToUpper();
if (playerChoice == "R")
{
Console.Write("You chose rock...");
}
else if (playerChoice == "P")
{
Console.Write("You chose paper...");
}
else if (playerChoice == "S")
{
Console.Write("You chose scissors...");
}
else
{
Console.Write("Invalid Input...");
}
using System;

Console.Write("Enter R (Rock), P (Paper) Or S (Scissors): ");
string playerChoice = Console.ReadLine()!.Trim().ToUpper();
if (playerChoice == "R")
{
Console.Write("You chose rock...");
}
else if (playerChoice == "P")
{
Console.Write("You chose paper...");
}
else if (playerChoice == "S")
{
Console.Write("You chose scissors...");
}
else
{
Console.Write("Invalid Input...");
}
if you want you could simplify your code to this
Angius
Angius14mo ago
But... that's why your last if doesn't work Do you want to fix it now? I thought you do But if not, then why ask?
Aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
ok i fixed it now
Random random = new Random();

string R = "R";
string P = "P";
string S = "S";

Console.WriteLine("Enter R (Rock), P (Paper) Or S (Scissors)");
char PlayerChoiceChar = Char.Parse(Console.ReadLine());
int PlayerChoiceInt;

int ComputerChoice = random.Next(1, 4);


if (PlayerChoiceChar.Equals(R))
{
PlayerChoiceInt = 1;
}

else if (PlayerChoiceChar.Equals(P))
{
PlayerChoiceInt = 2;
}

else if (PlayerChoiceChar.Equals(S))
{
PlayerChoiceInt = 3;
}

else
{
Console.WriteLine("Value Of Char Has To Be Either R, P Or S");
}


if (PlayerChoiceInt == ComputerChoice)
{
Console.WriteLine("Its A Draw!");
}
Random random = new Random();

string R = "R";
string P = "P";
string S = "S";

Console.WriteLine("Enter R (Rock), P (Paper) Or S (Scissors)");
char PlayerChoiceChar = Char.Parse(Console.ReadLine());
int PlayerChoiceInt;

int ComputerChoice = random.Next(1, 4);


if (PlayerChoiceChar.Equals(R))
{
PlayerChoiceInt = 1;
}

else if (PlayerChoiceChar.Equals(P))
{
PlayerChoiceInt = 2;
}

else if (PlayerChoiceChar.Equals(S))
{
PlayerChoiceInt = 3;
}

else
{
Console.WriteLine("Value Of Char Has To Be Either R, P Or S");
}


if (PlayerChoiceInt == ComputerChoice)
{
Console.WriteLine("Its A Draw!");
}
Angius
Angius14mo ago
You didn't PlayerChoiceInt will still be unassigned if the input is @
Aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
yea i know it just ends the program and thats not a problem but all the other ifs dont work the else works fine but the first if and the 2 else ifs dont work either how do i check if the letter is r p or s
ZacharyPatten
ZacharyPatten14mo ago
please elaborate on what your current question is.
how do i check if the letter is r p or s
isn't clear what you are currently struggling with because you already have if statements doing that at this point you seem like you need to finish the rest of your if statements comparing the player and computer choices
Aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
the if statement didnt work
Will
Will14mo ago
i don't even think this will compile, PlayerChoiceInt is being compared to something when it can be potentially unassigned
Aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
nvm i fixed it i changed the strings R P and S to chars i just need to make PlayerChoiceInt public and i dont know how
ZacharyPatten
ZacharyPatten14mo ago
why would you say you need to make it public? what is giving you the impression that is your issue?
Will
Will14mo ago
you either need to assign it a default value, or make it a field which will give it a default value automatically
ZacharyPatten
ZacharyPatten14mo ago
public is an access modifier. thus far the code you have shared is only top level statements. top level statements cannot include access modifiers
Aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
just assign it to 0 or something? k
Denis
Denis14mo ago
So much confusion in this thread
Aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
yea i know i asked like 5 different questions but i got it the draw works now i need to make the other wins and losses
ZacharyPatten
ZacharyPatten14mo ago
and do you need help with those?
Aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
maybe yea oh wait maybe not
Random random = new Random();

char R = 'R';
char P = 'P';
char S = 'S';

Console.WriteLine("Enter R (Rock), P (Paper) Or S (Scissors)");
char PlayerChoiceChar = Char.Parse(Console.ReadLine());
int PlayerChoiceInt = 0;

int ComputerChoice = random.Next(1, 4);


if (PlayerChoiceChar.Equals(R))
{
PlayerChoiceInt = 1;
}

else if (PlayerChoiceChar.Equals(P))
{
PlayerChoiceInt = 2;
}

else if (PlayerChoiceChar.Equals(S))
{
PlayerChoiceInt = 3;
}

else
{
Console.WriteLine("Value Of Char Has To Be Either R, P Or S");
}


if (PlayerChoiceInt == ComputerChoice)
{
Console.WriteLine("Its A Draw!");
}

if (ComputerChoice == 1 && PlayerChoiceInt == 2)
{
Console.WriteLine("You Won!");
}

if (ComputerChoice == 1 && PlayerChoiceInt == 3)
{
Console.WriteLine("You Lost!");
}

if (ComputerChoice == 2 && PlayerChoiceInt == 1)
{
Console.WriteLine("You Lost!");
}

if (ComputerChoice == 2 && PlayerChoiceInt == 3)
{
Console.WriteLine("You Won!");
}

if (ComputerChoice == 3 && PlayerChoiceInt == 1)
{
Console.WriteLine("You Lost");
}

if (ComputerChoice == 3 && PlayerChoiceInt == 2)
{
Console.WriteLine("You Won!");
}
Random random = new Random();

char R = 'R';
char P = 'P';
char S = 'S';

Console.WriteLine("Enter R (Rock), P (Paper) Or S (Scissors)");
char PlayerChoiceChar = Char.Parse(Console.ReadLine());
int PlayerChoiceInt = 0;

int ComputerChoice = random.Next(1, 4);


if (PlayerChoiceChar.Equals(R))
{
PlayerChoiceInt = 1;
}

else if (PlayerChoiceChar.Equals(P))
{
PlayerChoiceInt = 2;
}

else if (PlayerChoiceChar.Equals(S))
{
PlayerChoiceInt = 3;
}

else
{
Console.WriteLine("Value Of Char Has To Be Either R, P Or S");
}


if (PlayerChoiceInt == ComputerChoice)
{
Console.WriteLine("Its A Draw!");
}

if (ComputerChoice == 1 && PlayerChoiceInt == 2)
{
Console.WriteLine("You Won!");
}

if (ComputerChoice == 1 && PlayerChoiceInt == 3)
{
Console.WriteLine("You Lost!");
}

if (ComputerChoice == 2 && PlayerChoiceInt == 1)
{
Console.WriteLine("You Lost!");
}

if (ComputerChoice == 2 && PlayerChoiceInt == 3)
{
Console.WriteLine("You Won!");
}

if (ComputerChoice == 3 && PlayerChoiceInt == 1)
{
Console.WriteLine("You Lost");
}

if (ComputerChoice == 3 && PlayerChoiceInt == 2)
{
Console.WriteLine("You Won!");
}
i think its done
ZacharyPatten
ZacharyPatten14mo ago
else if ((PlayerChoiceInt is 1 && ComputerChoice is 3) ||
(PlayerChoiceInt is 2 && ComputerChoice is 1) ||
(PlayerChoiceInt is 3 && ComputerChoice is 2))
{
Console.WriteLine("You win!");
}
else if ((PlayerChoiceInt is 1 && ComputerChoice is 3) ||
(PlayerChoiceInt is 2 && ComputerChoice is 1) ||
(PlayerChoiceInt is 3 && ComputerChoice is 2))
{
Console.WriteLine("You win!");
}
you can reduce the size of that by a lot
ZacharyPatten
ZacharyPatten14mo ago
you can || multiple conditions together || is "or"
ZacharyPatten
ZacharyPatten14mo ago
also in general you should use else if instead of a bunch of if when appropriate also to help with your code's readability you can also do this:
const int Rock = 1;
const int Paper = 2;
const int Scissors = 3;
const int Rock = 1;
const int Paper = 2;
const int Scissors = 3;
and later do
else if ((PlayerChoiceInt is Rock && ComputerChoice is Scissors) ||
(PlayerChoiceInt is Paper && ComputerChoice is Rock) ||
(PlayerChoiceInt is Scissors && ComputerChoice is Paper))
{
Console.WriteLine("You win!");
}
else if ((PlayerChoiceInt is Rock && ComputerChoice is Scissors) ||
(PlayerChoiceInt is Paper && ComputerChoice is Rock) ||
(PlayerChoiceInt is Scissors && ComputerChoice is Paper))
{
Console.WriteLine("You win!");
}
that is much more readable than just using 1, 2, and 3
Aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
thx for all the help bro and im sorry that i called you stupid
ZacharyPatten
ZacharyPatten14mo ago
just curious, what made you want to make a rock paper scissors app in console? 🙂
Aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
oh i had homework and it was a challenge question
ZacharyPatten
ZacharyPatten14mo ago
if you liked that assignment, making console games is a great way to learn. you can make more games than just rock paper scissors. and there are plenty of examples on GitHub to help you out 😉
Aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
ok thx ill check that out
ZacharyPatten
ZacharyPatten14mo ago
if you want a link to some just ask. regardless, good luck learning 🙂
Accord
Accord14mo ago
Was this issue resolved? If so, run /close - otherwise I will mark this as stale and this post will be archived until there is new activity.
Want results from more Discord servers?
Add your server