3 Replies - 4711 Views - Last Post: 10 May 2010 - 05:43 PM Rate Topic: -----

#1 PNWtroop  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • 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();

            //add header to ListBox
            lstBoxDistance.Items.Add("Vehicle speed: " + 
                string.Format("{0:F} ",txtSpeed.Text)+"MPH");            
            lstBoxDistance.Items.Add("Time traveled: " + 
                string.Format("{0:F} ",txtTime.Text)+"hours");
            lstBoxDistance.Items.Add("Hours\tDistance Traveled");

            //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);

            
            lstBoxDistance.Items.Add("Total Distance: " +
                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  Icon User is offline

  • D.I.C Head

Reputation: 57
  • View blog
  • Posts: 221
  • 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.
Was This Post Helpful? 0
  • +
  • -

#3 PNWtroop  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • 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.
Was This Post Helpful? 0
  • +
  • -

#4 PNWtroop  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • 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();

            //add header to ListBox
            
            lstBoxDistance.Items.Add("Vehicle speed: " + 
                string.Format("{0:F} ",txtSpeed.Text)+"MPH");            
            lstBoxDistance.Items.Add("Time traveled: " + 
                string.Format("{0:F} ",txtTime.Text)+"hours");
            lstBoxDistance.Items.Add("Hours\tDistance Traveled");

            //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; 
                lstBoxDistance.Items.Add(Hour +"\t" + hourlyDistance);
            }
           
            Distance = distance(Speed, Time);
            lstBoxDistance.Items.Add("Total Distance:  " +
               string.Format("{0}", Distance));

        }//end method btnCalculate_Click 



Was This Post Helpful? 0
  • +
  • -

Page 1 of 1