• Advertisement
Sign in to follow this  

only int

This topic is 4150 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
could you be more precise about what type of input you are talking about?!

Share this post


Link to post
Share on other sites
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
Im positive there is a simple call, i just dont remember it.
Maybe it was part of the Standard Template Library.

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
i get the same result

#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.fail()) {
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
I think Dunge means the following. Note the cin.clear(); line.


#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.fail())
{
cout << "I said please :(" << endl;
answer = 9;
cin.clear();
}
cout << "The answer is "<< answer <<endl;
}
return 0;
}



Also you say it's not working. You need to be more precise when you say something isn't working.

Caveat: I don't often program in C++ and I have not tested the above code.

Share this post


Link to post
Share on other sites

Quote:
Original post by Greig Hamilton
I think Dunge means the following. Note the cin.clear(); line.
*** Source Snippet Removed ***
Also you say it's not working. You need to be more precise when you say something isn't working.

Caveat: I don't often program in C++ and I have not tested the above code.


Still does not work.
If you enter a Letter, the console goes into hyper mode.

Share this post


Link to post
Share on other sites
ya that will happen i recommend extraction an string and then check if its all numbers *then* convert it or else your program will go crazy.

sorta-like


bool numb = false;
string input;

while (numb != true ) {

cin >> input;
numb = true;


for ( int x = 0; x < input.length(); ++x )
if ( input[x] != '0' && input[x] != '1' && input[x] != '2' ) //you get the idea
numb == false;
}




NOTE: The above code has not been compiled and will almost for sure have errors.

[Edited by - slymr on October 16, 2006 9:22:09 PM]

Share this post


Link to post
Share on other sites
Or better yet, use the standard library function isdigit which was created for just such a purpose:

#include <iostream>
#include <string>
#include <cctype>

bool isInteger(const std::string & integer_string)
{
for(unsigned int i = 0; i < integer_string.length(); i++)
{
if(!std::isdigit( integer_string ))
{
return false;
}
}
return true;
}

int main()
{
std::string integer_string;
do
{
std::cout << "Enter an integer: ";

std::cin >> integer_string;
}while(!isInteger(integer_string));

std::cout << "You entered an integer!" << std::endl;

return 0;
}

Share this post


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

  • Advertisement