Help with loops?

fbodieslive

New member
Joined
Feb 15, 2015
Messages
1
Programming Experience
Beginner
Hello,
I am a marketing major lost in a programming class. I have to code a GUI that calculates distance with a "while loop" and display the results into a listbox. I have the program working, but it isn't doing what I need it to do. When I input 40MPH into the speed textbox and 2 for hours textbox it lists 80 in the listbox. What I need it to do is list 40 then 80, to show the distance traveled the 1st hour and the 2nd hour. Here is my code.
C#:
   private void calculateButton_Click(object sender, EventArgs e)
        {
            int speed;
            int time;
            int count = 1;
            double distance;


            if (int.TryParse(speedTextBox.Text, out speed))
            {
                if (int.TryParse(timeTextBox.Text, out time))
                {


                    while (count <= time)
                    {
                       


                     distance = (speed * time);
                     
                     outputListBox.Items.Add("After hour" + count + "the distance" + "is" + distance.ToString());
                     count = count + 1;
                    }


                }
                else
                {
                    MessageBox.Show("Invalid time");
                }


            }
            else
            {
                MessageBox.Show("Invalid speed");
            }
        }
 
I have the program working, but it isn't doing what I need it to do.

If it's not doing what you need it to do then it's not working, whether it throws an exception or not.

Here's your chance to learn how to program properly. You don't just have an idea in your head of what should happen and then read your code to see if it looks like it might do that. If it's not working then the first thing to do is to write down what it is actually supposed to do. I'm not talking about a single sentence describing the overall process. I'm talking about ste-by-step instructions that someone else could read and write code for each step in any language and build a working application. Use a flowchart if you want but have something written down that you can compare your code to. If the code doesn't match what's written down the the issue is obvious.

If it still looks like it should work then it's time to debug. You place a breakpoint at the point you want to pause using the F9 key. When execution breaks at that point, you can then step through the code line by line using the F10 key, stepping into a method with the F11 key. At each step, you can use the Autos, Locals, Watch and Intermediate windows, as well as other tools, to test the state of your application. As soon as the reality doesn't meet your expectation, you've found an issue and you know exactly where to look to fix it. If reality never deviates from expectation then obviously your expectations aren't valid.

If you still can't work out how to fix the error when you get to that stage then by all means post back, but you'll be able to provide us with far more relevant information to be able to diagnose the issue, including all the variable values at the time.
 
Back
Top Bottom