1

I am trying to make a simple distance calculator that outputs the miles traveled over the course of a user defined time at a user defined speed to a list box. I have used a series of IF statements to catch any invalid input. After its loaded I can type invalid input and it functions properly, so I know that the problem has something to do with my if. When I type in proper numbers, the whole program freezes then windows tells me that it has quit responding. I've never had a problem like this before.

int vehicleSpeed;
int hoursTraveled;
int loopCounter = 1;

private void calculateDIstanceButton_Click(object sender, EventArgs e)
{
    if (int.TryParse(vehicleSpeedTextbox.Text, out vehicleSpeed))
    {
        if (int.TryParse(hoursTravledTextbox.Text, out hoursTraveled))
        {
            while (loopCounter <= hoursTraveled)
                distanceCalculationsListBox.Items.Add("The Distance traveled after " + loopCounter + " hour is " + (vehicleSpeed * hoursTraveled));
            ++loopCounter;
        }

        else
        {
            MessageBox.Show("That is not a valid input for time");
        }
    }

    else
    {
        MessageBox.Show("That is not a valid speed input");
    }

}
3
  • Can you say which book are you following? I would also like to learn some concepts and I am willing to buy a book. :) Commented Oct 15, 2014 at 22:54
  • @Vishal I am using Visual C# (third edition) by tony gaddis. Im taking this as an extracurricular course. Its a fairly nice book and it has a ton of exercises to help drill the ideas into you. Commented Oct 15, 2014 at 22:57
  • Thanks. I would take a look at that book. Have a nice day. Commented Oct 15, 2014 at 23:03

2 Answers 2

2

You need to wrap your loop contents in braces. As it stands now, the ++loopCounter command is out of scope of the while loop and never gets run on any iteration, which is causing it to infinite loop and causes your program to crash. Without braces, the while loop only runs the command on the next line. The braces force it to be in scope.

while (loopCounter <= hoursTraveled)
{
    distanceCalculationsListBox.Items.Add("The Distance traveled after " + loopCounter + " hour is " + (vehicleSpeed * hoursTraveled));
    ++loopCounter;
}
Sign up to request clarification or add additional context in comments.

8 Comments

Not sure "scope" is really the right concept here, but I get what you are saying. It may be more accurate to say its not in the expression-list, or statement-list, or something like that (I'd have to check the grammar to see what Microsoft calls it).
@BradleyDotNET you're correct, scope is probably not the correct term here. The loopCounter variable is in scope, but that statement is not included as part of the body of the while loop. I'm not sure I've ever really thought of what that term would be, though...
"Body of the loop" is probably the best understood unless you are familiar with grammars. I'm going with that :)
@meisenman I answered within seconds of this post, without copying anything. I resent the plagiarism accusation. My point was (and he agreed) that you don't normally say statements execute "in scope". Variables are "in scope". Also, my original post was slightly more comprehensive than this (much better than before) edited version. I stand by my post.
@meisenman to prove Bradley's point further, I upvoted his answer because a.) I don't think he copied mine and b.) he worded it much more clearly than I did and should get the votes anyway.
|
2
while (loopCounter <= hoursTraveled)
                distanceCalculationsListBox.Items.Add("The Distance traveled after " + loopCounter + " hour is " + (vehicleSpeed * hoursTraveled));
++loopCounter;

Is an infinite loop, because without {} the while loop consists of only the next statement after the while. Thus, loopCounter is never incremented, and the condition is always true. You need to use:

while (loopCounter <= hoursTraveled)
{
    distanceCalculationsListBox.Items.Add("The Distance traveled after " + loopCounter + " hour is " + (vehicleSpeed * hoursTraveled));
    ++loopCounter;
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.