Welcome, Guest. Please login or register.

Author Topic: C/C++ programming  (Read 4209 times)

Description:

0 Members and 1 Guest are viewing this topic.

Offline smithy

  • Hero Member
  • *****
  • Join Date: Mar 2002
  • Posts: 511
    • Show all replies
Re: C/C++ programming
« on: November 08, 2005, 04:58:39 PM »
Quote

jonssonj wrote:
Hello!

I wonder how I can check that a input made by a user, really is an integer and not anything else.

int main(void)
{
   int test;
   cout << "Enter integer: ";
   cin >> test;

   // Now I want to check that the user really entered an
   // integer and not anything else.


Add this:

Code: [Select]

   if(cin.fail())
   {
       // a number wasn't entered
   }
   else
   {
      if(!cin.eof())
      {
          // there is more non-numeric input
      }
   }
}