View Single Post
Old 12-06-2004, 05:17 PM   #8 (permalink)
Valmont
[code][/code] enforcer
 
Valmont's Avatar
 
Join Date: Mar 2003
Location: Netherlands
Posts: 1,544
Valmont is on a distinguished road
Code:
#include <iostream>
#include <string>
#include <climits>
#include <cmath>

using std::cin;
using std::cout;
using std::endl;
using std::numeric_limits;
using std::streamsize;
using std::string;

//Optional function: depends on IDE. Remove if not needed. void wait_for_enter();

//Additional helper. Remove if not needed. void reset_istream();

int main(int argc, char *argv[])
{
   cout<<"Please enter a, b, c seperated by spaces. Then press <enter>."<<endl;
   cout<<"a b c = ";
   double a, b, c;
   cin>>a>>b>>c;
   
   double det = b*b-4.0*a*c;
   double x1, x2;
   
   if(a == 0)
   {
      cout<<"Coefficient a is 0. This is not a quadratic equation!"<<endl;
   }
   else if (det < 0)
   {
      cout<<"This quadratic does not have real roots."<<endl;
   }
   else
   {
      x1 = (-b + sqrt(det) ) / (2.0*a);
      x2 = (-b - sqrt(det) ) / (2.0*a);
      
      cout<<"x1= "<<x1<<endl<<"x2= "<<x2<<endl;
   }

   reset_istream();
   wait_for_enter();
   return 0;
}

//--------------------------------------------------- void reset_istream()
{
   cin.clear();
   cin.ignore(numeric_limits<streamsize>::max(), '\n');
}

//--------------------------------------------------- void wait_for_enter()
{
  cout << "press <enter> to continue...";
  // Reset failstate, just in case.
  cin.clear();
  string line;
  getline( cin, line);
}
__________________
Valmont is offline   Reply With Quote