I have this crazy idea, I would like a program to not execute anything if the wrong data is put into the console. Such as the alphabet, weird characters. All I want is decimal numbers and a period to be accepted. If the wrong data is typed in, I want the program to STAY there and do absolutely nothing after you hit enter.
My mindset thinks:
if (sum != decimal)
{
// Don't do anything, just leave it as is.
code I have no clue about.
}
Now, you must be thinking, you can’t use datatypes for an if statement! Maybe you can, but its not working for me. I’m sorry I’m a big noob.
try
{
Console.WriteLine("Put in the price of the product");
string input = Console.ReadLine();
decimal sum = Convert.ToDecimal(input);
if (sum <= 100)
{
decimal totalprice = sum * .90m;
Console.WriteLine("Your final price is {0:0:00}", totalprice);
}
}
catch
{
}
I was also thinking maybe a try and catch statement would work too, but again, I have no clue what to put in that either.
If your answers could be noob-safe and explained. (Because I want to learn the concept of how this stuff works) that would be nice.
A visual example:

When you hit enter, nothing happens but when you put in the correct datatype, the program will continue.
Datatypes are not written to console. Only strings could be retrieved from console input. What type has string
"2"– decimal, int, byte, string? All you can do is try to parse some type from your input string:For your case:
UPDATE
Decimalequivalent. A return value indicates whether the conversion succeeded or failed. It does not throws an exception if conversion failed.booland returnstrueif and only if its operand isfalse.So
if (!Decimal.TryParse(input, out sum))verifies if conversion was NOT successful. Then I put a sample message for user and exited from method (if it was yourMainmethod, then program will terminate. But this all is out of your initial question about parsing strings.