input only integer

Sep 21, 2016 at 6:44am
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 Sep 21, 2016 at 6:45am
Sep 21, 2016 at 8:29am
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 Sep 21, 2016 at 8:29am
Sep 21, 2016 at 10:35am
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 Sep 21, 2016 at 10:36am
Sep 22, 2016 at 7:30am
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.
Sep 22, 2016 at 8:53am
For example :
How many student would you like to add : 411 663 772 455


Is that the case you want to handle?
Sep 22, 2016 at 11:46am
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>.
Sep 23, 2016 at 7:48am
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.
Sep 23, 2016 at 8:27am
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>
Sep 24, 2016 at 3:15am
If you do as I said, you can catch whitespace as an error.
Sep 30, 2016 at 4:15am
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.