I want to check if the input is valid, but when i do run this code I see that it checks only input for charcters. If i input a float number it will take it and going t
Building on Raindrop7's solution, here's the full code to do what you need:
#include
#include
#include
using namespace std;
int main()
{
double m;
cout << "Your input is: "<> m;
while (cin.fail() || (m-floor(m)))
{
cout << "Error. Nubmer of elements has to be integer. Try again: " << endl;
cin.clear();
cin.ignore(256, '\n');
cin >> m;
}
int n = (int)m;
return 0;
}
Here's a sample output:
Your input is:
2.7
Error. Nubmer of elements has to be integer. Try again:
erer
Error. Nubmer of elements has to be integer. Try again:
2