input only integer

Please someone help me out with this question.
I want to input only integer number, except character and even <whitespace>
if I enter character or Whitespace >> then output " Please enter an integer:"
(I want to use the form as below by using While loop)
thank u

1
2
3
4
5
6
7
8
cout << "How many student would you like to add: ";
	
	while (!(cin >> istudent)) 
	{
		cout << "Please enter an integer:\n";
		cin.clear();
		cin.ignore(256, '\n');
	}
Last edited on
1
2
3
4
5
6
7
8
9
10
11
12
13
14
cout << "How many student would you like to add: ";
	
	while (true) 
	{
		cout << "Please enter an integer : "; cin >> numStudents;
                if(!cin)
                {
   		    cin.clear();
		    cin.ignore(numeric_limits<streamsize>::max(), '\n');
                    cout << "Invalid integer!!! Please try again\n\n";
                    continue;
               }
               else break;
	}
Last edited on
Hello Superman99,

Your while is a good start, but I have seen it this way:
1
2
3
4
5
6
7
8
9
cin >> istudent

while (!cin)
{
    cout << "Invalid integer!!! Please try again\n\n"
    cin.ignore(numeric_limits<streamsize>::max(), '\n');
    cout << "Invalid integer!!! Please try again\n\n";
    cin >> istudent
}

this way if you enter something that is not an integer it will say in the while loop until you do. SakurasouBusters's code will work also.

Hope that helps,

Andy
Last edited on
Thank you Handy Andy and SakurasouBusters. But I could not run your code perfectly, I did not know why bros. could you guys check for me once again please. Thank you

And we need user can input: <whitespace> enter
then run the output after that . that's is something that i could not fix it. thank u 2 you.
For example :
How many student would you like to add : 411 663 772 455


Is that the case you want to handle?
Get input as a std::string.

Check your string to verify it only contains digits. (#include <cctype> and use std::isdigit().)
(If you wish to act on user just pressing Enter or anything else, now is the time to do it.)

Convert to int using one of the functions in <string>.
SakurasouBusters, my case is

How many student would you like to add : @$%^$% < enter >
---> output : please enter an integer number : hdnfvjfn < enter >
---> output : please enter an integer number : <whitespace> <enter>
---> output : please enter an integer number:
that is what I want to do, especially WHITESPACE is which I could not make it

when i type an space and then enter, the output is :
_
_
_
_

so it mean the computer can not read the whitespace. hopefully you understand my case , ~.~ thank you so much for helping me SakurasouBusters.
It can read whitespace, but the formatted input does skip whitespace by default.
Therefore, when you have typed <whitespace> <enter>, which are both white, it is still reading input until you give non-white.
E.g. ---> output : please enter an integer number : <whitespace> <enter>7<whitespace>
will read the 7.

There is http://www.cplusplus.com/reference/ios/noskipws/


What about the case:
---> output : please enter an integer number : <whitespace>42<enter>
If you do as I said, you can catch whitespace as an error.
Thank u for all that you guys help me, i did find the way to do it :)

i used : cin >> noskipws >> istudent;

that is how i can catch the space .
Topic archived. No new replies allowed.