C# coding project

Need help on this homework assignment bad!

Page 1 of 1

4 Replies - 3760 Views - Last Post: 16 April 2010 - 11:38 AM Rate Topic: -----

#1 Guest_Shadowhand18*


Reputation:

C# coding project

Posted 16 April 2010 - 12:37 AM

sup Guys I am having a very hard time coding a project in C# for a school project. For this project I am suppose to code an application
so the a user enters digits into the text box, and have the numbers average out. Here is the project:

Write a Windows Application that will compute and display the average of three exam scores. Your Form should contain three TextBoxes, with identifying Labels, a Button, and fourth Label for output.
The user will enter three different test scores into the TextBoxes and click the Button. When that happens, your code should create and use four variables: one for each of the scores and one for the overall average. Be sure to use the appropriate data types for each variable. Be sure to place the result in the last Label and format the average to show exactly two decimal places.

I have been trying to code this forever now and the project is nearly due. Can someone please help me? Here is my coding so for:

private void calculateButton_Click(object sender, EventArgs e)
        {
            outputLabel.Text = Convert.ToString((Convert.ToInt32(textBox1.Text)
                + Convert.ToInt32(textBox2.Text)
                + Convert.ToInt32(textBox3.Text)) / 3);
            string output;

            output = outputLabel.Text;
            outputLabel.Text = "The average of these numbers is " + outputLabel.Text;

           
        }
    }
}



When the user click the button its suppose to show the average, plus the decimal points, but i cant get it to show the decimal place.
each time I enter a decimal number such as 12.40 and click the calculate button, the system crashes. Any suggestions on how to modify my code?

Is This A Good Question/Topic? 0

Replies To: C# coding project

#2 janne_panne  Icon User is offline

  • WinRT Dev
  • member icon

Reputation: 429
  • View blog
  • Posts: 1,047
  • Joined: 09-June 09

Re: C# coding project

Posted 16 April 2010 - 12:58 AM

My suggestion is to use something else than an integer if you want decimals too. Maybe this:
decimal d1 = decimal.Parse(textBox1.Text);
decimal d2 = decimal.Parse(textBox2.Text);
decimal d3 = decimal.Parse(textBox3.Text);

string output = ((d1 + d2 + d3) / 3).ToString("N2");
outputLabel.Text = output;


Was This Post Helpful? 0
  • +
  • -

#3 b.ihde  Icon User is offline

  • D.I.C for life
  • member icon

Reputation: 43
  • View blog
  • Posts: 1,070
  • Joined: 29-September 08

Re: C# coding project

Posted 16 April 2010 - 01:11 AM

Hey,

Convert.ToInt32 converts the numbers to an Integer, so 12,34 will be 12.
Using double or decimal, will get our program work.


            double myDouble = (Convert.ToDouble(textBox1.Text)
                + Convert.ToDouble(textBox2.Text)
                + Convert.ToDouble(textBox3.Text)) / 3;

            outputLabel.Text = myDouble.ToString(); ;
            outputLabel.Text = "The average of these numbers is " + outputLabel.Text;



Ben

Edit* ouh i was too slow :(

This post has been edited by b.ihde: 16 April 2010 - 01:12 AM

Was This Post Helpful? 0
  • +
  • -

#4 Yakyb  Icon User is offline

  • New D.I.C Head

Reputation: 3
  • View blog
  • Posts: 21
  • Joined: 04-June 08

Re: C# coding project

Posted 16 April 2010 - 01:26 AM

why not use the numeric up down control instead of the text box?

that way you would help limit a parsing error being thrown
Was This Post Helpful? 0
  • +
  • -

#5 Guest_shadowhand18*


Reputation:

Re: C# coding project

Posted 16 April 2010 - 11:38 AM

Thank you guys so much!!! Instead of using convert.Toint i should have used a double for the double numerics.
The reason I was using the textBox1 is because the teacher wanted us to use that for the time being, something about
teaching us how to rename the properties later (honestly you guys know more than he does!) thanks for all your help guys.
I got a feeling were going to meet again! This course is a little confusing.
Was This Post Helpful? 0

Page 1 of 1