-3

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?

Kuda Hitam
  • 13
  • 1
  • 2
  • By checking the values, and asking again? – StoryTeller - Unslander Monica Sep 24 '17 at 12:35
  • 5
    `do { read_input; } while { input_is_not_what_i_want };` – 463035818_is_not_an_ai Sep 24 '17 at 12:35
  • i want the user to input number only between 1 and 10, is that possible? – Kuda Hitam Sep 24 '17 at 12:38
  • There are two things you need to do: The first is to make sure that the user have given some input that actually *is* a number. This can be done by checking the status of the `std::cin` object ([it can be used directly in a condition](http://en.cppreference.com/w/cpp/io/basic_ios/operator_bool), and remember [the input operator](http://en.cppreference.com/w/cpp/io/basic_istream/operator_gtgt) returns the stream itself). Then you need to check if the number is within the expected range. – Some programmer dude Sep 24 '17 at 12:45
  • Well, no, you can't stop a user entering anything they want (short of using dedicated hardware support to do something, such as electrocuting the user who hits the `1` key twice in succession). The best you can do without harming your user is read the input, check it, and - if the data is not what is wanted - discard and read again. – Peter Sep 24 '17 at 12:48

1 Answers1

-2

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);

Carl
  • 2,057
  • 1
  • 15
  • 20
  • Should probably put the whole input-operation in a loop condition as well. What if the user inputs something which is not a number? – Some programmer dude Sep 24 '17 at 12:43
  • This question is severely off-topic. It boils down to "give me the codez". When answering such questions, please ask yourself who benefits? Will the OP benefit by you helping them cheat, and rob themselves of the experience one gets by figuring this out? Will future users actually find this question and glean something from it? I know the reputation grind is hard, but this is counter productive. – StoryTeller - Unslander Monica Sep 24 '17 at 12:43
  • im not cheating for anything, im just on a training and want to know how to do it. – Kuda Hitam Sep 24 '17 at 12:44
  • @KudaHitam - You are cheating yourself mostly. – StoryTeller - Unslander Monica Sep 24 '17 at 12:46
  • @StoryTeller Yes, I suppose that is a fair point. Should I then delete the answer? – Carl Sep 24 '17 at 12:47
  • @KudaHitam The way to get experienced is to *do*. As Yoda once said, "there is no try". Experiment, look what can be done with the statements and expressions and operators you know about. Learn new operators and statements. [Read good books](http://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list). And experiment, experiment, experiment. But mostly, your your brain. Try to think about it logically. – Some programmer dude Sep 24 '17 at 12:47
  • @Carl - That would a decent thing to do. And a great testament to your character. – StoryTeller - Unslander Monica Sep 24 '17 at 12:48
  • limiting input is not only about checking if it is smaller 10 and bigger 1, but also about checking if it was a number – 463035818_is_not_an_ai Sep 24 '17 at 13:12
  • @StoryTeller I was unable to delete this, as the answer got accepted in the meantime. Is there another way? If not, I shall be certain to refrain from posting "cheaty" answers in the future. – Carl Sep 25 '17 at 13:00
  • [The system will delete this entire post in due time](https://meta.stackexchange.com/a/5222/317773). – StoryTeller - Unslander Monica Sep 25 '17 at 13:11