1 Replies - 604 Views - Last Post: 31 October 2011 - 07:58 PM Rate Topic: -----

#1 parcero  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 8
  • Joined: 14-October 11

distance calculator

Posted 31 October 2011 - 07:53 PM

I am trying to create a distance calculator using a loop. the input is the MPH and the time traveled, the output is the list of distances traveled per hour. the validation is in the code and if is incorrect it needs to clear the boxes and focus at the box where the mistake was. Two primary errors that I do not understand are the <= 180 and <= 50 invalid expression and the "else" invalid expression. I do not know if there are other errors but those are highlighted. Thank you

 private void calculateButton_Click(object sender, EventArgs e)
        {
            // variables
            double speed;
            double hours;
            double distance;
            int count = 1;

            if (double.TryParse(speedTextBox.Text, out speed) && (speed > 0 && <= 180))
            {
                if (double.TryParse(hoursTextBox.Text, out hours) && (hours > 0 && <= 50))
                {
                    while (count <= hours)
                    {
                        distance = speed * hours;

                        distanceListBox.Items.Add("After hour" + count + "the distance is" + distance.ToString("c"));
 
                        count = count + 1;
                    }
                }
                else
                {
                    hoursTextBox.Text = "";
                    hoursTextBox.Focus();
                }
            }
            else
            {
                speedTextBox.Text = "";
                speedTextBox.Focus();
            }
        }

        private void exitButton_Click(object sender, EventArgs e)
        {
            this.Close();
        }


]

This post has been edited by no2pencil: 31 October 2011 - 07:56 PM
Reason for edit:: Corrected code tags


Is This A Good Question/Topic? 0
  • +

Replies To: distance calculator

#2 LaughingBelly  Icon User is offline

  • D.I.C Head

Reputation: 47
  • View blog
  • Posts: 103
  • Joined: 11-April 11

Re: distance calculator

Posted 31 October 2011 - 07:58 PM

While it makes sense when you read it aloud, the way you wrote the conditions does not make sense to the computer.

(speed > 0 && <= 180)
(hours > 0 && <= 50))
 


should be written as
(speed>0 && speed <=180)
(hours>0 && hours <=50)


Was This Post Helpful? 1
  • +
  • -

Page 1 of 1