Looping, Kind Of...

Hello. I need to have a program display an error message if the variable entered isn't an integer but then I want it to cin again. I have this but it doesn't work:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
cout << "Enter an Integer: " ;
    for (;;)
    {
        cin >> var;
        if (!cin)
        {
            cout << "That wasn't ant integer dumbass!\n" ;
            break ;
        }
        else
        {
            break ;
        }
    }

I am not sure how to do what I want and this doesn't work, it just repeats That wasn't an int.. over and over again. Any help would be appreciated. Thank you!
Last edited on
I would, instead, perform the if() statements on the var, not cin... would require a bit of conversion, though.
@IWishIKnew ,
I did what you said:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
cout << "Enter an Integer: " ;
cin >> var;
for (;;)
    {
        if (!cin)
        {
            cout << "That wasn't ant integer dumbass!\n" ;
            break ;
        }
        else
        {
            break ;
        }
    }

But now I need the program to repeat the "Enter an Integer"
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#include <limits>
#include <iostream>

int main(int argc, char* argv[]) {

	unsigned short value = 0;
	while(1) {
		std::cout << "Enter value: ";
		if(std::cin >> value) {
			break;
		}
		std::cin.clear();
		std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
	}

	std::cout << "You entered : " << value << std::endl;

	std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
	std::cin.get();
	return 0;
}
That works very well xismin. Could you briefly explain the 13th line. I understand everything else except that. I want to understand so next time I need something like this I'll know whats going on. Thank you!
(I'm going to back up a little bit to explain this)
So when the user enters something that's not an int, cin kicks up an error flag and line 9 becomes false as a result.
You need to clear that error flag first before you do any other input operations with cin, which is why line 12 is needed (it clears all error flags).
But the gibberish that you inputted is still there, so that all has to get cleared out as well before you can try again to input a number.
cin.ignore basically does this for you (line 13).
Technically, you could just do something like cin.ignore(1000, '\n'); (which will discard 1000 characters or until it reaches a newline character), but that won't work if you type 1001 characters (or more) of gibberish, since it'll only get rid of the first 1000. (But if you know for sure that nobody who's going to use this program would ever even think about inputting 1001 characters, then this might be okay. Of course, you can't really assume that other people won't be tempted to try....)
So the "proper" way is to use numeric_limits<streamsize>::max(), which tells the function to discard as many characters as possible until a newline character is reached. (You'll need to #include <limits> to use it, though)

You can read more about it here:
http://www.cplusplus.com/reference/istream/istream/ignore/
closed account (S6k9GNh0)
I sometimes use a small utility function I made for input grab that's type safe:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
template <typename T>
bool get_input(T& output, char delimiter = '\n')
{
	string input;
	getline(cin, input, delimiter);

	// This code converts from string to T in a safe manner
	stringstream myStream(input);

	if (myStream >> output) //This will break if T is something bogus.
		return true;

	cout << "Failed to convert " << input << " to type " << typeid(T).name() << endl;
        return false;
}
Last edited on
Whoops, didn't get here in time. Got ninja'd by long double main ;)
Thank you very much long double main and xismn. NOw I understand. Thank you! :)
Topic archived. No new replies allowed.