Jump to content
  • Advertisement
Sign in to follow this  
NUCLEAR RABBIT

Weird C# Issue

This topic is 3712 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Ok, so in a small game im making i am asking the user for a number between 0-8 and when i am testing it out, i type in 4. It then says in my bedugger that its out of range, which i thought was strange. so i put a console.writeline to see if the number i entered was actually being stored right. well, when i enter in 4, it stores it as 52...idk why. can someone look at this code and tell me if you see something i dont. thanx for any help
        static public void GetPlayerMove()
        {
            int playersChoice = new int();
            bool moveSuccessfull = false;

            while (moveSuccessfull == false)
            {
                Console.Write("Select which cell you would like to control: ");
                playersChoice = Console.Read();

                moveSuccessfull = GameManager.CheckMove(playersChoice);
            }

            // right here is says 52 if you enter in 4
            Console.WriteLine(playersChoice);
            GameManager.UpdateBoard(playersChoice, ReturnPlayerPiece());
        }

Share this post


Link to post
Share on other sites
Advertisement
It looks like Console.Read() is returning the ASCII value of the key entered. 4's value happens to be 52. Punch in a character with your current code and see what it gives you ('y' for example will return 121)

What you need to do is replace Console.Read() with Console.ReadLine() which will return the value entered as a string. You can then convert that to an Int32.

Also, you'll probably want to do some error checking to make sure what the player inputs is a valid number.

Hope that helps.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!