#I want to convert Console.ReadLine into int with the TryParse method
1 messages · Page 1 of 1 (latest)
Console.WriteLine("You need to get the number in under 10 tries then you win, if not you lose!");
Console.Write("Pick a number between 1 and 10: ");
int guess;
if(guess = int.TryParse(Console.ReadLine(), out int num)
{
while (guess <= 0 || guess > 10)
{
Console.WriteLine("Bro, just: ");
guess = Convert.ToInt32(Console.ReadLine());
}
} else
{
Console.WriteLine("You are done Brotha");
}
printNumber();
Console.WriteLine("--------------");
printNumber();
Console.WriteLine("--------------");
printNumber();
Console.ReadKey();
void printNumber()
{
int number = 1;
int num01 = 0;
int attempts = 0;
Random numGen = new Random();
while (num01 != guess && attempts <= 9)
{
Console.ReadKey();
num01 = numGen.Next(1, 11);
Console.WriteLine(number + ". You got: " + num01);
attempts++;
number++;
}
if (num01 == guess && attempts <= 9 && attempts > 1)
{
Console.WriteLine("You WIN!");
Console.WriteLine("And it took you " + attempts + " attempts to roll " + guess);
}
else if (num01 == guess && attempts == 1)
{
Console.WriteLine("You WIN!");
Console.WriteLine("Holy shit!");
}
else if (num01 == guess && attempts == 10)
{
Console.WriteLine("You WIN!");
Console.WriteLine("And it took you " + attempts + " attempts to roll " + guess);
Console.WriteLine("Lucky one Buddy");
}
else
{
Console.WriteLine("You LOSE!");
}
}```
oh wait
xD
maybe I realized smth
I don't think I fixed it
Console.WriteLine("You need to get the number in under 10 tries then you win, if not you lose!");
Console.Write("Pick a number between 1 and 10: ");
int guess;
if(guess = int.TryParse(Console.ReadLine(), out int num)
{
while (guess <= 0 || guess > 10)
{
Console.WriteLine("Bro, just: ");
guess = int.TryParse(Console.ReadLine(), out int num)
}
} else
{
Console.WriteLine("You are done Brotha");
}
printNumber();
Console.WriteLine("--------------");
printNumber();
Console.WriteLine("--------------");
printNumber();
Console.ReadKey();
void printNumber()
{
int number = 1;
int num01 = 0;
int attempts = 0;
Random numGen = new Random();
while (num01 != guess && attempts <= 9)
{
Console.ReadKey();
num01 = numGen.Next(1, 11);
Console.WriteLine(number + ". You got: " + num01);
attempts++;
number++;
}
if (num01 == guess && attempts <= 9 && attempts > 1)
{
Console.WriteLine("You WIN!");
Console.WriteLine("And it took you " + attempts + " attempts to roll " + guess);
}
else if (num01 == guess && attempts == 1)
{
Console.WriteLine("You WIN!");
Console.WriteLine("Holy shit!");
}
else if (num01 == guess && attempts == 10)
{
Console.WriteLine("You WIN!");
Console.WriteLine("And it took you " + attempts + " attempts to roll " + guess);
Console.WriteLine("Lucky one Buddy");
}
else
{
Console.WriteLine("You LOSE!");
}
}```
I think I still need a little bit of help in this one
I'm kinda stuck
where is the yashiro guy
what is the error?
Okay, so what does that error mean?
okay did the tutorial show some code? because the way you've written the code it seems like you didn't follow the tutorial exactly
i might double check there first, or I can explain what's going wrong
like
I watched a bunch of vids
and none of them is really helping
so I'm trying to kinda figure it out by myself
helping with...?
solving the convert problem
maybe you could structure your code more off the tutorials
the convert function contains an out parameter, you could learn more about this to fix your code
what
okay I'll try to explain what's going on
first what is strange about your code is that you are assigning a variable inside the condition part of the if statement
do you see the code I am talking about?
yep
what was your thought process for doing this?
but it doesn't work that way
that's why i put it in there
It didin't work the other way
Could you explain more about the different ways you tried? Remember, coding shouldn't be guessing things
eh
it is when you're beggining
I can understand why you might be tempted to guess, since it can be hard to find the right code. But in the long run it's much more useful to learn why things are correct instead of guessing
anyways, going back to the if statement, what normally goes inside the parentheses?
nope
okay good
now read the parse function and tell me what it returns. do you know about functions and what they return?
they return a variable
yes, and in this case, what is the type of the variable?
int
are you sure? did you read the function description?
yeah so does your error make more sense now?
What is the meaning of that return value? Like if the parse function returned false what does that mean?
it didin't convert
it wasn't successful, yeah. and true means the conversion (or parsing) was successful
exactly
So knowing this, how might you rewrite some of your code
And maybe you have other questions now about how this parse function works if it doesn't return an int
can you elaborate?
I was trying to change it so the tryparse will be above the if statement
Okay show me what you've got
It's okay if it has errors, we'll work through it together
okay that's a start. remind me what type the parse function returns?
bool
maybe you see a problem?
okay I think you are confusing something
the return value of the function is what the overall function "becomes"
like you can imagine replacing the entire function with its return value.
so for example, if the parse was successful, you might get
int guess = int.TryParse(...) would "become" int guess = true
does this make sense?
like if I had a function that looked like
int Add(int a, int b)
{
return a + b;
}
int c = Add(1, 2);
// you could think about it like:
int c = 3; // so because Add(1, 2) returns 3, you can imagine that the entire function call Add(1, 2) was replaced with 3
you could imagine that the Add(1, 2) was replaced with its return value, which is 3
it doesn't actually happen this way, but it might be useful to think about it this way
what part are you confused about?
yeah
get it
okay great!
so why is what you did here not correct? i see you did change a type from int to bool...
like this what you wrote
I get that you can replace an entire method with just smth smaller
okay. the point i was trying to illustrate it that a function can be "replaced" by its return value
but the return value is int the function
bro that shit is so confusing
don't worry about the parsed int value yet. just work on using the true false return value to indicate success or failure
alr
i mean, ask any questions you have and ill see if i haven't thought of them or if we will get to them later
if you want to continue, modify the code to take into account the return type of the parse method
so right now you've written int guess = int.TryParse(...). why is this wrong?
this is wrong?
haha we just went over this. the int.TryParse function returns a bool, meaning true or false.
so when the function returns this bool, it tries to assign the value to guess, but guess is an int
right? like int guess = true or int guess = false
which is not allowed because the types do not match
how about creating a new variable? the return value doesn't represent your guess, it represents whether the function succeeded, right?
yhm
go ahead and post the new code
okay LOL
haha I'll be frre again in like 3 hrs
maybe someone else can pick up where we left off
or you can wait!
anybody there to help?
?
I see conversation has already happened here
yyea

so
I'm trying to use the tryparse method for the first time
and it was not really working
"not working" how?
"It's not working" is not helpful
In order for a question to be answered, it must specify what exactly is wrong. Stating simply that "it doesn't work" is not sufficient.
Source: https://idownvotedbecau.se/itsnotworking
Please elaborate on your question by including all relevant details. What do you think is the problem? Have you tried to fix it? If you have, why didn't that work?
like for me
I watched like a bunch of tutorials
and none of them has helped me
so I asked here for help
and've been kinda stuck at the bool part of try parse
wait
I'll send you what we are working with
That's fine I can still explain how TryParse works, if you need that
okay
Console.WriteLine("You need to get the number in under 10 tries then you win, if not you lose!");
Console.Write("Pick a number between 1 and 10: ");
int guess = int.TryParse(Console.ReadLine(), out int num)
if (guess true)
{
while (guess <= 0 || guess > 10)
{
Console.WriteLine("Bro, just: ");
guess = int.TryParse(Console.ReadLine(), out int num)
}
}
else
{
Console.WriteLine("You are done Brotha");
}
printNumber();
Console.WriteLine("--------------");
printNumber();
Console.WriteLine("--------------");
printNumber();
Console.ReadKey();
void printNumber()
{
int number = 1;
int num01 = 0;
int attempts = 0;
Random numGen = new Random();
while (num01 != guess && attempts <= 9)
{
Console.ReadKey();
num01 = numGen.Next(1, 11);
Console.WriteLine(number + ". You got: " + num01);
attempts++;
number++;
}
if (num01 == guess && attempts <= 9 && attempts > 1)
{
Console.WriteLine("You WIN!");
Console.WriteLine("And it took you " + attempts + " attempts to roll " + guess);
}
else if (num01 == guess && attempts == 1)
{
Console.WriteLine("You WIN!");
Console.WriteLine("Holy shit!");
}
else if (num01 == guess && attempts == 10)
{
Console.WriteLine("You WIN!");
Console.WriteLine("And it took you " + attempts + " attempts to roll " + guess);
Console.WriteLine("Lucky one Buddy");
}
else
{
Console.WriteLine("You LOSE!");
}
}```
I quickly recreated it
Okay. So first question: do you know what type is returned by TryParse?
bool
Correct. So why do you store the result in an int?
int guess = int.TryParse(...
// ^ why this?
It should
okay
TryParse returns true if the conversion was successful, false otherwise
This is how you check if the user entered a valid integer
Okay next problem, you're missing semicolons
Which your IDE is crying out about to you
I didin't think you use them here
Okay cool
I see you also fixed the = to ==
That was going to be my next point
But actually, it is redundant here. A condition is compared against true anyway
When you write if (condition), what this actually means is "is condition true?" This means you can just write if (guess), because that checks if guess is true.
When you write if (guess == true), this is like a much more verbose question, "is the claim that guess is equal to true, a true claim?" which - while technically results in the same output - is needlessly wasteful
okay
didin't know that
The same applies to while loops
You have while (guess <= 0) (let's just ignore the >10 one for now). This is asking "is the claim that guess being less than or equal to 0, a true claim?"
You don't write == true here, despite the fact that you could. You could write while (guess <= 0 == true). Which just extends the question to something like "is the assertion that guess being less than or equal to 0 is a fact, a true assertion?"
But no one does that because it's stupid 
You can chain any amount of == true to the end, and the logic holds. if (guess == true == true == true == true)
But it's pointless because you're just asking the same question again and again
Anyway, next problem: your while loop
guess is defined to be of type bool, which means it's either true or false. So you can't check if it's between 1 and 10, because true/false isn't a number between 1 and 10
I think what you meant to write on line 5 is ..., out int guess); - this stores the converted integer into guess. And the bool itself should be called something else
don't know what exactly
Because your code above shows you using guess in the printNumber method, with the assumption that it's an int
uuu
that was deep
An example of how TryParse is to be used:
Console.Write("Enter a number: ");
string input = Console.ReadLine();
bool success = int.TryParse(input, out int number);
if (success)
{
Console.WriteLine($"You entered a number, and it was {number}.");
}
else
{
Console.WriteLine("You did not enter a number");
}
Or alternatively, because success is only used in the if statement, you can just call it as the condition:
Console.Write("Enter a number: ");
string input = Console.ReadLine();
if (int.TryParse(input, out int number))
{
Console.WriteLine($"You entered a number, and it was {number}.");
}
else
{
Console.WriteLine("You did not enter a number");
}
I think I should change the guess in if statement to "statement"
Yes, though statement is a weird name
what is that dollar sign
I had a feeling you'd ask about that 
Smh complicating everything
It's called string interpolation. It allows you to use variables in the string by surrounding them with { }, like you see it has {number}
$"You entered a number, and it was {number}."
is more or less the same as
"You entered a number, and it was " + number + "."
except the latter is a shit way to do it and shouldn't be used
Yes you should always use interpolation wherever possible
Which is like 98% of cases. In the other cases, you can use string.Format or StringBuilder
but don't worry about those for now
okay
Same situation here, you're storing the result in an int guess and declaring a num, when it should store to a bool and write to guess
you don't need to create a new bool, just use the one you already have
Because you're not spelling it correctly
and as for the error on guess, that's because you're trying to redeclare a new variable with the name guess
Instead of out int x, to write to an existing variable you just do out x
You're welcome I guess 
Now you just need to use success to print a message or whatever if the user enters anything except a number
But looks like you know how to do that already
I appreciate that🙏
(Hint: x == false is also redundant. You can just write !x)
I've got one more question
Console.WriteLine("You need to get the number in under 10 tries then you win, if not you lose!");
Console.Write("Pick a number between 1 and 10: ");
bool success = int.TryParse(Console.ReadLine(), out int guess);
if (success)
{
while (guess <= 0 || guess > 10)
{
if (success)
{
Console.WriteLine("Bro, just: ");
success = int.TryParse(Console.ReadLine(), out guess);
}
else
{
Console.WriteLine("You are done Brotha");
}
}
printNumber();
Console.WriteLine("--------------");
printNumber();
Console.WriteLine("--------------");
printNumber();
}
else
{
Console.WriteLine("You are done Brotha");
}
Console.ReadKey();
void printNumber()
{
int number = 1;
int num01 = 0;
int attempts = 0;
Random numGen = new Random();
while (num01 != guess && attempts <= 9)
{
Console.ReadKey();
num01 = numGen.Next(1, 11);
Console.WriteLine(number + ". You got: " + num01);
attempts++;
number++;
}
if (num01 == guess && attempts <= 9 && attempts > 1)
{
Console.WriteLine("You WIN!");
Console.WriteLine("And it took you " + attempts + " attempts to roll " + guess);
}
else if (num01 == guess && attempts == 1)
{
Console.WriteLine("You WIN!");
Console.WriteLine("Holy shit!");
}
else if (num01 == guess && attempts == 10)
{
Console.WriteLine("You WIN!");
Console.WriteLine("And it took you " + attempts + " attempts to roll " + guess);
Console.WriteLine("Lucky one Buddy");
}
else
{
Console.WriteLine("You LOSE!");
}
}```
the program before was like if the first input is false then it shows "You are done Brotha" and quits the program
but if the first input would be true and then the next ones will be false it would keep asking "Bro just: "
and I wanted to make it that everytime it's false it quits the program
and now it just crashes the program

btw I love it
I didin't mean it but it turned out being even better
Okay so remember in the other post how Zenvin said that every program's entry point is the Main method? And that if you don't write one, like you haven't, it gets generated for you implicitly?
but I still want to know how to do it how I wanted normally
yee
So a program closes/terminates, whenever the Main method has finished everything
So you need to just return; out of the Main method, in order to quit
It is confusing because it might appear like you're not in a method, so calling return; seems weird and out of context, but that is how it works
where do I call it
Whenever you want it to quit
Also I misspoke, you don't "call" return, I just said it that way for simplicity sake
It's actually a return statement. But eh whatever
It does in this case
return ends the current method
So it will go back to whatever method called it
But since your code is running in the Main method, ending that method consequently ends the program
uu
You just don't see the Main method because it's generated for you. But it's there
nice
I will also want to add some other texts besides only "Bro just: "
so it won't be that plane
i.e. a complete program like so:
Console.WriteLine("Hello World");
return;
Console.WriteLine("This message will not print!");
is actually equivalent to something like:
class Program
{
static void Main()
{
Console.WriteLine("Hello World");
return;
Console.WriteLine("This message will not print!");
}
}
the Program class and Main method are just generated for you so you don't have to waste time writing it




