Wrox Programmer Forums
| Search | Today's Posts | Mark Forums Read
BOOK: Beginning C# 3.0 : An Introduction to Object Oriented Programming ISBN: 978-0-470-26129-3
This is the forum to discuss the Wrox book Beginning C# 3.0 : An Introduction to Object Oriented Programming by Jack Purdum; ISBN: 9780470261293
Welcome to the p2p.wrox.com Forums.

You are currently viewing the BOOK: Beginning C# 3.0 : An Introduction to Object Oriented Programming ISBN: 978-0-470-26129-3 section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
  #1 (permalink)  
Old September 6th, 2012, 02:37 PM
Registered User
Points: 32, Level: 1
Points: 32, Level: 1 Points: 32, Level: 1 Points: 32, Level: 1
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jan 2012
Posts: 7
Thanks: 2
Thanked 0 Times in 0 Posts
Default Chapter 3: exercise 2.

Hi, i have problems with exercise 2 in chapter 3. When i click btnCalc, no matter what number i've typed into the fahrenheit box, the program gives me the answer 0! I've used the following code:

Code:
using System;
using System.Windows.Forms;

public class frmMain : Form
{
    private Label label1;
    private Label label2;
    private TextBox txtTf;
    private TextBox txtTc;
    private Button btnCalc;
    #region Windows code
    private void InitializeComponent()
    {
        this.btnCalc = new System.Windows.Forms.Button();
        this.label1 = new System.Windows.Forms.Label();
        this.label2 = new System.Windows.Forms.Label();
        this.txtTf = new System.Windows.Forms.TextBox();
        this.txtTc = new System.Windows.Forms.TextBox();
        this.SuspendLayout();
        // 
        // btnCalc
        // 
        this.btnCalc.Location = new System.Drawing.Point(107, 87);
        this.btnCalc.Name = "btnCalc";
        this.btnCalc.Size = new System.Drawing.Size(75, 23);
        this.btnCalc.TabIndex = 0;
        this.btnCalc.Text = "Calculate";
        this.btnCalc.UseVisualStyleBackColor = true;
        this.btnCalc.Click += new System.EventHandler(this.btnCalc_Click);
        // 
        // label1
        // 
        this.label1.Location = new System.Drawing.Point(12, 48);
        this.label1.Name = "label1";
        this.label1.Size = new System.Drawing.Size(131, 15);
        this.label1.TabIndex = 1;
        this.label1.Text = "Temperature in Celcius:";
        // 
        // label2
        // 
        this.label2.AutoSize = true;
        this.label2.Location = new System.Drawing.Point(12, 25);
        this.label2.Name = "label2";
        this.label2.Size = new System.Drawing.Size(131, 13);
        this.label2.TabIndex = 2;
        this.label2.Text = "Temperature in fahrenheit:\r\n";
        // 
        // txtTf
        // 
        this.txtTf.Location = new System.Drawing.Point(149, 25);
        this.txtTf.Name = "txtTf";
        this.txtTf.Size = new System.Drawing.Size(100, 20);
        this.txtTf.TabIndex = 3;
        // 
        // txtTc
        // 
        this.txtTc.Location = new System.Drawing.Point(150, 52);
        this.txtTc.Name = "txtTc";
        this.txtTc.ReadOnly = true;
        this.txtTc.Size = new System.Drawing.Size(100, 20);
        this.txtTc.TabIndex = 4;
        // 
        // frmMain
        // 
        this.ClientSize = new System.Drawing.Size(284, 118);
        this.Controls.Add(this.txtTc);
        this.Controls.Add(this.txtTf);
        this.Controls.Add(this.label2);
        this.Controls.Add(this.label1);
        this.Controls.Add(this.btnCalc);
        this.Name = "frmMain";
        this.ResumeLayout(false);
        this.PerformLayout();

    }
    #endregion

    public frmMain()
    {
        InitializeComponent();
    }

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

    private void btnCalc_Click(object sender, EventArgs e)
    {
        bool flag;
        float Tf;
        float Tc;

        //check input
        flag = float.TryParse(txtTf.Text, out Tf);
        if (flag == false)
        {
            MessageBox.Show("Enter a whole number", "input error");
            txtTf.Focus();
            return;
        }
        //process step...
        Tc = 5 / 9 * (Tf - 32);
        txtTc.Text = Tc.ToString();
        txtTc.Visible = true;
    }

}
  #2 (permalink)  
Old September 6th, 2012, 02:53 PM
Registered User
Points: 32, Level: 1
Points: 32, Level: 1 Points: 32, Level: 1 Points: 32, Level: 1
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jan 2012
Posts: 7
Thanks: 2
Thanked 0 Times in 0 Posts
Default

Oh, and also, thanks! Realy great book! I've learned so much already! :)
  #3 (permalink)  
Old September 6th, 2012, 03:01 PM
Registered User
Points: 32, Level: 1
Points: 32, Level: 1 Points: 32, Level: 1 Points: 32, Level: 1
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jan 2012
Posts: 7
Thanks: 2
Thanked 0 Times in 0 Posts
Default

haha, stupid me! I looked up the solution just now and saw that i have to use "double" instead of "float". And that 5 / 9 dont work for some reason so you have to use 5.0 / 9.0.. Well, thanks anyway ^^
  #4 (permalink)  
Old September 6th, 2012, 11:09 PM
Friend of Wrox
Points: 1,166, Level: 13
Points: 1,166, Level: 13 Points: 1,166, Level: 13 Points: 1,166, Level: 13
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Sep 2008
Location: Indianapolis, IN, USA.
Posts: 234
Thanks: 0
Thanked 32 Times in 30 Posts
Default C3,Ex2

Glad you figured it out. You could use your float data if you change the one line to:

Tc = (float) ((5.0 / 9.0) * (Tf - 32.0));

Personally, I would always use the double data type because I find it easier to read code that uses double's, plus all of the math library routines use double's for their arguments.
__________________
Jack Purdum, Ph.D.
Author: Beginning C# 3.0: Introduction to Object Oriented Programming (and 14 other programming texts)


Similar Threads
Thread Thread Starter Forum Replies Last Post
Chapter 3 exercise 6 earlmw BOOK: Ivor Horton's Beginning Visual C++ 2008 ISBN: 978-0-470-22590-5 1 February 10th, 2012 09:10 PM
Chapter 4 Exercise 4 Will BOOK: Beginning Microsoft Visual C# 2008 ISBN: 978-0-470-19135-4 2 September 18th, 2009 11:08 AM
Chapter 3 - Exercise 3 AndyN BOOK: Beginning Cryptography with Java 3 August 16th, 2006 03:09 PM
chapter 9 exercise michaelcode BOOK: Beginning ASP.NET 1.0 1 April 9th, 2006 03:56 PM
Chapter 4, Exercise 3 DRAYKKO BOOK: Beginning Java 2 3 July 9th, 2004 02:34 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.