Answer to Question #242318 in C++ for Astor

Question #242318

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

 


1
Expert's answer
2021-09-26T02:42:45-0400
#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;
}

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
APPROVED BY CLIENTS