Question

In: Computer Science

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:

Lee 18
Lua 21
Mary Beth 19
Stu 33
-1

then the output is:

Lee 19
Lua 22
Mary 0
Stu 34

Here's the code:

#include <string>
#include <iostream>

using namespace std;

int main(int argc, char* argv[]) {
string inputName;
int age;
// Set exception mask for cin stream
cin.exceptions(ios::failbit);

cin >> inputName;
while(inputName != "-1") {
// FIXME: The following line will throw an ios_base::failure.
// Insert a try/catch statement to catch the exception.
// Clear cin's failbit to put cin in a useable state.
try{
//Reading age
cin >> age;
//Printing Name and age incremented by 1 if correct values are entered
cout << inputName << " " << (age + 1) << endl;
}
catch(ios_base::failure){
//Clear cin
cin.clear();
}
  
cin >> age;
cout << inputName << " " << (age + 1) << endl;

cin >> inputName;
}

return 0;
}

Solutions

Expert Solution

Please look at my code and in case of indentation issues check the screenshots.

---------------main.cpp----------------

#include <string>
#include <iostream>
using namespace std;

int main(int argc, char *argv[])
{
   string inputName;
   int age;
   // Set exception mask for cin stream
   cin.exceptions(ios::failbit);

   cin >> inputName;
   while (inputName != "-1")
   {
       // FIXME: The following line will throw an ios_base::failure.
       // Insert a try/catch statement to catch the exception.
       // Clear cin's failbit to put cin in a useable state.
       try
       {
          //Reading age
           cin >> age;
           //Printing Name and age incremented by 1 if correct values are entered
           cout << inputName << " " << (age + 1) << endl;
       }
       catch (ios_base::failure)
       {
          //Clear cin
          cout << inputName << " 0" << endl;       //on exception, print name and age as 0
           cin.clear();                           //clear the failbit
           cin.ignore(256,'\n');                   //ignore the current input until a new line '\n' is encountered
       }
      
       cin >> inputName;
   }
   return 0;
}

--------------Screenshots--------------------

------------------Output------------------

---------------------------------------------------------------------------------------------------------
Please give a thumbs up if you find this answer helpful.
If it doesn't help, please comment before giving a thumbs down.
Please Do comment if you need any clarification.
I will surely help you.


Related Solutions

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...
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..
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.  ...
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...
A C program that will perform the following: Input the string: Abc_3_deF Expected Output: The string...
A C program that will perform the following: Input the string: Abc_3_deF Expected Output: The string you entered is: aBC_Three_DEf An output for just this specific input will be fine. If you want to provide a program for all outputs, it would help with my understanding of how the program works overall as well but it is not needed. Thanks!
(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...
C++ Write a program that takes a string and integer as input, and outputs a sentence...
C++ Write a program that takes a string and integer as input, and outputs a sentence using those items as below. The program repeats until the input string is "quit". If the input is: apples 5 shoes 2 quit 0 the output is: Eating 5 apples a day keeps your doctor away. Eating 2 shoes a day keeps your doctor away.
Using C++ Create a program that asks the user to input a string value and then...
Using C++ Create a program that asks the user to input a string value and then outputs the string in the Pig Latin form. - If the string begins with a vowel, add the string "-way" at the end of the string. For “eye”, it will be “eye-way”. - If the string does not begin with a vowel, first add "-" at the end of the string. Then rotate the string one character at a time; that is, move the...
Write a basic C++ program with function, whose input is a character and a string, and...
Write a basic C++ program with function, whose input is a character and a string, and whose output indicates the number of times the character appears in the string. Ex: If the input is: n Monday the output is: 1 Ex: If the input is: z Today is Monday the output is: 0 Ex: If the input is: n It's a sunny day the output is: 2 Case matters. n is different than N. Ex: If the input is: n...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT