Question

In: Computer Science

Update your program from Homework 4, number 2 (Hat, Jacket, and Waist size) to allow the...

Update your program from Homework 4, number 2 (Hat, Jacket, and Waist size) to allow the user to repeat the calculation as many times as they wish. Include error handling - the program should throw an error if the user enters invalid input (ie. a negative height). The program must use try, throw and catch and must use the runtime_error kind of how I used it but it still has to be debugged.

#include <iostream>

#include <exception>

using namespace std;

double hat(double,double);

double jacket(double,double,int);

double waist(double,double,int);

int main ()

{

  

double height = 0.0, weight = 0.0;

int age = 0;

char answer;

  

do

{

try{

if (height <= 0){

throw runtime_error ("Error: Invalid input of height ");

}

cout << "Enter your height in inches: ";

cin >> height;

}catch (const runtime_error& e){

cerr << e.what ();

}

try{

if (weight <= 0){

throw runtime_error ("Error: Invalid input of pounds ");

}

cout << "Enter your weight in pounds: ";

cin >> weight;

}catch (const runtime_error& e){

cerr << e.what ();

  

}

try{

if (age <= 0){

throw runtime_error ("Error: Invalid input of age ");

}

cout << "Enter your age: ";

cin >> age;

}catch (const runtime_error& e){

cerr << e.what ();

}

cout << "\nYour Hat size is: " << hat(weight ,height);

cout << "\nYour Jacket size is: "<< jacket( height, weight, age);

cout << "\nYour Waist size is: "<< waist( height, weight, age);

cout << "\nWould you like to continue (y/n)? ";

cin>>answer;

}

while((answer) == 'y');

return 0;

}

double hat(double weight ,double height)

{

return ((weight/height) * 2.9);

}

double jacket(double height,double weight,int age)

{

double size;

int j;

if (age>=30)

{

if((age % 10) !=0)

age = age-(age%10);

j= (age-30)/10;

size =((height * weight) / 288)+((1.0/8)*j);

}

else

size =((height * weight) / 288);

return size;

}

double waist(double height,double weight,int age)

{

double size2;

int w;

  

if(age >= 28)

{

if((age % 2) !=0)

age = age-(age%2);

w = (age-28)/2;

size2 = (weight/(5.7))+( (1.0/10)*w);

}

else

size2 = weight / (5.7);

return size2;

}

Solutions

Expert Solution

In case of any query do comment. Thanks

I modified the code as per your requirement. Actually you need to first take input from the user and then validation for incorrect values. and then break the loop if you have any error.

Code:

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

double hat(double,double);
double jacket(double,double,int);
double waist(double,double,int);

int main ()
{
    double height = 0.0, weight = 0.0;
    int age = 0;
    char answer;
    do
    {
    try
    {
        //take user input height and then check height for incorrect value
        cout << "Enter your height in inches: ";
        cin >> height;
        if (height <= 0){
            throw runtime_error ("Error: Invalid input of height ");
        }
    
   
    }catch (const runtime_error& e){
        cerr << e.what ();
        break; //break if error thorwn
    }
    
    try
    {
        //take user input weight and then check weight for incorrect value
        cout << "Enter your weight in pounds: ";
        cin >> weight;
        if (weight <= 0){
            throw runtime_error ("Error: Invalid input of pounds ");
        }
        
   
    }catch (const runtime_error& e){
        cerr << e.what ();
        break; //break if error thorwn
    }
    
    try
    {
        //take user input age and then check age for incorrect value
        cout << "Enter your age: ";
        cin >> age;
        if (age <= 0){
            throw runtime_error ("Error: Invalid input of age ");
        }
    }catch (const runtime_error& e){
        cerr << e.what ();
        break; //break if error thorwn
    }
    
    cout << "\nYour Hat size is: " << hat(weight ,height);
    cout << "\nYour Jacket size is: "<< jacket( height, weight, age);
    cout << "\nYour Waist size is: "<< waist( height, weight, age);
    cout << "\nWould you like to continue (y/n)? ";
    cin>>answer;
    
    }
    
    while((answer) == 'y');
    
    return 0;

}

double hat(double weight ,double height)
{
    return ((weight/height) * 2.9);
}

double jacket(double height,double weight,int age)
{
    double size;
    int j;
    
    if (age>=30)
    {
        if((age % 10) !=0)
            age = age-(age%10);
        j= (age-30)/10;
        size =((height * weight) / 288)+((1.0/8)*j);
    }
    else
        size =((height * weight) / 288);
    return size;

}

