Problem Solving with C++ (10th Edition)
10th Edition
ISBN: 9780134521176
Author: SAVITCH
Publisher: PEARSON
expand_more
expand_more
format_list_bulleted
Concept explainers
Question
Chapter 16, Problem 3PP
Program Plan Intro
- Include required library files.
- Define a class named “Out_Range” and “Non_Digits”.
- Declare and define a function named “isAll_Digits()”.
- Declare the required variables.
- “for” loop to check the string contains only digits or not then return the Boolean result
- Initialize the constant value for maximum value.
- Define a “main()” function to test the histogram.
- “while” loop to handle the exception for the user giving inputs.
- Get the user input to print the result.
- Declare the required variables.
- “for” loop to check the string contains only digits or not then return the Boolean result
- Define a “main()” function to test the histogram.
- “while” loop to handle the exception for the user giving inputs.
- Get the user input to print the result.
Expert Solution & Answer
Want to see the full answer?
Check out a sample textbook solutionStudents have asked these similar questions
please code in python
Write a function called weighted_avg that takes a list of grades and a corresponding list of weights and returns the weighted average of the grades rounded to 1 decimal place.
Your function should raise an exception with the message exactly as shown:
a weight is less than 0 or greater than 100 (message: "A weight is less than 0 or greater than 100")
the weights do not add to 100 (message: "Weights do not add to 100")
the number of weights and grades are not equal (message: "The number of weight and grades are not equal")
a grade is below 0 (grades above 100 would be considered extra credit and are acceptable) (message: "A grade is below 0")
Do not print the exception just 'raise' it.
Run your function on grades1 with weights1 and grades2 with weights2 and grades3 with weights3 and grades4 with weights4, defined below. Catch the errors generated in each case as an exception with the useful message for the user as defined above.
Hint: The first 3 test cases should…
C++Write a program SEGMENT that does the following:
Asks the user for his/her birthday using 2 integer values, i.e., int month, int year.
Using a try/catch block with two catch blocks, check for the following exceptions:
Invalid month (i.e., a month less than or equal to 0 or greater than 12). Throw a string exception to be caught by a catch block accepting a string. Output the message thrown by the exception indicating the month is an invalid month.
Year less than 1900 or greater than 2020. Throw an integer exception to be caught by a catch block accepting an integer. In the catch block, if the integer thrown is less than 1900, output the message “You are very old”. If the integer exception is greater than 2020, output the message “You have not been born yet!”
If neither exception occurs, simply output (cout) a message with the user’s birth month and year, for example, “You were born in “ << month << “ and “ << year.
Declare any variables needed.…
C++
You did this way back in Unit 1! But now all of your RomanNumber code is wrapped up in a class, so the "main" program is short and sweet. As before, write a program that accepts entries from the user.
If it's an integer, convert to Roman Number and display.
If it's a Roman Number, convert to integer and display.
If it's neither, thrown an exception and continue to process
The action to take here will be to display an error message
If the entry is 0 or O (The digit 0 (zero) or the letter O) exit and state how many conversions were done of each type (integer to Roman and Roman to integer) and how many exceptions were thrown.
As I said, the main program will be short and sweet.
Provide your .cpp code and a screen shot of your program in action. Enter twelve conversions - four integer to Roman, four Roman to integer, and two of each in which the input is invalid so that I can see the exceptions. The thirteenth and final entry will be 0 or O.
Chapter 16 Solutions
Problem Solving with C++ (10th Edition)
Ch. 16.1 - Prob. 1STECh. 16.1 - What would be the output produced by the code in...Ch. 16.1 - Prob. 3STECh. 16.1 - What happens when a throw statement is executed?...Ch. 16.1 - In the code given in Self-Test Exercise 1, what is...Ch. 16.1 - Prob. 6STECh. 16.1 - Prob. 7STECh. 16.1 - What is the output produced by the following...Ch. 16.1 - What is the output produced by the program in...Ch. 16.2 - Prob. 10STE
Knowledge Booster
Learn more about
Need a deep-dive on the concept behind this application? Look no further. Learn more about this topic, computer-science and related others by exploring similar questions and additional content below.Similar questions
- Write a C++ program that converts 12-hour time (including AM/PM) to 24 hour time. You must define an exception class called InvalidTime wherein if the user enters an out of range time or gibberish characters, your program will throw and catch InvalidTime. Make your class support the following code: TwelveHourTime sevenAM( "7:00 AM" );cout << sevenAM.twentyFourTime( ) << endl; // prints "07:00"TwelveHourTime eightPM( "8:30 PM" );cout << eightPM.twentyFourTime( ) << endl; // prints "20:30"try{ TwelveHourTime bad( "asef32422" ); // the previous line should throw an InvalidTime exception} catch( InvalidTime ) { cout << "caught error as expected" << endl;}arrow_forwardThis lab will exercise your understanding of some of the concepts covered in Chapter 14: exception handling 1. 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 valuesmust be positive integers. b. Calculate and output the equivalent measurement in centimeters1. 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 The program may be named any name of our choice, must have a .cpp…arrow_forwardThe 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 integer. At FIXME in the code, add try and except blocks to catch the ValueError exception and output 0 for the age. Ex: If the input is: Lee 18 Lua 21 Mary Beth 19 Stu 33 -1 then the output is: Lee 19 Lua 22 Mary 0 Stu 34 code name = ()age = ()parts = input().split()name = parts[0]while name != '-1':# FIXME: The following line will throw ValueError exception.# Insert try/except blocks to catch the exception.age = int(parts[1]) + 1print('{} {}'.format(name, age))# Get next lineparts = input().split()name = parts[0]arrow_forward
- Write a program that reads integers user_num and div_num as input, and output the quotient (user_num divided by div_num). Use a try block to perform all the statements. Use an except block to catch any ZeroDivisionError as a variable and output "Zero Division Exception: " followed by the exception message from the variable. Use another except block to catch any ValueError caused by invalid input as a variable and output "Input Exception: " followed by the exception message from the variable. Note: ZeroDivisionError is raised when a division by zero happens. ValueError is raised when a user enters a value of different data type than what is defined in the program. Do not include code to raise any exception in the program. (in Python)arrow_forwardWrite a program that reads integers user_num and div_num as input, and output the quotient (user_num divided by div_num). Use a try block to perform all the statements. Use an except block to catch any ZeroDivisionError as a variable and output "Zero Division Exception: " followed by the exception message from the variable. Use another except block to catch any ValueError caused by invalid input as a variable and output "Input Exception: " followed by the exception message from the variable. Note: ZeroDivisionError is raised when a division by zero happens. ValueError is raised when a user enters a value of different data type than what is defined in the program. Do not include code to raise any exception in the program. Ex: If the input of the program is: 15 3 the output of the program is: 5 Ex: If the input of the program is: 10 0 the output of the program is: Zero Division Exception: integer division or modulo by zero Ex: If the input of the program is: 15.5 5 the output of…arrow_forwardThis code is in Python As you know, when you attempt to get a number from the user by usingsomething like:number = int(input(“Enter a number”) ornumber = float(input(“Enter a number”),if the user includes commas or a dollar sign in the number, the conversion will failwith an exception.Write a program that will include and use a function called getNumber( prompt )that will do the following:1) Use the standard input function to display the prompt and get the user’sinput.2) Use the string functions to remove any commas or dollar sign from theinput.3) Check for the presence of a decimal point.4) If a decimal point is present, use the float conversion function to convertthe modified input to float; otherwise, use the int conversion function toconvert to an int5) Return the number to the calling code.arrow_forward
- Write a program using c++ oop that prompts the user to enter a length in feet and inches and outputs the equivalent length in centimeters. If the user enters a negative number, throw and handle an appropriate exception and prompt the user to enter another set of numbers.arrow_forward2 in java!! Write a program that takes a singular form of a noun and produces the plural form. For most nouns, the plural form has “s” added to the end, but allow for the following exceptions:• If the word’s last letter is 's’, 'h’, 'x’, or 'z’, add “es” to the end of the noun, example date/dates.• If the word’s last letter is 'y’, replace the ‘y’ with “ies” at the end of the noun, example baby / babiesarrow_forwardThe problem comes from Introduction to C++ Programming and Data Structures, 4th Edition (HexFormatException)Implement the hex2Dec function in Programming Exercise 16.1 to throw a HexFormatException if the string is not a hex string .Define a custom exception class named HexFormatException. Write a test program that prompts the user to enter a hex number as a string and displays the number in decimal. If the function throws an exception, display "Not a hex number".arrow_forward
- Mr. Ahmed wants to check whether the Employee ID and GSM number entered by the Employee to register for participating in a football competition is valid or not. Write a Java program to read the Employee ID and GSM Number of an Employee. If the Employee ID does not start with the characters ST or if the Mobile Number does not contain exactly 8 digits, throw a user-defined exception InvalidDetailsException. If the details entered are valid, display the message “correct details are entered!!!” otherwise display “Entered details are invalid!!” Sample Test case: Enter Employee ID: ST1500 Enter GSM Number: 999656478 Entered details are invalid!!”arrow_forwardWrite a program that prompts the user to enter a length in feet and then enter a length in inches and outputs the equivalent length in centimeters. If the user enters a negative number or a non-digit number, throw and handle an appropriate exception and prompt the user to enter another set of numbers. Your error message should read A non positive number is entered and allow the user to try again. Format your output with setprecision(2) to ensure the proper number of decimals for testing!arrow_forwardThe problem comes from Introduction to C++ Programming and Data Structures, 4th Edition by Daniel Liang. (HexFormatException)Implement the hex2Dec function in Programming Exercise 16.1 to throw a HexFormatException if the string is not a hex string .Define a custom exception class named HexFormatException. Write a test program that prompts the user to enter a hex number as a string and displays the number in decimal. If the function throws an exception, display "Not a hex number".arrow_forward
arrow_back_ios
SEE MORE QUESTIONS
arrow_forward_ios
Recommended textbooks for you
- C++ Programming: From Problem Analysis to Program...Computer ScienceISBN:9781337102087Author:D. S. MalikPublisher:Cengage Learning
C++ Programming: From Problem Analysis to Program...
Computer Science
ISBN:9781337102087
Author:D. S. Malik
Publisher:Cengage Learning