using System;
namespace L2._7_BodyMassIndex { class Program { static void Main(string[] args) { Console.WriteLine("Body Mass Index Calculator."); // All your code here Console.WriteLine("Enter Your Name"); string userName = Console.ReadLine();
Console.WriteLine("Enter Your Weight In kg");
double userWeight = int.Parse(Console.ReadLine());
Console.WriteLine("Enter Your Height In Meters");
double userHeight = int.Parse(Console.ReadLine());
double userHeightSqrd = Math.Pow(userHeight,2);
double userBmi = (userWeight / userHeightSqrd);
Console.WriteLine("============");
Console.WriteLine(userName);
Console.WriteLine("============");
Console.WriteLine("Weight: " userWeight);
Console.WriteLine("Height: " userHeight);
Console.WriteLine("BMI: " userBmi);
}
}
}
Getting unhandled exception errors when program is displaying end of the program.
"Unhandled exception. System.FormatException: Input string was not in a correct format. at System.Number.ThrowOverflowOrFormatException(ParsingStatus status, TypeCode type) at System.Number.ParseInt32(ReadOnlySpan`1 value, NumberStyles styles, NumberFormatInfo info) at System.Int32.Parse(String s)"
CodePudding user response:
Use same datatype for conversion and use the double.TryParse() instead of int.Parse() to prevent exceptions.
Console.WriteLine("Enter Your Weight In kg");
double userWeight;
double.TryParse(Console.ReadLine(), out userWeight);
Console.WriteLine("Enter Your Height In Meters");
double userHeight;
double.TryParse(Console.ReadLine(), out userHeight);
CodePudding user response:
Try to match the type of your input and the type of your variables like:
double userWeight = double.Parse(Console.ReadLine());
double userHeight = double.Parse(Console.ReadLine());