how to only accept numbers in cin?

Posted on 2006-04-21
Last Modified: 2008-02-26
how to only accept number characters (0-9) in cin?
Question by:m-jansen
    LVL 12

    Expert Comment

    Use ignore. Something like

    Check the link

    Author Comment

    cin.ignore(33) sounds good
    LVL 5

    Expert Comment

    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 ;
    LVL 5

    Accepted Solution

    sorry :)

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

    Featured Post

    IT, Stop Being Called Into Every Meeting

    Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

    Join & Write a Comment

    Often, when implementing a feature, you won't know how certain events should be handled at the point where they occur and you'd rather defer to the user of your function or class. For example, a XML parser will extract a tag from the source code, wh…
    This article will show you some of the more useful Standard Template Library (STL) algorithms through the use of working examples.  You will learn about how these algorithms fit into the STL architecture, how they work with STL containers, and why t…
    The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
    The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.

    729 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    20 Experts available now in Live!

    Get 1:1 Help Now