Question

In: Computer Science

write in c++ Create a program that uses EXCEPTION HANDLING to deal with an invalid input...

write in c++
 Create a program that uses EXCEPTION HANDLING to deal with an invalid input entry
    by a user.
    
    a.  Write a program that prompts a user to enter a length in feet and inches.  The length values
        must be positive integers.
    
    b.  Calculate and output the equivalent measurement in centimeters
    
        1 inch = 2.54 centimeters
        
    c.  Write the code to handle the following exceptions:

        If the user enters a negative number, throw and catch an error that gives the user the appropriate
        message and request a valid number.  Do this until the user enters a valid number

        If the user enters a character, throw and catch an error that indicates the input stream
        is invalid.  Write code that resets the input stream and request a valid number.  Do this
        until the user enters a valid number
        
    d.  Output the feet-inch input values to centimeter conversion value

Solutions

Expert Solution

Here is code:

#include <iostream>

#include <limits>

#define CENTIMETER 2.54

using namespace std;

int getInches(int low)

{

int num;

// check if input is valid or not

if (!(cin >> num) || (num <= low))

{

// flush the cin data

cin.clear();

cin.ignore(numeric_limits<streamsize>::max(), '\n');

throw "The number is out of bounds: Try again : ";

}

return num;

}

int main()

{

int inches = 0;

cout << "Enter a length in feet and inches : ";

while (1)

{

try

{

inches = getInches(0);

cout << "Feet-inch input values to centimeter conversion value : " << (inches * CENTIMETER) << endl;

break;

}

catch (const char *str)

{

cout << str;

}

}

// Number validation

return 0;

}

Output:


Related Solutions

EXCEPTION HANDLING Concept Summary: 1. Exception handling 2. Class design Description Write a program that creates...
EXCEPTION HANDLING Concept Summary: 1. Exception handling 2. Class design Description Write a program that creates an exception class called ManyCharactersException, designed to be thrown when a string is discovered that has too many characters in it. Consider a program that takes in the last names of users. The driver class of the program reads the names (strings) from the user until the user enters "DONE". If a name is entered that has more than 20 characters, throw the exception....
EXCEPTION HANDLING Concept Summary: 1. Exception   handling   2. Class   design Description Write   a   program   that   creates  ...
EXCEPTION HANDLING Concept Summary: 1. Exception   handling   2. Class   design Description Write   a   program   that   creates   an   exception   class   called   ManyCharactersException,   designed   to be thrown when a   string is discovered that has too many characters in it. Consider a   program that   takes   in the last   names   of   users.   The   driver   class   of   the   program reads the   names   (strings) from   the   user   until   the   user   enters   "DONE".   If   a   name is   entered   that   has   more   than   20   characters,   throw   the   exception.  ...
C++ Zybook Lab 16.5 LAB: Exception handling to detect input string vs. int The given program...
C++ Zybook Lab 16.5 LAB: Exception handling to detect input string vs. int The given program reads a list of single-word first names and ages (ending with -1), and outputs that list with the age incremented. The program fails and throws an exception if the second input on a line is a string rather than an int. At FIXME in the code, add a try/catch statement to catch ios_base::failure, and output 0 for the age. Ex: If the input is:...
Write a C Program that uses file handling operations of C language. The Program should perform...
Write a C Program that uses file handling operations of C language. The Program should perform following operations: 1. The program should accept student names and students’ assignment marks from the user. 2. Values accepted from the user should get saved in a .csv file (.csv files are “comma separated value” files, that can be opened with spreadsheet applications like MS-Excel and also with a normal text editor like Notepad). You should be able to open and view this file...
in C#, What is an exception and what are the advantages of exception handling?
in C#, What is an exception and what are the advantages of exception handling? What are the differences between the traditional error-handling methods and the object-oriented exception-handling methods and when should you use each one? Provide three to four paragraphs detailing your findings/views on these questions..provide examples with a simple code also..
write a python program, please. Create a program that uses the Gettysburg Address as input and...
write a python program, please. Create a program that uses the Gettysburg Address as input and outputs a list of tuples for every two words. For example: [('Four', 'score'),('and', 'seven'), ...].
In visual Studio C++ Create a program that uses a for loop to input the high...
In visual Studio C++ Create a program that uses a for loop to input the high temperature, and low temperature for each day of the week. The high and low will be placed into two elements of the array. For each loop the high and low will be placed into the next set of elements of the array. After the temps for all seven days have been entered into the array, a for loop will be used to pull out...
Write a program in C, that uses standard input and output to ask the user to...
Write a program in C, that uses standard input and output to ask the user to enter a sentence of up to 50 characters, the ask the user for a number between 1 & 10. Count the number of characters in the sentence and multiple the number of characters by the input number and print out the answer. Code so far: char sentence[50]; int count = 0; int c; printf("\nEnter a sentence: "); fgets(sentence, 50, stdin); sscanf(sentence, %s;    for(c=0;...
*In Java please! EXCEPTION HANDLING Concept Summary: 1. Exception   handling   2. Class   design Description Write   a  ...
*In Java please! EXCEPTION HANDLING Concept Summary: 1. Exception   handling   2. Class   design Description Write   a   program   that   creates   an   exception   class   called   ManyCharactersException,   designed   to   be   thrown   when   a   string   is   discovered   that   has   too   many   characters   in   it. Consider   a   program   that   takes   in   the   last   names   of   users.   The   driver   class   of   the   program reads the   names   (strings) from   the   user   until   the   user   enters   "DONE".   If   a   name is   entered   that   has   more   than   20   characters,  ...
(in C# please.) EXCEPTION HANDLING Concept Summary: 1. Use of try… catch in a program 2....
(in C# please.) EXCEPTION HANDLING Concept Summary: 1. Use of try… catch in a program 2. Define an exception class and call it from the driver program. For this lab you will complete two parts. Part (a) of the lab implements try… catch and uses an existing exception in C# or in Java. Write a program that implements an ArrayIndexOutOfBounds error. Part (b) writes a program that converts a time from 24-hour notation to 12-hour notation. Assume the user will...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT