C# : Console.Read() does not get the "right" input

Posted by Daemonfire3002nd on Stack Overflow See other posts from Stack Overflow or by Daemonfire3002nd
Published on 2010-03-13T11:44:24Z Indexed on 2010/03/13 11:55 UTC
Read the original article Hit count: 188

Filed under:

Hi there,

I have the following code: The actual problem is the "non-quoted" code. I want to get the player amount (max = 4), but when I ask via Console.Read() and I enter any Int from 1 to 4 I get as value: 48 + Console.Read(). They only thing how I can get the "real" input is using Console.ReadLine(), but this does not give me an Integer, no it returns a string, and actually do not know how to convert String (Numbers) to Integers in C#, because I am new, and because I only found ToString() and not ToNumber.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace eve_calc_tool
{
    class Program
    {

        int players;
        int units;
        int active_units;
        int inactive_units;
        int finished_units;
        int lastDiceNumber = 0;
        bool game_state;

        public static void Main(string[] args)
        {
            int count_game = 0;

            //Console.Title = "Mensch ärger dich nicht";
            //Console.WriteLine("\tNeues Spiel wird");
            //Console.WriteLine("\t...geladen");
            //System.Threading.Thread.Sleep(5000);

            //Console.Clear();
            //Console.WriteLine("Neues Spiel wird gestartet, bitte haben sie etwas Geduld");
            //Console.Title = "Spiel " + count_game.ToString();

            //Console.Clear();
            //string prevText = "Anzahl der Spieler: ";

            //Console.WriteLine(prevText);

            string read = Console.ReadLine();

            /*Program game = new Program();
            game.players = read;

            game.setPlayers(game.players);

            if (game.players > 0 && 5 > game.players)
            {
                game.firstRound();
            }*/

            string readagain = read;




            Console.ReadLine();
        }
        /*
        bool setPlayers(int amount)
        {
            players = amount;

            if (players > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        bool createGame()
        {

            inactive_units = units = getPlayers() * 4;
            active_units = 0;
            finished_units = 0;

            game_state = true;

            if (game_state == true)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        int getPlayers()
        {
            return players;
        }


        private static readonly Random random = new Random();
        private static readonly object syncLock = new object();
        public static int RandomNumber(int min, int max)
        {
            lock (syncLock)
            { // synchronize
                return random.Next(min, max);
            }
        }


        int rollDice()
        {
            lastDiceNumber = RandomNumber(1,6);
            return lastDiceNumber;
        }

        int firstRound()
        {
            int[] results = new int[getPlayers()];

            for (int i = 0; i < getPlayers(); i++)
            {
                results[i] = rollDice();
            }
            Array.Sort(results);

            return results[3];
        }
         */
    }
}

© Stack Overflow or respective owner

Related posts about c#