DevX Home Today's Headlines   Articles Archive   Tip Bank   Forums

## float vs double

hey i am writing this program to do some simple calculations but i declare
the variables as double...but if the inputs are not an integer and dont =
an output that is an integer it crashes...how can i change it so that it
is float...this is the code...

#include <iostream.h>
#include <iomanip.h>

int main()
{
double a11,a12,a21,a22,x1,x2,c1,c2,numerator1,numerator2,denominator;

cout << "please enter the value of a11 ";
cin >> a11;
cout << "please enter the value of a12 ";
cin >> a12;
cout << "please enter the value of a21 ";
cin >> a21;
cout << "please enter the value of c1 ";
cin >> c1;
cout << "please enter the value of c2 ";
cin >> c2;

numerator1 = (c1 * a22) - (c2 * a12);
numerator2 = (c2 * a11) - (c1 * a21);
denominator = (a11 * a22) - (a12 * a21);
x1 = (numerator1 / denominator);
x2 = (numerator2 / denominator);

cout << "the answer for x1 = " << x1 <<endl;
cout << "the answer for x2 = " << x2 <<endl;

return 0;
}  Reply With Quote

2. Steve F Guest

## Re: float vs double

I think you forgot to set a22, as in:

cout << "please enter the value of a22 ";
cin >> a22;

Steve

>
>hey i am writing this program to do some simple calculations but i declare
>the variables as double...but if the inputs are not an integer and dont

=
>an output that is an integer it crashes...how can i change it so that it
>is float...this is the code...
>
>#include <iostream.h>
>#include <iomanip.h>
>
>int main()
>{
> double a11,a12,a21,a22,x1,x2,c1,c2,numerator1,numerator2,denominator;
>
> cout << "please enter the value of a11 ";
> cin >> a11;
> cout << "please enter the value of a12 ";
> cin >> a12;
> cout << "please enter the value of a21 ";
> cin >> a21;
> cout << "please enter the value of c1 ";
> cin >> c1;
> cout << "please enter the value of c2 ";
> cin >> c2;
>
> numerator1 = (c1 * a22) - (c2 * a12);
> numerator2 = (c2 * a11) - (c1 * a21);
> denominator = (a11 * a22) - (a12 * a21);
> x1 = (numerator1 / denominator);
> x2 = (numerator2 / denominator);
>
> cout << "the answer for x1 = " << x1 <<endl;
> cout << "the answer for x2 = " << x2 <<endl;
>
> return 0;
>}  Reply With Quote

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
• 