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.– Maniero
well the idea is precisely to limit the input flow to variables of type int
– Diego
The
cin
will ensure that it is aint
. 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.– Maniero
if(!(Cin>> num && !num % 2 == 0)) that condition making the isolation however when I enter an integer it error
– Diego
I just said that this doesn’t make sense. Explain in Portuguese what should be the criteria to help you.
– Maniero
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
– Diego
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 variableint
?– Maniero
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
– Diego
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.
– Maniero
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
– Diego
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.
– Maniero