Hi there! I am having issues with the program. When I enter numbers it gives the
ID: 3649984 • Letter: H
Question
Hi there! I am having issues with the program. When I enter numbers it gives the correct output; however when you type in a character such as a, it just goes crazy! Here is my code. Any assistance is greatly appreciated!#include "std_lib_facilities_3.h"
class BadArea{};
int area(int length, int width)
{
if(length <= 0 || width <=0) throw BadArea();//catch bad area
double result = length * width;
if(result<0||(result/length)!=width||(result/width)!=length) throw BadArea();//incase bad area
return result;
}
double mysqrt(double x)
{
if (x==0)
{
return 0;
}
if (x==1)
{
return 1;
}
else
{
double result = ((x*x*x*x) +(28*x*x*x)+(70*x*x)+(28*x)+1)/((8*(1+x))*(1+(6*x)+(x*x))); //using newton equation
return result;
}
}
int main()//initialize this
{
int infloop = 0;
while(infloop != 1) // since baby is 0 and not 1 this will create the infinite loop
{
try{
cout<<"Please enter the length ";
int length;
cin>>length;
cout<<"Please enter the width ";
int width;
cin>>width;
if(!cin)error("Couldn't read ");
int Area = area(length, width);
cout <<"The area is"<< Area << ' ';
cout <<"The mysqrt of area is "<< mysqrt(Area) << ' ';
return 0;
}
catch(BadArea)
{
cerr << "Exception: bad area ";
main();
}
catch(exception& e)
{
cerr << "Exception: " << e.what() << ' ';
}
catch(...)
{
cerr << "Exception occurred ";
}
}
}
Explanation / Answer
please rate - thanks
I had to use my header,since I don't have yours
I also changed an area from int to double, it was getting a compilation error
#include <iostream>
using namespace std;
class BadArea{};
double area(int length, int width)
{
if(length <= 0 || width <=0) throw BadArea();//catch bad area
double result = length * width;
if(result<0||(result/length)!=width||(result/width)!=length) throw BadArea();//incase bad area
return result;
}
double mysqrt(double x)
{
if (x==0)
{
return 0;
}
if (x==1)
{
return 1;
}
else
{
double result = ((x*x*x*x) +(28*x*x*x)+(70*x*x)+(28*x)+1)/((8*(1+x))*(1+(6*x)+(x*x))); //using newton equation
return result;
}
}
int main()//initialize this
{int length,width;
int infloop = 0;
while(infloop != 1) // since baby is 0 and not 1 this will create the infinite loop
{
try{
cout<<"Please enter the length ";
cin>>length;
while(cin.fail())
{cout<<"invalid input ";
cout<<"Please enter the length ";
cin.clear();
cin.ignore(80,' ');
cin>>length;
}
cout<<"Please enter the width ";
cin>>width;
while(cin.fail())
{cout<<"invalid input ";
cout<<"Please enter the width ";
cin.clear();
cin.ignore(80,' ');
cin>>width;
}
//if(!cin)error("Couldn't read ");
double Area = area(length, width);
cout <<"The area is"<< Area << ' ';
cout <<"The mysqrt of area is "<< mysqrt(Area) << ' ';
return 0;
}
catch(BadArea)
{
cerr << "Exception: bad area ";
main();
}
catch(exception& e)
{
cerr << "Exception: " << e.what() << ' ';
}
catch(...)
{
cerr << "Exception occurred ";
}
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.