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 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....
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
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,...
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...
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...
create a program that will allow the user to enter a start value from 1 to...
create a program that will allow the user to enter a start value from 1 to 5, a stop value from 10 to 12 and a multiplier from 1 to 4. the program must display a multiplication table from the values entered. for example if the user enters: start 2, stop 10 and multiplier 3, the table should appear as follows: 3*2=6 3*3=9 3*4=12 . . . 3*10=30
Write a program that prompts the user for an even number from 2 to 100 until...
Write a program that prompts the user for an even number from 2 to 100 until the number 90 is encountered. Not including the 90, calculate the minimum value. In case you know what this means: DO NOT USE LISTS! We will look into the use of lists later. This has to be done in the python program. Here's what I have so far: inp = 0 min = 0 while inp != 90:     inp = int(input("Please enter an even...
2. Write a c++ program that takes from the user the ​number of courses​ and constructs...
2. Write a c++ program that takes from the user the ​number of courses​ and constructs 3 ​dynamic 1D arrays​ with size courses+1. Each array represents a student. Each cell in the array represents a student’s mark in a course. In the last cell of each 1D array you should calculate the average mark of that student. Then output the average mark of all students in each course. Delete any allocated memory. Example Number of courses : 4 50 60...
Java Programming: Can I get an example of a program that will allow 4 options? Example...
Java Programming: Can I get an example of a program that will allow 4 options? Example Output: Choose on of the options below: 1. Display all items in CSV file. (CSV file includes for example brand of phone, size, price) 2. Pick an item by linear searching 3. Pick an item by bineary searching 4. Quit Program
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT