Distance Calculator

Page 1 of 1

3 Replies - 16511 Views - Last Post: 10 May 2010 - 05:43 PMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'https://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=172864&amp;s=dbe0b2159e5c13e0c1be7e2dd98fec98&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

#1 PNWtroop

Reputation: 0
• Posts: 27
• Joined: 01-April 10

Distance Calculator

Posted 09 May 2010 - 11:04 PM

I am trying to create a Windows form application distance calculator that executes the following formula Distance= Speed * Time. The exercise called for us to use a listbox but that was changed to using 2 textboxes, one for the user to enter the speed and the other for time. We have a Calculate button and an Exit button. When the calculate button is clicked the vehicle speed and the time of travel is to be displayed in a listbox along with the distance traveled for each hour of that time period. The total distance should be the last to be displayed.

I have the application displaying everything but the incremental hour and the distance traveled. Can some one help me with the loop that I should be using?

Here is the code I have thus far.
``` //declare variables
int iHour = 0;   //counter
double Speed;
double Time;
double Distance;
//int Hour = 0;
//double hourlyDistance = 0;

//method

double distance(double speed, double time)
{
return speed * time;
}

//handles Calculate button click event.
private void btnCalculate_Click(object sender, EventArgs e)
{

//clear any text in the list box
lstBoxDistance.Items.Clear();

string.Format("{0:F} ",txtSpeed.Text)+"MPH");
string.Format("{0:F} ",txtTime.Text)+"hours");

//retrieve user input and assign values
//to their respective variables
Speed = double.Parse(txtSpeed.Text);
Time = double.Parse(txtTime.Text);

//calculate the distance
for (iHour = 1; iHour <= Time; iHour++)
Distance = distance(Speed,Time);

string.Format("{0:F}", Distance));

}//end method btnCalculate_Click

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

```

Is This A Good Question/Topic? 0

Replies To: Distance Calculator

#2 BigR1983

Reputation: 57
• Posts: 226
• Joined: 12-April 10

Re: Distance Calculator

Posted 10 May 2010 - 04:35 AM

so each time you are going through your loop, you are just doing the calculation and not storing it anywhere.

```
//calculate the distance
for (iHour = 1; iHour <= Time; iHour++)
{
Distance = distance(Speed,Time);
lstBoxDistance.Items.Add("Total Distance: " + string.Format("{0:F}", Distance));
}

```

try that code, it should populate your list box each time you run through the loop.

#3 PNWtroop

Reputation: 0
• Posts: 27
• Joined: 01-April 10

Re: Distance Calculator

Posted 10 May 2010 - 02:12 PM

It tried the code and it gave me the total distance three times rather than for each hour and then cumulnating with the total Distance.

#4 PNWtroop

Reputation: 0
• Posts: 27
• Joined: 01-April 10

Re: Distance Calculator

Posted 10 May 2010 - 05:43 PM

Ok I worked through this and here is how I finally got it to work. so that it showed the hours incrmentally and the distance traveled for that period of time.

```         //handles Calculate button click event.
private void btnCalculate_Click(object sender, EventArgs e)
{

//clear any text in the list box
lstBoxDistance.Items.Clear();

string.Format("{0:F} ",txtSpeed.Text)+"MPH");
string.Format("{0:F} ",txtTime.Text)+"hours");

//retrieve user input and assign values
//to their respective variables

Speed = double.Parse(txtSpeed.Text);
Time = double.Parse(txtTime.Text);

//calculate the distance
for (Hour = 1; Hour <= Time; Hour++)
{
hourlyDistance = Hour * Speed;
}

Distance = distance(Speed, Time);