double waist(double height,double weight,int age)
{

    double size2;
    int w;
    if(age >= 28)
    {
        if((age % 2) !=0)
            age = age-(age%2);
        w = (age-28)/2;
        size2 = (weight/(5.7))+( (1.0/10)*w);
    }
    else
        size2 = weight / (5.7);
    
    return size2;

}

=================screen shot of code==============

output:


Related Solutions

A hat contains a number of cubes: 3 red, 2 white, 1 blue, and 4 black....
A hat contains a number of cubes: 3 red, 2 white, 1 blue, and 4 black. If one cube is chosen at random, what is the probability that it is: A red cube? (3 points) Not a red cube? (3 points) A cube that is white OR black? (4 points) A cube that is neither white nor black? (4 points) What do the answers to part a and part b add up to and why? (5 points) If three cubes...
Write a C++ program that : 1. Allow the user to enter the size of the...
Write a C++ program that : 1. Allow the user to enter the size of the matrix such as N. N must be an integer that is >= 2 and < 11. 2. Create an vector of size N x N. 3. Call a function to do the following : Populate the vector with N2 distinct random numbers. Display the created array. 4. Call a function to determines whether the numbers in n x n vector satisfy the perfect matrix...
Write a JAVA program that allow a user to enter 2 number, starting point and end...
Write a JAVA program that allow a user to enter 2 number, starting point and end point. For example a user me enter 1 and 100. Your program will Add numbers from 1 to 100, add all the even number, and add all the odd numbers Output: The sum of number 1 to 100 is: The sum of even number 1 to 100 is: The sum of odd number 1 to 100 is:
Write a program in C A teacher will assign homework and give the number of days...
Write a program in C A teacher will assign homework and give the number of days for the students to work on. The student is responsible for calculating the due date. The teacher does not collect homework on Friday or weekend. Write a C program that let the user enter today’s day of the week (0 for Sunday, 1 for Monday, etc.) and the number of days to allow the students to do the work, which may be several weeks....
Overview For this program, add code to program 2 that will (potentially) allow for a discount...
Overview For this program, add code to program 2 that will (potentially) allow for a discount to be applied to the cost of the tickets to the movie theater. Basic Program Logic The program should start the same as program 2 by asking the user for the number of tickets to purchase for adult and children. The program should then ask the user if they have a discount coupon. This is a string value. A value of "Y" indicates the...
Create design document for a program that will allow the user: Convert a number to binary....
Create design document for a program that will allow the user: Convert a number to binary. Convert from binary to number. Display the hexadecimal representation of a binary number. Given an hexadecimal display its binary representation. Convert a number to IEEE single precision and vice versa. More to come. PLEASE ADD PSEUDOCODE AND USE C PROGRAMMING USE FUNCTIONS IF POSSIBLE
2. From a sample of size 250, number of females is 173. What is the point...
2. From a sample of size 250, number of females is 173. What is the point estimate of population proportion? Provide an answer with 3 decimal points. 3. Which one is the point estimate of population mean? a. sample mean b. sample proportion c. sample median d. sample maximum 4. A survey of 1,206 people asked: “What would you do with an unexpected tax refund?” Forty-seven percent responded that they would pay off debts (Vanity Fair, June 2010). if the...
Update the code from the questions if necessary. #include #include /* Program sorts an array of...
Update the code from the questions if necessary. #include #include /* Program sorts an array of integers using a selection sort. The general algorithm repeatedly finds the smallest number in the array and places it at the front of the list. */ using namespace std; int find_small_index (int start_index, int numbers []); void swap_values (int index1, int index2, int numbers []); int main(int argc, char *argv[]) {     // array of numbers     int numbers [10] = {7, 9, 21,...
Develop a rudimentary Java program that will allow anyone to enter any number of grades and...
Develop a rudimentary Java program that will allow anyone to enter any number of grades and then calculate grade point average. For reference, a grade point average is a weighted average of a set of grades based on the relative number of credits. For example, a 1 credit “A” (4.0) should count twice as much as a .5 credit “C” (3.0), and a 1.5 credit “A+”(4.33) should count three times as much as the “C” and 1.5 times as much...
C++: Write a program to convert from gallons to liters. The program should allow the user...
C++: Write a program to convert from gallons to liters. The program should allow the user to input the amount with decimal places and then see the result on the screen.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT