how to loop exceptions

in main....

cout<<"enter an int"
cin>>a

try
checking(A);
catch (char *eString)
{
cout<<eString<<endl;
cout<<"please re enter your value MAKE SURE IT'S POSITIVE"<<endl;
cin>>a;
}
out of main...

float checking(float num)
{
if (num < 0||num==0)
throw"error - it can't be negative nor zero";
else
return (num);
}


now if u look carefully if i type negative number or zero i will get the errow thow message, but if i type -3 or 0 again it lets me, i need to loop it how to do it? (it's a requirement to use try/catch).
This isn't the best way to validate input. I would say try...catch is used to catch a failure when it occurs but not to prevent it. You could wrap your try...catch block with a loop but you better just replace it by a loop instead:

1
2
3
4
5
6
7
8
cout << "Please enter a positive integer: ";
cin >> a;

// btw, n < 0 || n == 0 can be replaced by n <= 0.
while (a <= 0) {
	cout << "Your integer isn't positive! Enter a positive one: ";
	cin >> a;
}
Topic archived. No new replies allowed.