1

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 to use like integer without fractional part.

#inclide <iostream>
using namespace std;
...
int n;
cout << "Your input is: "<<endl;
cin >> n;
while (cin.fail()) {
    cout << "Error. Number of elements must be integer. Try again: " << endl;
    cin.clear();
    cin.ignore(256, '\n');  
    cin >> n;
}
...        
      `

So, how to make this code see if the input is float?

2
  • 1
    read in as a string and parse. Commented Oct 18, 2016 at 22:57
  • You are asking the wrong question, the question that has a follow-up question: "And how do I observe any non-number input?" The question you should be asking is this: *"How do I determine, whether the entire input is part of an integer, and this answer perfectly addressed that. Commented Oct 19, 2016 at 0:33

3 Answers 3

2

You can try to convert the input string to a int using a std::istringstream. If it succeeds then check for eof() (after ignoring blank spaces) to see if the whole input was consumed while converting to int. If the whole input was consumed then it was a valid int.

Something a bit like this:

int input_int()
{
    int i;

   // get the input
    for(std::string line; std::getline(std::cin, line);)
    {
        // try to convert the input to an int
        // if at eof() all of the input was converted - must be an int
        if(!line.empty() && (std::istringstream(line) >> i >> std::ws).eof())
            break;

        // try again
        std::cout << "Not an integer please try again: " << std::flush;
    }

    return i;
}

int main()
{
    std::cout << "Enter an integer: " << std::flush;

    std::cout << "i: " << input_int() << '\n';
}
Sign up to request clarification or add additional context in comments.

Comments

0

Building on Raindrop7's solution, here's the full code to do what you need:

#include <cstdio>
#include <iostream>
#include <cmath>
using namespace std;

int main()
{
    double m;
    cout << "Your input is: "<<endl;
    cin >> 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

4 Comments

This will, by sheer coincidence, produce false positives. Floating point representation is inexact, and operator>> loses information. If things go bad, it turns a floating point value into something that is representable by an integer value. And there goes your test...
@IInspectable give an example proving what you say
@Raindrop7: Have a look at std::nextafter, std::nexttoward, and produce arbitrary examples that prove this fact yourself. (Hint: Start with any integer, call std::next towards a higher value, and enter the value that's just below the midpoint of those two values.)
thanx! yes that is good. but we for practice we create our own try. in a real program we use these new facilities
0

The code below should be able to do what you are hoping to achieve:

#inclide <iostream>
using namespace std;
int n;
cout << "Your input is: "<<endl;
while (!(cin >> n) || cin.get() != '\n') {
    cout << "Error. Number of elements must be integer. Try again: " << endl;
    cin.clear();
    cin.ignore(256, '\n');  
}

The program asks the user to re-enter an integer if either of the following happens:

  1. If the program is unable to extract an integer from the std::cin stream. (For example, when a character or string is entered by the user)
  2. If, after an integer is extracted successfully, the next character in std::cin is not the new line '\n' character. (For example, when a number with a decimal point like 1.1 is entered, or when an integer followed by a character like 1a is entered.)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.