Help with looping

I need to ensure that h, d, and t aren't input negative (that part works) and that t is no greater that half the lesser of h and d. If I input a t value larger than h and d, it still computes instead of looping to ask for input again. This is connected to a header file and another cpp file but I'm not concerned with any of that right now.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#include <iostream>
using std::cin;
using std::cout;
using std::endl;

#include "DWallVol.h"

int main()
{
  double h, d, t;

  do
  {
	cout << "Enter the cylinder height, diameter, and wall thickness:  ";
	cin >> h >> d >> t;
  }while (h < 0 || d < 0 || t < 0 || (d < h && t > (0.5 * d)) || (h < d && t > (0.5 * h)));

  cout << endl << "A hollow cylinder with height " 
    << h << ", diameter " << d << ", and wall thickness " << t << endl
    << "has volume of "
    << tankWallVolume(h, d, t) << " square units." << endl;

  return 0;
}
Are you testing if d and h are the same?
no, but that shouldn't affect the tests when they're different
Well, I don't know why it works, but I changed it on mine and I've tested it about ten times now with completely random values and it's worked just fine.

1
2
3
4
5
  do
  {
	cout << "Enter the cylinder height, diameter, and wall thickness:  ";
	cin >> h >> d >> t;
  }while (h < 0 || d < 0 || t < 0 || (d <= h && t > (0.5 * d)) || (h < d && t > (0.5 * h)));


EDIT: Also tested with rand() about 20 more times with a max value of 999.
Last edited on
huh. weird. well thanks
Sometimes the program just does what it wants.
Topic archived. No new replies allowed.