Jump to content

C# Problem With a loop

Redliquid
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

Just a random thought: after inputing from console try to put InputInt = InputInt - 13;

I never dealt with C#

Link to comment
Share on other sites

Link to post
Share on other sites

Just a random thought: after inputing from console try to put InputInt = InputInt - 13;

I never dealt with C#

Still goes to the Else part of the loop, why did you think it would change annything? :)

Redliquid~

Link to comment
Share on other sites

Link to post
Share on other sites

Still goes to the Else part of the loop, why did you think it would change annything? :)

Because maybe it counts "Enter" towards input and converts it to int.

Try to output what InputInt value is.

Link to comment
Share on other sites

Link to post
Share on other sites

Because maybe it counts "Enter" towards input and converts it to int.

Try to output what InputInt value is.

The actuall Fuck?

4de51858c9.png

What the balls increased my number by + 48?

(If i entered 2 it became 50)

Redliquid~

Link to comment
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)

Link to comment
Share on other sites

Link to post
Share on other sites

change:

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

to:

int InputInt;int.TryParse(Console.ReadLine(), out InputInt);
Link to comment
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

Link to comment
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
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
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
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

×