0

How do I make it so that the cin statement only accepts integers?

rectangletangle
  • 50,393
  • 94
  • 205
  • 275
  • 1
    What exactly do you mean by "only accepts integers"? Specifically, what do you want to happen when/if the user enters something else (e.g., a letter or punctuation)? – Jerry Coffin Sep 29 '10 at 19:02
  • http://stackoverflow.com/questions/2292202/while-loop-with-try-catch-fails-at-bad-cin-input – karlphillip Sep 29 '10 at 19:20
  • The `cin` is a generic object, you can't modify it to only supply integers. You can modify your program to only accept integers. Either write your own, filtered version of `cin` or write something that only takes integers from a file and pass a file to your program. – Thomas Matthews Sep 29 '10 at 19:52

4 Answers4

5

I don't think you can force std::cin to refuse to accept non-integral input in all cases. You can always write:

std::string s;
std::cin >> s;

since the string doesn't care about the format of the input. However, if you want to test whether reading an integer succeeded you can use the fail() method:

int i;
std::cin >> i;
if (std::cin.fail())
{
   std::cerr << "Input was not an integer!\n";
}

Alternatively, you can just test the cin object itself, which is equivalent.

int i;
if (std::cin >> i)
{
   // Use the input
}
else
{
   std::cerr << "Input was not an integer!\n";
}
Nick Meyer
  • 39,212
  • 14
  • 67
  • 75
  • 1
    "I don't think you can do such a thing globally", you can set `cin.exception(ios::failbit)` to apply it globally. – Yakov Galka Sep 29 '10 at 19:18
  • @ybungalobill Even with the exception mask, you can read other types sucessfully (`std::string s; std::cin >> s;`). To my knowledge, there is no way to force `operator>>` to *always* fail when trying to read a non-integral type (even when the input is in the correct format). – Nick Meyer Sep 29 '10 at 19:25
  • Ah, I see how the wording was confusing now. Editing. – Nick Meyer Sep 29 '10 at 19:27
  • Problem here is that it doesn't exactly make sure it's an int. If you typed say 10.5 it would get truncated and converted to an int. Which may not be what you expect or want. It's important to realize that >> is type safe and also to understand how type conversions work. – baudtack Apr 17 '12 at 18:09
4
#include <iostream>
#include <limits>
using namespace std;

int main(){
   int temp;
   cout << "Give value (integer): ";
   while( ! ( cin >> temp ) ){
      cout << "That was not an integer...\nTry again: ";
      cin.clear();
      cin.ignore( numeric_limits<streamsize>::max(), '\n' );
   }
   cout << "Your integer is: " << temp;
}

Found this source from: http://www.dreamincode.net/forums/topic/53355-c-check-if-variable-is-integer/ I needed to do this just yesterday :)

Kenny Cason
  • 12,109
  • 11
  • 47
  • 72
0

Another approach using std::cin.

#include <iostream>
#include <limits>

using namespace std;

int main()
{

    double input;
    while (cin)
    {
        cout << "Type in some numbers and press ENTER when you are done:" << endl;
        cin >> input;
        if (cin)
        {
            cout << "* Bingo!\n" << endl;
        }
        else if (cin.fail() && !(cin.eof() || cin.bad()))
        {
            cout << "* Sorry, but that is not a number.\n" << endl;
            cin.clear();
            cin.ignore(numeric_limits<std::streamsize>::max(),'\n');
        }
    }
}

g++ -o num num.cpp

karlphillip
  • 92,053
  • 36
  • 243
  • 426
-1

Put the cin in a while loop, and test.

cin.exceptions(ios::failbit | ios::badbit );
int a;
bool bGet = true;
while(bGet)
{
  try
  { 
    cin >> a;
    bGet = false;
  }
  catch
  { ; }
}      
Jess
  • 2,991
  • 3
  • 27
  • 40