• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 476
  • Last Modified:

how to only accept numbers in cin?

how to only accept number characters (0-9) in cin?
0
m-jansen
Asked:
m-jansen
  • 2
1 Solution
 
rajeev_devinCommented:
Use ignore. Something like
cin.ignore(...)

Check the link
http://www.augustcouncil.com/~tgibson/tutorial/iotips.html
0
 
m-jansenAuthor Commented:
cin.ignore(33) sounds good
0
 
B1-66ERCommented:
Hi m-jansen.
You can use ignore like rajeev_devin proposed or simply use cin get() method, and chech if its return value is a number:

 char c ;

 while (c = cin.get())
 {
  if (c > '0' && c < '9') cout << c << endl ;
 }
0
 
B1-66ERCommented:
sorry :)

if (c >= '0' && c <= '9') cout << c << endl ;
0

Featured Post

Prep for the ITIL® Foundation Certification Exam

December’s Course of the Month is now available! Enroll to learn ITIL® Foundation best practices for delivering IT services effectively and efficiently.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now