I get the following exception:
Unhandled Exception: System.FormatException: Input string was not in a correct format. at System.Number.StringToNumber(String str, NumberStyles options, NumberBuffer& number, NumberFormatInfo info, Boolean parseDecimal) at System.Number.ParseInt32(String s, NumberStyles style, NumberFormatInfo info) at System.Int32.Parse(String s) at weatherApp.Program.Main(String[] args) in C:\Users\decagon\source\repos\weatherApp\Program.cs:line 15 Press any key to continue . . .
Here's my code:
namespace weatherApp
{
internal class Program
{
static void Main(string[] args)
{
Console.WriteLine("what is the Value of Temperature in Celcius? ");
Console.Read();
int temp = int.Parse(Console.ReadLine());
if (temp > 24 && temp <50 )
{
Console.WriteLine("Wear a short and light Cloth");
}
if (temp > 50 && temp < 59)
{
Console.WriteLine("Dont Wear Clothes, Weather is too hot");
}
if (temp > 60 && temp <= 100)
{
Console.WriteLine("You are Dead, because you have reached the boiling point");
}
if (temp < 24)
{
Console.WriteLine("You are also dead!!!!!!!");
}
}
}
}
CodePudding user response:
Your problem lies here:
Console.Read(); // Problem
int temp = int.Parse(Console.ReadLine());
For example, when you press the number "5" on your keyboard and then press "Enter", the input stream will receive these characters (on Windows):
'5' '\r' '\n'
The Console.Read()
will grab the '5' (and throw it away). All that's left for the Console.ReadLine()
at that point is the "\r\n" which it returns as an empty string. And that empty string cannot be parsed with int.Parse
.
CodePudding user response:
Delete Console.Read(),you are reading the line when you assign temp.
CodePudding user response:
Remove Console.Read(): It will work fine so:
namespace weatherApp
{
internal class Program
{
static void Main(string[] args)
{
Console.WriteLine("what is the Value of Temperature in Celcius? ");
int temp = int.Parse(Console.ReadLine());
if (temp > 24 && temp <50 )
{
Console.WriteLine("Wear a short and light Cloth");
}
if (temp > 50 && temp < 59)
{
Console.WriteLine("Dont Wear Clothes, Weather is too hot");
}
if (temp > 60 && temp <= 100)
{
Console.WriteLine("You are Dead, because you have reached the boiling point");
}
if (temp < 24)
{
Console.WriteLine("You are also dead!!!!!!!");
}
}
}
}