0

So I'm writing a console application in C# for class and I need to figure out how to check if the input is a decimal or not. It would follow this code that I've written so far:

Console.Write("Enter the annual amount of money saved: ");

decimal moneySaved = Convert.ToDecimal(Console.ReadLine());

After it checks the input I'd want it to output something like "You didn't enter a number, please try again."

Thanks in advance!

Rob
  • 26,989
  • 16
  • 82
  • 98

1 Answers1

0
 Console.Write("Enter the annual amount of money saved: ");
 string moneySaved = Console.ReadLine();
 decimal number;
 if (Decimal.TryParse(moneySaved, out number))
 {
   Console.Write("You entered a decimal number");
 }
 else
 {
  Console.Write("You didn't enter a number, please try again.");
 }
Sajeetharan
  • 216,225
  • 63
  • 350
  • 396