A try/catch problem, and a rounding issue

  • (2 Pages)
  • +
  • 1
  • 2

15 Replies - 881 Views - Last Post: 05 July 2011 - 06:41 AM Rate Topic: -----

#1 Goff256  Icon User is offline

  • D.I.C Head

Reputation: 4
  • View blog
  • Posts: 93
  • Joined: 04-July 11

A try/catch problem, and a rounding issue

Posted 04 July 2011 - 10:35 AM

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace WindowsFormsApplication3
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void btnAverage_Click(object sender, EventArgs e)
        {

            int Number1 = int.Parse(txt1.Text);
            int Number2 = int.Parse(txt2.Text);
            int Number3 = int.Parse(txt3.Text);
            decimal TotalAverage = (Number1 + Number2 + Number3) / 3M;
            lblAverage.Text = "The Average of those numbers is" + " " + TotalAverage.ToString("#.##");
            lblAverage.Text = "The Average of those numbers is" + " " + TotalAverage;

            try
            {
                Number1 = int.Parse(txt1.Text);
                txt1.Text = Number1.ToString();
            }
            catch
            {
                lblAverage.Text = "Error in user input.";
            }
            try
            {
                Number2 = int.Parse(txt2.Text);
                txt2.Text = Number2.ToString();
            }
            catch
            {
                lblAverage.Text = "Error in user input.";
            }
            try
            {
                Number3 = int.Parse(txt3.Text);
                txt3.Text = Number3.ToString();
            }
            catch
            {
                lblAverage.Text = "Error in user input.";
            }
        }

    }
}



When I put in three numbers, I am supposed to
A) Get an average
B) Get an average that is rounded to two places

I'm able to do one of those things, which is of course A.

The try/catch is supposed to make sure that people put in the correct type of information (good old numbers), but it's doing -nothing-.

I apologize, in advance, if this code is poorly done. I'm kind of new to this.

This post has been edited by Curtis Rutland: 04 July 2011 - 11:04 AM


Is This A Good Question/Topic? 0
  • +

Replies To: A try/catch problem, and a rounding issue

#2 BigR1983  Icon User is offline

  • D.I.C Head

Reputation: 57
  • View blog
  • Posts: 221
  • Joined: 12-April 10

Re: A try/catch problem, and a rounding issue

Posted 04 July 2011 - 10:50 AM

you are performing your operations before you verify that the input is correct.

you can do all the parses in one try catch.

try
{
     parse Number1;
     parse Number2;
     parse Number3;

     do operations;
     print output;
}
catch ()
{
     print error message
}


Was This Post Helpful? 3
  • +
  • -

#3 Goff256  Icon User is offline

  • D.I.C Head

Reputation: 4
  • View blog
  • Posts: 93
  • Joined: 04-July 11

Re: A try/catch problem, and a rounding issue

Posted 04 July 2011 - 11:07 AM

So all three of them can be put in a single try/catch field? Excellent.

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace WindowsFormsApplication3
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }


        private void btnAverage_Click(object sender, EventArgs e)
        {


            int Number1 = int.Parse(txt1.Text);
            int Number2 = int.Parse(txt2.Text);
            int Number3 = int.Parse(txt3.Text);
            try
            {
                Number1 = int.Parse(txt1.Text);
                txt1.Text = Number1.ToString();
                Number2 = int.Parse(txt2.Text);
                txt2.Text = Number2.ToString();
                Number3 = int.Parse(txt3.Text);
                txt3.Text = Number3.ToString();
            }
            catch
            {
                lblAverage.Text = "Error in user input.";

                decimal TotalAverage = (Number1 + Number2 + Number3) / 3M;
                lblAverage.Text = "The Average of those numbers is" + " " + TotalAverage.ToString("#.##");
                lblAverage.Text = "The Average of those numbers is" + " " + TotalAverage;

            }

        }
    }
}


Now the code is being... streamlined. I'm still running into errors when I try an invalid input, and not the error message type. I'm also still unable to round the numbers to the second decimal point. When I put 90, 90, and 80 as inputs, I get 86.666666666666666666. I apologize if this is all me being dense and simply user error.
Was This Post Helpful? 0
  • +
  • -

#4 Curtis Rutland  Icon User is online

  • (╯□)╯︵ (~ .o.)~
  • member icon


Reputation: 4433
  • View blog
  • Posts: 7,702
  • Joined: 08-June 10

Re: A try/catch problem, and a rounding issue

Posted 04 July 2011 - 11:14 AM

You're putting logic in the catch statement. That's just supposed to be for errors only. Move your average calculation out of the catch statement.
Was This Post Helpful? 1
  • +
  • -

#5 codeprada  Icon User is offline

  • Changed Man With Different Priorities
  • member icon

Reputation: 944
  • View blog
  • Posts: 2,353
  • Joined: 15-February 11

Re: A try/catch problem, and a rounding issue

Posted 04 July 2011 - 11:18 AM

Line#24 - 26 are doing the exact same thing as 29 to 34. Lines#30, 32, 34 aren't necessary. Line#24 - 26 is defeating the purpose of your try, catch statement since it's outside of it.

Your catch statement is suppose to have code that is to be performed when an exception is caught.
try
{
	int x = 10 / 0;
	Console.Writeline("Operation completed"); //will not reach
}
catch (Exception e)
{
	Console.Writeline("Exception Caught: " + e.Message);
}


So move Line#38 to 42 inside of the try clause. Also check out this link
Was This Post Helpful? 2
  • +
  • -

#6 Goff256  Icon User is offline

  • D.I.C Head

Reputation: 4
  • View blog
  • Posts: 93
  • Joined: 04-July 11

Re: A try/catch problem, and a rounding issue

Posted 04 July 2011 - 11:48 AM

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace WindowsFormsApplication3
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }


        private void btnAverage_Click(object sender, EventArgs e)
        {


            try
            {
                int Number1 = int.Parse(txt1.Text);
                int Number2 = int.Parse(txt2.Text);
                int Number3 = int.Parse(txt3.Text);
                decimal TotalAverage = (Number1 + Number2 + Number3) / 3M;
                lblAverage.Text = "The Average of those numbers is" + " " + TotalAverage.ToString("#.##");
                lblAverage.Text = "The Average of those numbers is" + " " + TotalAverage;
            }
            catch(Exception)
            {
                lblAverage.Text = "User Error";
            }
 
                

            }

        }
    }


Thanks to your help, I have finally been able to make the try/catch work. Now I'm stuck with the problem I had even before I tried to throw my try/catch into there. I have no clue how to make it round. I have been looking at every source material I can find, but apparently I'm just dense.
Was This Post Helpful? 0
  • +
  • -

#7 Curtis Rutland  Icon User is online

  • (╯□)╯︵ (~ .o.)~
  • member icon


Reputation: 4433
  • View blog
  • Posts: 7,702
  • Joined: 08-June 10

Re: A try/catch problem, and a rounding issue

Posted 04 July 2011 - 11:55 AM

Are you looking to round or trim?

Decimal.Round

Trimming is as easy as using a format string.
Was This Post Helpful? 1
  • +
  • -

#8 Goff256  Icon User is offline

  • D.I.C Head

Reputation: 4
  • View blog
  • Posts: 93
  • Joined: 04-July 11

Re: A try/catch problem, and a rounding issue

Posted 04 July 2011 - 12:15 PM

Round to the second decimal.

86.76 is what it's supposed to be, if I'm not a failure at math.
Was This Post Helpful? 0
  • +
  • -

#9 Curtis Rutland  Icon User is online

  • (╯□)╯︵ (~ .o.)~
  • member icon


Reputation: 4433
  • View blog
  • Posts: 7,702
  • Joined: 08-June 10

Re: A try/catch problem, and a rounding issue

Posted 04 July 2011 - 12:22 PM

Well, read the link I posted.
Was This Post Helpful? 1
  • +
  • -

#10 fixo  Icon User is offline

  • D.I.C Regular

Reputation: 85
  • View blog
  • Posts: 335
  • Joined: 10-May 09

Re: A try/catch problem, and a rounding issue

Posted 04 July 2011 - 01:41 PM

Just in addition
you might be want to use Decimal.TryParse instead of Parse
to handling errors outside of try..catch code block
and then calculate your expression right after try {
See here for more:
http://msdn.microsof...y/ew0seb73.aspx
Was This Post Helpful? 1
  • +
  • -

#11 Goff256  Icon User is offline

  • D.I.C Head

Reputation: 4
  • View blog
  • Posts: 93
  • Joined: 04-July 11

Re: A try/catch problem, and a rounding issue

Posted 04 July 2011 - 03:15 PM

Thanks. I had responded quickly before rushing off to spend time with my family. The article you linked helped me out, and now I feel kind of silly. I have the try/catch working. I have the rounding working. Still staring at the screen with a dumb expression on my face when it comes to fixing an error with the TryParse, but that's not completely necessary.

Is it?

No overload for method 'TryParse' takes 1 arguments

is the error, in case you were wondering.
Was This Post Helpful? 0
  • +
  • -

#12 fixo  Icon User is offline

  • D.I.C Regular

Reputation: 85
  • View blog
  • Posts: 335
  • Joined: 10-May 09

Re: A try/catch problem, and a rounding issue

Posted 04 July 2011 - 04:07 PM

See code
double n ;//or double n= new double();
// handling:
if (!double.TryParse(textbox1.Text, n)
{
MessageBox.Show("Problem to convert text")
return;
}
// otherwise all is OK, keep moving to the next box


This post has been edited by fixo: 04 July 2011 - 04:08 PM

Was This Post Helpful? 1
  • +
  • -

#13 Curtis Rutland  Icon User is online

  • (╯□)╯︵ (~ .o.)~
  • member icon


Reputation: 4433
  • View blog
  • Posts: 7,702
  • Joined: 08-June 10

Re: A try/catch problem, and a rounding issue

Posted 04 July 2011 - 04:21 PM

Not quite, fixo.

double n ;//or double n= new double();
// handling:
if (!double.TryParse(textbox1.Text, out n)
{
MessageBox.Show("Problem to convert text")
return;
}
// otherwise all is OK, keep moving to the next box



The double value is an "out" parameter.

This post has been edited by Curtis Rutland: 04 July 2011 - 04:21 PM

Was This Post Helpful? 3
  • +
  • -

#14 Goff256  Icon User is offline

  • D.I.C Head

Reputation: 4
  • View blog
  • Posts: 93
  • Joined: 04-July 11

Re: A try/catch problem, and a rounding issue

Posted 04 July 2011 - 05:31 PM

Thank you, everyone.

Seriously, you all saved my rear and taught me a lot.
Was This Post Helpful? 0
  • +
  • -

#15 fixo  Icon User is offline

  • D.I.C Regular

Reputation: 85
  • View blog
  • Posts: 335
  • Joined: 10-May 09

Re: A try/catch problem, and a rounding issue

Posted 05 July 2011 - 12:41 AM

View PostCurtis Rutland, on 04 July 2011 - 05:21 PM, said:

Not quite, fixo.

double n ;//or double n= new double();
// handling:
if (!double.TryParse(textbox1.Text, out n)
{
MessageBox.Show("Problem to convert text")
return;
}
// otherwise all is OK, keep moving to the next box



The double value is an "out" parameter.

Sorry my bad
You can trust me or not but I wrote first with out parameter
and then I've delete "out" :)
I don't know why, it's sclerozis may be :)
Thanks
Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2