Jessica Alvarado
Jessica Alvarado

Reputation: 75

While loop creating infinite loop

So I'm creating a program for a C++ class, and I created a while loop to stop invalid inputs. Every time I do test it with an invalid input it goes into an infinite loop. I'm new to coding, so I really don't know how to fix it.

 cout << "Enter weight in ounces (Max: 1800)" << endl;
    cin >> pkgweight;

    while (pkgweight > 0 || pkgweight < 1800)
    {
        cout << "Weight out of range. Program terminating.\n" << endl;
    }

    cout << "Enter miles shipping (Max: 3500)" << endl;
    cin >> distance;

    while (distance > 0 || distance < 3500)
    {
        cout << "Shipping distance out of range." << endl;
        cout << "Program terminating.\n" << endl;
    }

Upvotes: 3

Views: 503

Answers (3)

0x1a4
0x1a4

Reputation: 111

Looking at your code, it seems like you want to kill the program if the input is wrong. You could consider terminating the function. Is this all in main()? If it is not in an external function, just do return -1. I know this is probably bad programming practice, but hey, it works specifically for this!
By the way, your conditional said > 0 and < 1800, which means the program will terminate if the variables distance and pkgweight are in the specified range.
Here is my working snippet without these errors, tested on onlineGDB.

 #include <iostream>
 using namespace std;
 int main ()
 {
    int pkgweight, distance;
    cout << "Enter weight in ounces (Max: 1800)" << endl;
    cin >> pkgweight;

    while (pkgweight < 0 || pkgweight > 1800)
    {
        cout << "Weight out of range. Program terminating.\n" << endl;
        return -1;
    }

    cout << "Enter miles shipping (Max: 3500)" << endl;
    cin >> distance;

    while (distance < 0 || distance > 3500)
    {
        cout << "Shipping distance out of range." << endl;
        cout << "Program terminating.\n" << endl;
        return -1;
    }
    return 0;
}

Of course, instead of switching the less-than and greater-than, you could always wrap the conditional in a not operator.

Upvotes: 0

TimD1
TimD1

Reputation: 1032

If you want the user to be able to fix an incorrectly entered input, you would want:

 cout << "Enter weight in ounces (Max: 1800)" << endl;
 cin >> pkgweight;

 while (pkgweight > 0 || pkgweight < 1800)
 {
     cout << "Weight out of range. Program terminating.\n" << endl;
     cout << "Enter weight in ounces (Max: 1800)" << endl;
     cin >> pkgweight;
 }

That way, if the user enters a number which is outside of the valid range, they will be prompted to enter a new number. If the new value is within the range, the loop will exit.

The problem with your current program is that a while loop will execute "while" the condition it checks for is true. In your current program, once pkgweight is set, it stays the same value. This means that if the loop is entered because the condition it checks for is true, that condition will never change (allowing the loop to exit), and your error message will be printed indefinitely.

Upvotes: 1

tadman
tadman

Reputation: 211540

If nothing changes inside that loop, the exit condition will never be tripped.

Maybe you mean:

int pkgweight = 0;

cout << "Enter weight in ounces (Max: 1800)" << endl;
cin >> pkgweight;

if (pkgweight < 0 || pkgweight > 1800)
{
  cout << "Weight out of range. Program terminating.\n" << endl;
}

You'll want to use while for situations where you want to loop until some condition is met. if is like a non-looping while.

While it's great that you're learning and it's understood you're going to make mistakes, slipping up on something this fundamental is usually a sign you don't have a good reference to work from. Get yourself a solid C++ reference book and refer to it often if you're ever stumped about something. This is essential for learning properly, not just picking up bits and pieces here and there and trying to intuit the gaps. Many parts of C++ will not make sense, they are artifacts of design decisions decades old and the influence of other programming languages you've never heard of. You need a proper foundation.

Upvotes: 2

Related Questions