Solved

What is/isn't wrong?

Posted on 2013-06-13
4
408 Views
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

M1Bill72

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();
        this.SuspendLayout();
        //
        // 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.Controls.Add(this.btnExit);
        this.Controls.Add(this.bntClear);
        this.Controls.Add(this.btnConvert);
        this.Controls.Add(this.txtCelcius);
        this.Controls.Add(this.txtFahrenheit);
        this.Controls.Add(this.label1);
        this.Name = "frmMain";
        this.Text = "Fahrenheit Conversion";
        this.ResumeLayout(false);
        this.PerformLayout();

    }
    #endregion

    public frmMain()
    {
        InitializeComponent();
    }

    public static void Main()
    {
        frmMain main = new frmMain();
        Application.Run(main);
    }

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

    private void btnExit_Click(object sender, EventArgs e)
    {
        Close();
    }

    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.Focus();
            txtFahrenheit.Text = String.Empty;
            return;
        }
       
        intCelcius = 5 / 9 * (intFahrenheit - 32);
        txtCelcius.Text = "The conversion from " + txtFahrenheit.Text + " degrees fahrenhiet changes to " +
            intCelcius.ToString() + " degrees celcius";

    }
}
0
Comment
Question by:M1Bill72
  • 2
  • 2
4 Comments
 
LVL 24

Accepted Solution

by:
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
0
 

Author Comment

by:M1Bill72
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.
0
 
LVL 24

Expert Comment

by:chaau
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

0
 

Author Comment

by:M1Bill72
ID: 39246728
Thanks
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

This article is for Object-Oriented Programming (OOP) beginners. An Interface contains declarations of events, indexers, methods and/or properties. Any class which implements the Interface should provide the concrete implementation for each Inter…
If you haven’t already, I encourage you to read the first article (http://www.experts-exchange.com/articles/18680/An-Introduction-to-R-Programming-and-R-Studio.html) in my series to gain a basic foundation of R and R Studio.  You will also find the …
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.

932 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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now