Hi guys!
Making a simple math program calculating the quadratic formula so that I can get practice using multiple methods and basic in/out prompts. I wanted to add a simple check to make sure that what is being inputed for "a", "b", or "c" isn't a character that cannot be used in a calculation. Not sure about how to work the exception for it. Any advice? This is what I have so far..
[code] #include <iostream>
#include <math.h>
#include <stdio.h>
#include <cmath>
#include <string>
using namespace std;
//Discriminate Method :: Formula (b^2 - 4ac)
float discriminate(float a, float b, float c)
{
float discrim;
try
{
discrim = (b*b) - (4 * a * c);
}
catch(exception& e)
{
cout << "error" << endl;
}
return discrim;
}
float quadratic(float a, float b, float c)
{
float discrim = discriminate(a,b,c);
if (discrim < 0)
{
cout << "Discriminate is imaginary" << endl;
return 0;
}
else
{
float quad = sqrt(discrim);
return quad;
}
} //End of Quadratic Method
int main()
{
//Variable Declaration
float a, b, c;
//Welcome Message
cout << "Welcome to Josh's Quadratic Formula C++ Program" << endl;
cout << "================================================" << endl;
cout << "" << endl;
cout << "Input A: " << endl;
cin >> a;
cout << "Input B: " << endl;
cin >> b;
cout << "Input C: " << endl;
cin >> c;
float answer = quadratic(a,b,c);
if (answer == 0)
{
cout << "Imaginary Number: Cannot Calculate" << endl;
}
else
{
cout << "The answer: +/- " << answer << endl;
}
return 0;
}
[code]