Limit input flow to values of type int

Asked

Viewed 1,906 times

3

#include <iostream> 
#include <limits> 

using std::cout;
using std::cin;
using std::endl;
int getVar(int num);
int getInt(int num);

int main(){

    int n;
    cout<<"Insira um inteiro. \n\n";
    getInt(n);


    return 0 ;
}
int getInt(int n){

    cin>>n;
    return getVar(n);
}

int getVar(int num){

    if(!(cin>> num &&  !num % 2 == 0)){

        cout<< num <<" Entrada nao corresponde ao tipo de variavel solicitado.\n\n";
        cin.clear();
        cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
        getInt(num);
    }
    else{
        cin.clear();
        cout<<"\n"<< num <<" Entrada recebida com sucesso!!! \n";
        return num;
    }
}

When I type a number that is accepted by the function, in the case of an integer, I have to type it twice for it to be captured, I would like it to pick up the direct input on the first attempt.

  • The condition is probably wrong. It makes no sense. What is the criterion for which a number should be accepted or rejected? Explain what the program should do. There is some reason for having two auxiliary functions and not being all in the main()? The code is full of unnecessary things.

  • well the idea is precisely to limit the input flow to variables of type int

  • 1

    The cin will ensure that it is a int. The only thing you can do is to determine whether what was entered is valid or not. But then you need to explain what is considered valid or not.

  • if(!(Cin>> num && !num % 2 == 0)) that condition making the isolation however when I enter an integer it error

  • I just said that this doesn’t make sense. Explain in Portuguese what should be the criteria to help you.

  • if the input is a character or a float type it should not read with input if it is of the whole type it should read as input

  • This phrase doesn’t make sense either. So let’s split. How will you find out if the input is of the type float if it is impossible to type one into a type variable int?

  • in the case that this codition that I put was everything that is different from an integer is isolated (in the case of char )and if the division module of what was typed by 2 other than 0 should not be considered because it is a float

  • 1

    But this is the problem, this condition does not do what you want. P module determines whether the number is even. The request to enter the number again makes no sense. If you can’t put the problem in a way that makes sense, explain what you want, because the code is like this, there’s no way to help.

  • but the module of a division of a number of type float never returns an even number or returns I thought this was a feature to isolate the float type input

  • I will try to post a solution that solves this in the best possible way, but there is no simple and clear solution. I will organize the code as well. It’s a lot of trouble for something simple. The current code can give stack overflow in extreme case.

Show 6 more comments

1 answer

3


First, if you are going to take a number that can be written with a floating point, the variable type must be float and not int. It’s weird to do this, because if you want whole numbers, ask whole numbers, then you don’t have to do anything. But it must be some crazy nonsense exercise. You can use another form, but it’s more complex.

To know if a number that can have decimal part is only integer, just compare it with its whole part, making a cast simple as it was done in condition. So it is comparing the number with decimal and the number converted to integer. If they are the same it is because the conversion has lost nothing, then it was already whole.

I removed all unnecessary statements and functions that were not bringing anything useful to the code, on the contrary, it was caused problems.

#include <iostream>
#include <limits>
using namespace std;

int main() {
    float n;
    cout << "Insira um inteiro." << endl;
    while (true) {
        cin >> n;
        if (!cin.fail() && n == (int)n) break;
        cout << n << " - Entrada nao corresponde ao tipo de variavel solicitado." << endl;
        cin.clear();
        cin.ignore(numeric_limits<streamsize>::max(), '\n');
    }   
    cout << endl << n << " - Entrada recebida com sucesso!!!" << endl;
}

Behold working in the ideone. And in the repl it.. Also put on the Github for future reference.

The verification whether cin.fail() was used to prevent the typing of non-numeric characters.

It is possible to simplify the code a little more if you accept a small change in behavior. Could not give the error message, would have to just ask for the number again.

  • Let me see if I intendi you determine while true capture the value of n if n > 0 and n is = n integer but then it does not round the float number to int

  • That’s pretty much it. The check is dispensable to see if a number was typed, as I explained above, put to check if letters were typed, but this check is flawed, I do not know even if I should do this. It’s just that if you want to check if the data is right, it’s weird to accept letters as if it were something valid. Anyway, I’ll repeat that it doesn’t make sense to do this. It has to simplify this code even more but it would slightly change the behavior.

  • and if the input is 0 it would be an integer but would be classified as not

  • It is, then, this is the flaw. If you want to take this part. But then and if the entry is letter?

  • I understand face I thank you immensely to start now so still they are much doubt but of face your code is much cleaner than mine.but you can give me an idea of how to make this isolation of the letters without wanting to ask too much

  • or indicate to me something that should study for such at the moment to starting the C++ as programming 5ª ed

  • I found a solution to solve the problem of using non-numeric characters.

  • you wondered about the idea of doing this is that in the exercises that are passed in the book, always talk to capture the input with Cin only it always seems that the code is not correct because it does not capture the input correctly, so I decided to create ramifications, like something that captures only int, only float, only char and gave in that you saw

  • how is the solution?

  • 1

    But if it’s to "capture" int, use a variable int as I was doing, and do not need to check whether there is floating point or not, mainly do not need to split by 2 to see if it gives rest, do not need to read the number again to do the check. Don’t do what language already does for you. You’re playing random codes. If you don’t understand what you’re doing, you won’t be able to produce anything useful right that works. The solution lies in the answer.

Show 5 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.