[2 days left] What’s wrong with your cloud strategy? Learn why multicloud solutions matter with Nimble Storage.Register Now

x
?
Solved

What is/isn't wrong?

Posted on 2013-06-13
4
Medium Priority
?
418 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 1000 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

Important Lessons on Recovering from Petya

In their most recent webinar, Skyport Systems explores ways to isolate and protect critical databases to keep the core of your company safe from harm.

Question has a verified solution.

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

It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
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…
This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.
Suggested Courses

649 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