Code Snippets

  

C++ Source Code


Welcome to Dream.In.Code
Become a C++ Expert!

Join 149,632 C++ Programmers for FREE! Get instant access to thousands of C++ experts, tutorials, code snippets, and more! There are 2,116 people online right now. Registration is fast and FREE... Join Now!





Input Validation in C++

Code will check whether the number entered is integer or not.

Submitted By: Xing
Actions:
Rating:
Views: 11,034

Language: C++

Last Modified: October 5, 2006

Snippet


  1. #include <iostream>
  2. #include <limits>
  3.  
  4. using namespace std;
  5.  
  6. int main() {
  7.   int number = 0;
  8.   cout << "Enter an integer: ";
  9.   cin >> number;
  10.   cin.ignore(numeric_limits<int>::max(), '\n');
  11.  
  12.   if (!cin || cin.gcount() != 1)
  13.     cout << "Not a numeric value.";
  14.   else
  15.     cout << "Your entered number: " << number;
  16.   return 0;
  17. }

Copy & Paste


Comments


There are currently no comments for this snippet. Be the first to comment!

Add comment


You must be registered and logged on to </dream.in.code> to leave comments.




Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter

Live C++ Help!

C++ Tutorials

Reference Sheets

C++ Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month