Solved

What is/isn't wrong?

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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
The goal of this video is to provide viewers with basic examples to understand and use conditional statements in the C programming language.
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…

730 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