Design a program that uses try-catch mechanism to check whether the integer input entered is correct type or not.
/**
* C++ program to request user to enter positive integer and uses try catch
*/
#include <iostream>
using namespace std;
int main()
{
int num;
cout<<"Enter a positive integer: ";
try
{
cin>> num;
if (num < 0 )
throw 1;
else
cout<<num <<" is a positive integer.";
}
catch (int err)
{
cout<<num<<" is not a positive integer ";
}
return 0;
}
Comments
Leave a comment