Kuda Hitam - 29 days ago 8

C++ Question

so, here's my code

`#include <iostream>`

#include<math.h>

using namespace std;

int main()

int T, N;

int i = 1;

int y = 5;

cin >> T;

while (T-- ) {

cin >> N;

int c = pow(N, y);

int z = (c + 1) / (N + 1);

cout << "Kasus #" << i++ << ": " << z << endl;

}

}

all i want to do is limiting the user input ( T and N), T is between 1 and 10, N is between 1 and 100000, how can i do that?

Answer Source

Simply replace 1, and 10 with your min and max desired input. This will loop and ask for input to T for as long as the input is less than the minimum or greater than the maximum desired value.

```
#include <iostream>
int main() {
int T;
do {
std::cin >> T;
} while (T < 1 || T > 10);
return 0;
}
```

As a function for use on integers:

```
// Get valid input in given range: [min, max]
int getValidInputInRange(int min, int max) {
int val;
do {
std::cin >> val;
} while (val < min || val > max);
return val;
}
```

Then: `int T = getValidInputInRange(1, 10);`