What is/isn't wrong?

Posted on 2013-06-13
Last Modified: 2013-06-13
Reading a book on C# to learn.  On one project I don't have the code there to just follow.  I just have to put what I think might work.  But, getting the a number from the textbox. I have to convert that to an operand and proceed with the operators to get my answer.  However, when I type in any number in that textbox I always end up with a zero in the textbox with the answer.  Not sure what I am doing wrong.  Any help would be much appreciated.  Thanks


using System;
using System.Windows.Forms;

public class frmMain : Form
    private Label label1;
    private TextBox txtFahrenheit;
    private TextBox txtCelcius;
    private Button btnConvert;
    private Button bntClear;
    private Button btnExit;
    #region Windows Code
    private void InitializeComponent()
        this.label1 = new System.Windows.Forms.Label();
        this.txtFahrenheit = new System.Windows.Forms.TextBox();
        this.txtCelcius = new System.Windows.Forms.TextBox();
        this.btnConvert = new System.Windows.Forms.Button();
        this.bntClear = new System.Windows.Forms.Button();
        this.btnExit = new System.Windows.Forms.Button();
        // label1
        this.label1.AutoSize = true;
        this.label1.Location = new System.Drawing.Point(12, 35);
        this.label1.Name = "label1";
        this.label1.Size = new System.Drawing.Size(153, 13);
        this.label1.TabIndex = 0;
        this.label1.Text = "Enter in the current Fahrenheit:";
        // txtFahrenheit
        this.txtFahrenheit.Location = new System.Drawing.Point(172, 35);
        this.txtFahrenheit.Name = "txtFahrenheit";
        this.txtFahrenheit.Size = new System.Drawing.Size(100, 20);
        this.txtFahrenheit.TabIndex = 1;
        // txtCelcius
        this.txtCelcius.Location = new System.Drawing.Point(15, 77);
        this.txtCelcius.Multiline = true;
        this.txtCelcius.Name = "txtCelcius";
        this.txtCelcius.Size = new System.Drawing.Size(257, 56);
        this.txtCelcius.TabIndex = 2;
        this.txtCelcius.TextAlign = System.Windows.Forms.HorizontalAlignment.Center;
        // btnConvert
        this.btnConvert.Location = new System.Drawing.Point(15, 152);
        this.btnConvert.Name = "btnConvert";
        this.btnConvert.Size = new System.Drawing.Size(86, 23);
        this.btnConvert.TabIndex = 3;
        this.btnConvert.Text = "Con&vert Temp";
        this.btnConvert.UseVisualStyleBackColor = true;
        this.btnConvert.Click += new System.EventHandler(this.btnConvert_Click);
        // bntClear
        this.bntClear.Location = new System.Drawing.Point(116, 152);
        this.bntClear.Name = "bntClear";
        this.bntClear.Size = new System.Drawing.Size(75, 23);
        this.bntClear.TabIndex = 4;
        this.bntClear.Text = "&Clear";
        this.bntClear.UseVisualStyleBackColor = true;
        this.bntClear.Click += new System.EventHandler(this.bntClear_Click);
        // btnExit
        this.btnExit.DialogResult = System.Windows.Forms.DialogResult.Cancel;
        this.btnExit.Location = new System.Drawing.Point(197, 152);
        this.btnExit.Name = "btnExit";
        this.btnExit.Size = new System.Drawing.Size(75, 23);
        this.btnExit.TabIndex = 5;
        this.btnExit.Text = "E&xit";
        this.btnExit.UseVisualStyleBackColor = true;
        this.btnExit.Click += new System.EventHandler(this.btnExit_Click);
        // frmMain
        this.AcceptButton = this.btnConvert;
        this.CancelButton = this.btnExit;
        this.ClientSize = new System.Drawing.Size(284, 262);
        this.Name = "frmMain";
        this.Text = "Fahrenheit Conversion";


    public frmMain()

    public static void Main()
        frmMain main = new frmMain();

    private void bntClear_Click(object sender, EventArgs e)
        txtCelcius.Text = String.Empty;
        txtFahrenheit.Text = String.Empty;

    private void btnExit_Click(object sender, EventArgs e)

    private void btnConvert_Click(object sender, EventArgs e)
        bool blnFlag;
        int intFahrenheit;
        int intCelcius;

        blnFlag = int.TryParse(txtFahrenheit.Text, out intFahrenheit);
        if (blnFlag == false)
            MessageBox.Show("Enter a whole number", "Input Error");
            txtFahrenheit.Text = String.Empty;
        intCelcius = 5 / 9 * (intFahrenheit - 32);
        txtCelcius.Text = "The conversion from " + txtFahrenheit.Text + " degrees fahrenhiet changes to " +
            intCelcius.ToString() + " degrees celcius";

Question by:M1Bill72
  • 2
  • 2
LVL 24

Accepted Solution

chaau earned 250 total points
ID: 39246652
Modify this line to:

intCelcius = 5.0 / 9.0 * (intFahrenheit - 32);

Open in new window

What it does is converts the int literals to doubles, making the calculation work

Author Comment

ID: 39246680
The int would only have whole numbers.  But, if I used an int why wouldn't that just show the whole number and not the not show the remains (e.g. 15 (not 15.66666666667))? And when tried to use double before I posted this it didn't work.  However, I used the numbers 9 and 5 instead of 9.0 and 5.0 with this.  What makes the 9 not work but the 9.0 to work?  I appreciate any help.
LVL 24

Expert Comment

ID: 39246694
It is all to do with the Numeric Literals.
C# has certain rules how to deal with the numbers you insert in your code. If you have not specified a decimal point (.) the numbers are all converted to int. When you divide 5/9 the number will be converted to int and becomes 0.

By adding a decimal point, you tell the compiler to use floating point math for calculation. You could use suffix D as well (but it would confuse you as much, I guess):
intCelcius = 5D / 9D * (intFahrenheit - 32);

Open in new window


Author Comment

ID: 39246728

Featured Post

Three Reasons Why Backup is Strategic

Backup is strategic to your business because your data is strategic to your business. Without backup, your business will fail. This white paper explains why it is vital for you to design and immediately execute a backup strategy to protect 100 percent of your data.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

How to remove superseded packages in windows w60 or w61 installation media (.wim) or online system to prevent unnecessary space. w60 means Windows Vista or Windows Server 2008. w61 means Windows 7 or Windows Server 2008 R2. There are various …
Having just graduated from college and entered the workforce, I don’t find myself always using the tools and programs I grew accustomed to over the past four years. However, there is one program I continually find myself reverting back to…R.   So …
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

770 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question