Sign in to follow this  

Limiting input type in C++

This topic is 4711 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

I want to limit the type of input the user makes in my short C++ program for my uni. Here is the code I have
int main()
{
	int radie = -1;

	while (radie != 0)
	{
		cout << "Ange cirkelns radie (0 avslutar): ";
		cin >> radie;
            cout << radie << endl;
	}

	return 0;
}

I want to be able to limit the input that is stored in radie to numbers only. If I input a char or a string, the program loops endlessly. Please help Obi-Wan Kenobi.. you are my only hope :-D

Share this post


Link to post
Share on other sites
There are a few things that could make it an infinite loop. One being a spare newline character in the input buffer (namely '\n'). To remove this you would do something of the form


std::cin.ignore(std::cin.rdbuf()->in_avail(), '\n');




It should be noted that the above does not work in all situations, but there is no completly portable way to flush an input streams buffer(s). Secondly you can get problems if the stream enters a failed state. This might happen if you enter a string instead of a number into your integer. Doing this would set the streams fail flags. When a stream is in a failed state, all subsequent reads from the stream will fail, causing an infinite loop in this case. You should check for this condition each loop iteration.


if(std::cin.fail())
{
std::cin.clear();
}




You could combine these together into a single function, or stream manipulator.


void inputstreamcheck()
{

if(std::cin.fail())
{
std::cin.clear();
}
std::cin.ignore(std::cin.rdbuf()->in_avail(), '\n');
}

int main()
{
int radie = -1;

while (radie != 0)
{
std::cout << "Ange cirkelns radie (0 avslutar): ";
std::cin >> radie;
inputstreamcheck();
std::cout << radie << std::endl;
}

return 0;
}



Share this post


Link to post
Share on other sites

#include <iostream>
#include <limits>
using namespace std;

int main()
{
int radie = -1;

while (radie != 0)
{
cout << "Ange cirkelns radie (0 avslutar): ";

cin >> radie;

if(cin.fail())
{
// Clear the fail flag
cin.clear();

// Discard the rest of the line
cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
}
else
{
cout << radie << endl;
}
}

return 0;
}




edit: cin.ignore() accepts std::numeric_limits<int>::max() std::numeric_limits<std::streamsize>::max() as a magic number to mean "any number". And don't forget to cin.clear() the fail flag.

[Edited by - Fruny on January 18, 2005 2:14:12 PM]

Share this post


Link to post
Share on other sites

This topic is 4711 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.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this