Hi i am suppose to make a Temperature calculator that will accept either Celsius or perimeter and convert that temperature to the other scale. If a Celsius temperature is entered it will be converted to Fahrenheit and vice versa.
Instructions:
For this you will have to design and code a method to convert one temperature scale to another and return the result. This single method should take two arguments, one for the temperature value to convert and a second indicating which temperature scale to convert to.
Your method should be coded so that it could be accessed by another class or application. Also, make sure there is only one return statement in your method.
So far i have created this code but it showing me 2 small errors and i don’t know how to fix them.
**error 1. Constant value ’67’ cannot be converted to a ‘char’
error 2. Constant value ’70’ cannot be converted to a ‘char’**
namespace Lab7
{
public partial class frmTemperatureConverter : Form
{
public frmTemperatureConverter()
{
InitializeComponent();
}
private void txtValueToConver_TextChanged(object sender, EventArgs e)
{
}
private void btnConvert_Click(object sender, EventArgs e)
{
char chr;
string str1;
string str2;
object[] objArray;
if (this.txtConvert.Text != "")
{
double num1 = double.Parse(this.txtConvert.Text);
if (this.radCelsius.Checked)
{
chr = 67;
str1 = "farenheit";
str2 = "celsius";
}
else
{
chr = 70;
str1 = "celsius";
str2 = "farenheit";
}
double num2 = Math.Round(this.ConvertTemperature(num1, chr), 2);
this.lblResult.Text = string.Concat(new object[] { num1, " ", str1, " converts to ", num2, " ", str2 });
}
else
{
this.lblResult.Text = "Please enter a numeric temperature to convert.";
this.txtConvert.Focus();
}
}
public double ConvertTemperature(double inTemp, char toScale)
{
double num;
if (toScale == 70)
{
num = inTemp * 1.80 + 32.00;
}
else
{
if (toScale == 67)
{
num = (inTemp - 32.00) / 1.80;
}
else
{
num = inTemp;
}
}
return num;
}
private void btnClear_Click(object sender, EventArgs e)
{
this.txtConvert.Text = "";
this.lblResult.Text = "";
this.txtConvert.Focus();
this.radCelsius.Checked = true;
}
private void btnExit_Click(object sender, EventArgs e)
{
base.Close();
}
}
}
The problem is that 67 and 70 are not characters — they are integers.
The simplest thing would to make the variable “chr” an integer. Then you should probably change its name too — perhaps to “toScale” — the same as the parameter name.
Or you could leave the variable “chr” as a char, and use the values ‘C’ instead of 67 and ‘F’ instead of 70. This method makes it easier to understand the program, too.