In: Computer Science
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;
}
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: