Solved

What is/isn't wrong?

Posted on 2013-06-13
4
413 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
4 Comments
 
LVL 25

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 25

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

[Webinar] How Hackers Steal Your Credentials

Do You Know How Hackers Steal Your Credentials? Join us and Skyport Systems to learn how hackers steal your credentials and why Active Directory must be secure to stop them. Thursday, July 13, 2017 10:00 A.M. PDT

Question has a verified solution.

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

This article is meant to give a basic understanding of how to use R Sweave as a way to merge LaTeX and R code seamlessly into one presentable document.
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.
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.

705 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