Ryan Velo

Ryan Velo

  • NA
  • 4
  • 2.6k

C# Error cannot convert string to int

Oct 3 2016 4:22 PM
Hello! I've been working on this 2 class Bmi calculator have narrowed it down to 2 errors on the second class.
 
I've trying to figure out how I can make it convert string to int or give back a number value. I'm still new to this and any help is appreciated!
 
First part
 
 
using System;
namespace Calculator
{
public class BMICalc
{
private const
decimal REQ = 703;
private int weightPerson;
private int heightPerson;
public BMICalc()
{
}
public BMICalc(int weiP, int heiP)
{
this.weightPerson = weiP;
this.heightPerson = heiP;
}
public decimal SetBmi()
{
decimal bmi;
bmi = (this.weightPerson * BMICalc.REQ) / (this.heightPerson * this.heightPerson);
if (bmi < 18.5m)
{
Console.WriteLine("UnderWeight");
Console.ReadLine();
}
if (bmi > 18.5m && bmi < 25.0m)
{
Console.WriteLine("Normal");
Console.ReadLine();
}
if (bmi > 25.0m && bmi < 29.9m)
{
Console.WriteLine("OverWeight");
Console.ReadLine();
}
if (bmi > 29.9m && bmi < 40.0m)
{
Console.WriteLine("Obese");
Console.ReadLine();
}
return bmi;
}
public override string ToString()
{
return "\tCalculator" +
"\n\n Weight:" + this.weightPerson +
"\n\n Height:" + this.heightPerson +
"\n\n BMI Score:" + SetBmi().ToString("C");
}
}
}
 
 
Second part
 
 
namespace Calculator
{
public class App
{
public static void Main() //
{
int heiP, weiP;
heiP = InputHeight();
// main error here
weiP = InputWeight();
BMICalc bmiCal = new BMICalc(heiP, weiP);
Console.Clear();
Console.WriteLine(bmiCal.ToString());
Console.ReadKey();
}
public static int InputHeight()
{
int hNumber;
Console.Write("Please enter your height");
hNumber = Console.ReadLine(); // error is here
return hNumber;
}
public static int InputWeight()
{
int wNumber;
Console.Write("Please enter your height");
wNumber = Console.ReadLine(); // error is here
return wNumber;
}
}
}
 

Answers (3)