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....
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 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...
Write a C++ Program Write a program that prompts the user to input a string. The...
Write a C++ Program Write a program that prompts the user to input a string. The program then uses the function substr to remove all the vowels from the string. For example, if str=”There”, then after removing all the vowels, str=”Thr”. After removing all the vowels, output the string. Your program must contain a function to remove all the vowels and a function to determine whether a character is a vowel. You must insert the following comments at the beginning...
Write a C program that contains a structure that uses predefined types and union. • Create...
Write a C program that contains a structure that uses predefined types and union. • Create a struct with name, age, kind (Either child, college student, or adult), and kindOfPerson (Either kid, student, or adult) • kids have a school field. Students have college and gpa. Adults have company and salary. • Create one non-dynamic struct with the content for a college student: "Bob", 20, K-State, 3.5 • Create one struct dynamically for a kid with: "Alison", 10, "Amanda Arnold...
Create a java program that: - Has a class that defines an exception -Have that exception...
Create a java program that: - Has a class that defines an exception -Have that exception throw(n) in one method, and be caught and handled in another one. -Has a program that always continues even if incorrect data is entered by the user -has a minimum of 2 classes in it
Write a program to create a tree randomly. You can use C++ programming language. The input...
Write a program to create a tree randomly. You can use C++ programming language. The input is the number of vertices in the tree, and the output is an adjacent list of the tree. (Managed to complete this assignment with a binary tree. But, was told I needed a general tree instead)
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT