Reputation: 193
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.
Upvotes: 0
Views: 193
Reputation: 236268
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:
Int32.TryParse("2", out value)
For your case:
Console.WriteLine("Put in the price of the product");
string input = Console.ReadLine();
decimal sum;
if (!Decimal.TryParse(input, out sum))
{
Console.WriteLine("Decimal number cannot be parsed from your input.");
return;
}
if (sum <= 100)
Console.WriteLine("Your final price is {0:0:00}", sum * 0.90M);
UPDATE
Decimal
equivalent. A return value indicates whether the conversion succeeded or failed. It does not throws an exception if conversion failed.bool
and returns true
if and only if its operand is false
.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 your Main
method, then program will terminate. But this all is out of your initial question about parsing strings.
Upvotes: 2
Reputation: 23731
The conversion function you are using will I believe throw an exception if it cannot convert the string passed to the requested type. Generally, exceptions should be avoided for controlling program flow, and reserved for truly unexpected situations. Instead, you should look to using a method that doesn't throw an exception, but instead returns a value to indicate success or failure. With this in ind you could try:
try
{
Console.WriteLine("Put in the price of the product");
decimal sum;
// Repeat forever (we'll break the loop once the user enters acceptable data)
while (true)
{
string input = Console.ReadLine();
// Try to parse the input, if it succeeds, TryParse returns true, and we'll exit the loop to process the data.
// Otherwise we'll loop to fetch another line of input and try again
if (decimal.TryParse(input, out sum)) break;
}
if (sum <= 100)
{
decimal totalprice = sum * .90m;
Console.WriteLine("Your final price is {0:0:00}", totalprice);
}
}
catch
{
}
Upvotes: 0
Reputation: 20640
Try this (note the while/break pairing):
while (true)
{
string input = Console.ReadLine();
decimal sum;
if (Decimal.TryParse(input, out sum) == true)
{
if (sum <= 100)
{
decimal totalprice = sum * .90m;
Console.WriteLine("Your final price is {0:0:00}", totalprice);
break; // break out of while
}
}
}
Upvotes: 2