Answer to Question #193420 in C++ for Ruhi

Question #193420

Design a program that uses try-catch mechanism to check whether the integer input entered is correct type or not.


1
Expert's answer
2021-05-14T16:04:45-0400
/**
 * 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;
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog