I can't get this while loop to keep looping and not do a fast loop.
The full code is downloadable

while (num == 0) {
        cout << "Type in two values:\n";
        cin >> num;
        num1 += num;
        cin >> num;
        num2 += num;
        cout << "Which measurment unit do you want to use cm/m/in/ft?";
        cin >> un;

        cout << "\nValue 1: "<<num1;
        cout << "\nValue 2: " << num2 <<"\n";

        if (num1 > num2)
        {
            cout << "\nThe largest so far: " << num1;
            cout << "\nThe smallest so far: " << num2;
        }
        else if (num1 < num2) {
            cout << "\nThe largest so far: " << num2;
            cout << "\nThe smallest so far: " << num1;
        }
    
        cout << "\n";
        num = 0;
        

        
            
    }

Recommended Answers

All 3 Replies

try throwing a cin.ignore() at the bottom before you close off the the loop.

while (num == 0) {
cout << "Type in two values:\n";
cin >> num; // num isn't 0 if you enter anything but 0, no more while loop.

You said "keep looping", obviously it won't loop if num isn't 0

If you want it to loop forever, try doing this:
while(true) {
}

Then it will never end.

That's kind of bad though, so you should just use an exit value.

char input('a');
while( input != 'n' ) {
  std::cout << "Type n to quit.";
  std::cin >> input;
}

Edit: Oops, I lied. You put num=0; at the end.
What do you mean by keep looping?
Do you want the min/max of every value you've ever entered (not just 2 values)?
If so you'll need an array to store the data.

Well in the loop
I want it to ask for a number twice and declare those two numbers then compare them.
Then I want it to restart or close by asking whether or not you chose to.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.