#I want to convert Console.ReadLine into int with the TryParse method

1 messages · Page 1 of 1 (latest)

sick haven
#

Why is it not working dude

#
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

soft jetty
#

what is the error?

sick haven
soft jetty
#

Okay, so what does that error mean?

sick haven
#

that it can't convert bool

#

into int

soft jetty
#

mhm

#

are you following some guide or are you trying this function on your own?

sick haven
#

this is my first time using tryparse

#

but yeah I'm watching some tuts

soft jetty
#

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

sick haven
#

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

soft jetty
sick haven
#

solving the convert problem

soft jetty
#

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

sick haven
#

what

soft jetty
#

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?

sick haven
#

yep

soft jetty
#

what was your thought process for doing this?

sick haven
#

but it doesn't work that way

#

that's why i put it in there

#

It didin't work the other way

soft jetty
#

Could you explain more about the different ways you tried? Remember, coding shouldn't be guessing things

sick haven
#

eh

soft jetty
# sick haven 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?

sick haven
#

condition

#

that's need to be met

#

in order to do smth

soft jetty
#

yeah!

#

So should the variable assignment go there?

sick haven
#

nope

soft jetty
#

okay good

#

now read the parse function and tell me what it returns. do you know about functions and what they return?

sick haven
#

they return a variable

soft jetty
#

yes, and in this case, what is the type of the variable?

sick haven
#

int

soft jetty
#

are you sure? did you read the function description?

sick haven
#

oh

#

bool

#

but my return variable is int

soft jetty
#

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?

sick haven
#

it didin't convert

soft jetty
#

it wasn't successful, yeah. and true means the conversion (or parsing) was successful

sick haven
#

exactly

soft jetty
#

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

sick haven
#

eeeeee

#

return the variable as an bool

soft jetty
#

can you elaborate?

sick haven
#

change it from int to bool

#

so we wiil get the true and false options

soft jetty
#

okay sure

#

let's start changing the code. show me the new code you wrote

sick haven
#

I was trying to change it so the tryparse will be above the if statement

soft jetty
#

Okay show me what you've got

#

It's okay if it has errors, we'll work through it together

sick haven
soft jetty
#

okay that's a start. remind me what type the parse function returns?

sick haven
#

bool

soft jetty
#

maybe you see a problem?

sick haven
soft jetty
#

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?

sick haven
#

hm

#

not really

soft jetty
#

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?

soft jetty
soft jetty
# sick haven

so why is what you did here not correct? i see you did change a type from int to bool...

sick haven
soft jetty
#

oh

#

haha

#

could you be even more specific with what you don't get?

sick haven
#

I get that you can replace an entire method with just smth smaller

soft jetty
#

okay. the point i was trying to illustrate it that a function can be "replaced" by its return value

sick haven
#

but the return value is int the function

soft jetty
sick haven
#

bro that shit is so confusing

soft jetty
#

don't worry about the parsed int value yet. just work on using the true false return value to indicate success or failure

sick haven
#

alr

soft jetty
#

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

sick haven
#

😭

#

th is bro talking about

#

sorry I just don't know what to do

soft jetty
#

so right now you've written int guess = int.TryParse(...). why is this wrong?

sick haven
#

this is wrong?

soft jetty
#

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

sick haven
#

so I change the int guess

#

to bool guess

soft jetty
#

how about creating a new variable? the return value doesn't represent your guess, it represents whether the function succeeded, right?

sick haven
#

yhm

soft jetty
#

go ahead and post the new code

sick haven
#

Wait

#

My PC shut down

soft jetty
#

okay LOL

sick haven
#

The electricity turned off or smth

sick haven
#

okay

#

@soft jetty

#

I got on my laptop

soft jetty
#

haha I'll be frre again in like 3 hrs

#

maybe someone else can pick up where we left off

#

or you can wait!

sick haven
#

@void steppe

#

I need you

#

my friend

sick haven
#

anybody there to help?

void steppe
sick haven
#

LESGGGGGOOOOOO

#

alright

void steppe
#

I see conversation has already happened here

sick haven
#

yyea

void steppe
#

I ain't reading all that, fill me in var

#

tl;dr please

sick haven
#

so

#

I'm trying to use the tryparse method for the first time

#

and it was not really working

void steppe
#

"not working" how?

hidden juniperBOT
#

"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?

sick haven
#

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

void steppe
#

👍

#

Seeing the code will help

sick haven
#

ooh

#

I forgot that my pc crashed

#

and it didin't save up to some point

void steppe
#

That's fine I can still explain how TryParse works, if you need that

sick haven
#

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

void steppe
#

Okay. So first question: do you know what type is returned by TryParse?

sick haven
#

bool

void steppe
#

Correct. So why do you store the result in an int?

#
  int guess = int.TryParse(...
// ^ why this?
sick haven
#

that is a good question out here

#

hm

#

so it should be turned into bool?

void steppe
#

It should

sick haven
#

okay

void steppe
#

TryParse returns true if the conversion was successful, false otherwise

#

This is how you check if the user entered a valid integer

sick haven
#

fixed

void steppe
#

Okay next problem, you're missing semicolons

#

Which your IDE is crying out about to you

sick haven
#

I didin't think you use them here

void steppe
#

Every statement ends with one

#

Think of it like a period in english

sick haven
#

nice

void steppe
#

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

sick haven
#

waiiit

#

shouldn't the functions be in the if true statement

#

like that

void steppe
#

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

void steppe
#

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 var

#

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

sick haven
#

get it

void steppe
#

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

sick haven
#

hum

#

makes sense

#

go on

#

I think I see what do we have to do here

void steppe
#

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

sick haven
#

don't know what exactly

void steppe
#

Because your code above shows you using guess in the printNumber method, with the assumption that it's an int

void steppe
#

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");
}
sick haven
# sick haven

I think I should change the guess in if statement to "statement"

void steppe
#

Yes, though statement is a weird name

sick haven
#

I'll change it

#

but

void steppe
#

I had a feeling you'd ask about that var

soft jetty
#

Smh complicating everything

void steppe
#

smh I am preferring optimal code

#

concatenation is a bad habit to get into

void steppe
#
$"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

sick haven
#

so you say I should be using the first one

#

always

void steppe
#

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

sick haven
#

okay

void steppe
#

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

sick haven
#

updated

void steppe
#

you don't need to create a new bool, just use the one you already have

sick haven
#

i tried

#

and it gets me this

void steppe
#

Because you're not spelling it correctly

sick haven
#

omg

#

idiot

#

alr

#

fixed

void steppe
#

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

sick haven
#

holy shit

#

that's all

#

bro you are saving my ass so bad

void steppe
#

You're welcome I guess var

#

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

sick haven
#

I appreciate that🙏

void steppe
#

(Hint: x == false is also redundant. You can just write !x)

sick haven
#
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

sick haven
#

I didin't mean it but it turned out being even better

void steppe
#

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?

sick haven
#

but I still want to know how to do it how I wanted normally

void steppe
#

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

sick haven
#

where do I call it

void steppe
#

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

sick haven
#

oh

#

I didin't know that if you use the return statement it just quits the program

void steppe
#

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

sick haven
#

uu

void steppe
#

You just don't see the Main method because it's generated for you. But it's there

sick haven
#

nice

#

I will also want to add some other texts besides only "Bro just: "

#

so it won't be that plane

void steppe
#

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