Jump to content

C# Problem With a loop

Go to solution Solved by Guest,

The actuall Fuck?

4de51858c9.png

What the balls increased my number by + 48?

(If i entered 2 it became 50)

http://stackoverflow.com/questions/9156363/console-readline-add-48-to-int

When you do (int)Console.Read() that is actually getting the ASCII value of the string "1".

 

As xshockz suggested, you want int.Parse("1") or int.TryParse("1", out InputInt).

 

The difference is that int.Parse will throw exception if the string cannot be parse. Where as int.TryParse will return false, useful if you want to repeat until valid input.

 

Ref: https://msdn.microsoft.com/en-us/library/f02979c7%28v=vs.110%29.aspx

Link to comment
https://linustechtips.com/topic/322623-c-problem-with-a-loop/#findComment-4386546
Share on other sites

Link to post
Share on other sites

When you do (int)Console.Read() that is actually getting the ASCII value of the string "1".

 

As xshockz suggested, you want int.Parse("1") or int.TryParse("1", out InputInt).

 

The difference is that int.Parse will throw exception if the string cannot be parse. Where as int.TryParse will return false, useful if you want to repeat until valid input.

 

Ref: https://msdn.microsoft.com/en-us/library/f02979c7%28v=vs.110%29.aspx

 

Right, it should also change the output variable to 0 or null if the parse fails if I remember correctly.

Link to comment
https://linustechtips.com/topic/322623-c-problem-with-a-loop/#findComment-4387071
Share on other sites

Link to post
Share on other sites

Right, it should also change the output variable to 0 or null if the parse fails if I remember correctly.

 

Yes, from the MSDN:

 

 

result Type: System.Int32

When this method returns, contains the 32-bit signed integer value equivalent of the number contained in s, if the conversion succeeded, or zero if the conversion failed. The conversion fails if the s parameter is null or String.Empty, is not of the correct format, or represents a number less than MinValue or greater than MaxValue. This parameter is passed uninitialized.

Link to comment
https://linustechtips.com/topic/322623-c-problem-with-a-loop/#findComment-4387105
Share on other sites

Link to post
Share on other sites

Don't cast the input to an int. Best practice would be to use Int.Parse(input)

 

 

change:

int InputInt = (int)Console.Read();

to:

int InputInt;int.TryParse(Console.ReadLine(), out InputInt);

 

When you do (int)Console.Read() that is actually getting the ASCII value of the string "1".

 

As xshockz suggested, you want int.Parse("1") or int.TryParse("1", out InputInt).

 

The difference is that int.Parse will throw exception if the string cannot be parse. Where as int.TryParse will return false, useful if you want to repeat until valid input.

 

Ref: https://msdn.microsoft.com/en-us/library/f02979c7%28v=vs.110%29.aspx

Thanks friends, i solved it  :)

Redliquid~

Link to comment
https://linustechtips.com/topic/322623-c-problem-with-a-loop/#findComment-4389526
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×