Jump to content
  • Advertisement
Sign in to follow this  
pascalosti

only int

This topic is 4384 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Advertisement
so it accepts only numbers, no letters or other signs.


example
int num;
cout<< "enter number 1-5" << endl;

cin >> num; // before it gets assigned make sure the input is a number.


Share this post


Link to post
Share on other sites
You would probably have to do non-blocking input so that you can check the pressed key in real-time.

Share this post


Link to post
Share on other sites
Quote:
Original post by Dave
You would probably have to do non-blocking input so that you can check the pressed key in real-time.

You'd just have to do the same thing cin already does, just filtered so that letters and such are ignored. That would be really hard, and completely not worth the effort.

cout << "Enter 1-5. Please do not enter any letters or other signs." << endl;
cin >> num;
if(!cin) cout << "I said please :(" << endl;

CM

Share this post


Link to post
Share on other sites
Quote:
Original post by Conner McCloud
Quote:
Original post by Dave
You would probably have to do non-blocking input so that you can check the pressed key in real-time.

You'd just have to do the same thing cin already does, just filtered so that letters and such are ignored. That would be really hard, and completely not worth the effort.

cout << "Enter 1-5. Please do not enter any letters or other signs." << endl;
cin >> num;
if(!cin) cout << "I said please :(" << endl;

CM


Fair play.

I don't think i have ever tried it in C++ :/

Share this post


Link to post
Share on other sites
its not working for me?

#include "stdafx.h"
#include <iostream>
using namespace std;


int _tmain(int argc, _TCHAR* argv[])
{
int answer = 0;
while(answer != 8){
cout << "Enter number"<< endl;
cin >> answer;
if(!cin) {
cout << "I said please :(" << endl;
answer = 9;}

cout << "The answer is "<< answer <<endl;
}
return 0;
}

Share this post


Link to post
Share on other sites
MSDN exemple of cin do just that, but it use cin.fail() instead of !cin..... don't forget to .clear() on error ;)

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!