0

How would I go by checking if there are number values in a string when using console.readline?

PlayerNode playerList = new PlayerNode();
for (int x = 1; x <= numPlayers; x++)
{
    Console.WriteLine("What is your name?");
    Player Aplayer = new Player();
    Aplayer.Name = Console.ReadLine();
    playerList.AddPlayer(Aplayer);
    Console.WriteLine("How Much money do you want to play with?");
    Aplayer.Winnings = float.Parse(Console.ReadLine());// How would i change this to check for number values?        
}
Pang
  • 9,564
  • 146
  • 81
  • 122
tizzyman
  • 9
  • 1

2 Answers2

1

It will not go to the next statement until a number is typed into the console.

        Console.WriteLine("How much money do you want to play with?");
        bool itsNumeric = false;
        double money;

        while(itsNumeric == false)
        {
            itsNumeric = double.TryParse(Console.ReadLine().ToString(), out money);
        }                
0

Decimal.TryParse(...)

Usage:

decimal result;
if (Decimal.TryParse(str, NumberStyles.Number | NumberStyles.AllowCurrencySymbol,
                     CultureInfo.InvariantCulture, out result))
{
    // ...
}

Don't use floats for financial values. Where is the floating point problem in this C# financial calculation?

Community
  • 1
  • 1
myermian
  • 31,823
  • 24
  • 123
  • 215