Write a C++ program to handle the Input Mismatch Exception.
In the Main method, ask the user for the number of shirts they want to order. Implement try-catch to handle values other than an integer.
Input and Output format:
Refer to sample input and output
[All text in bold corresponds to input and rest corresponds to output]
Sample Input and Output 1:
Enter the number of t-shirts you want to order:
3
Your order for: 3 t-shirts has been successfully placed
Sample Input and Output 2:
Enter the number of t-shirts you want to order:
e
Please enter an Integer only.
Enter the number of t-shirts you want to order:
5
Your order for: 5 t-shirts has been successfully placed
#include <iostream>
#include <string>
using namespace std;
int main()
{
int number;
cout << "Enter the number of t-shirts you want to order:\n" ;
cin >> number;
try
{
if (number == int(number) && number!=0)
{
cout<< "Your order for: " + to_string (number)+ " t-shirts has been successfully placed\n";
}
else
{
cout<< "Please enter an Integer only." <<endl;
throw (number );
}
}
catch(int number )
{
}
return 0;
}
Comments
Leave a comment