0
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)"

1
  • Input is not valid integer value, please use int.TryParse Commented Oct 6, 2021 at 18:45

2 Answers 2

1

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);
Sign up to request clarification or add additional context in comments.

1 Comment

hahah.. i think every C# programmer has had this nightmare before :)
0

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());

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.