User-defined decimals (0.1-0.9)

How can the user input a decimal between 0.1 and 0.9 and not get an infinitely large answer? The c value must be between 0.1-0.9, but when such values are entered, I get a very unreasonably large answer in the second equation where the c value is used. This is a system of equations program where two variables (V3 and V4) are solved for.

#include <iostream>
#include <iomanip>
#include <cmath>
#include <fstream>
#include <iomanip>
#include <float.h>

using namespace std;

int main()
{
int m1, v1, m2, v2, D1, D2, c;
cout << "Enter the values for the first equation." << endl;
cout << "Enter the value for m1" << endl;
cin >> m1;
cout << "Enter the value for v1" << endl;
cin >> v1;
cout << "Enter the value for m2" << endl;
cin >> m2;
cout << "Enter the value for v2" << endl;
cin >> v2;
cout << "Enter the value for D1" << endl;
cin >> D1;
cout << "Enter the value for D2" << endl;
cin >> D2;
cout << "Enter the coefficient of restitution." << endl;
cin >> c;
cout << m1*v1*(D1) << "v3+" << m2*v2*(D2) << "v4=" << (m1*v1*(D1))+(m2*v2*(D2)) << endl;
cout << 1 << "v3+" << 1 << "v4=" << c*(v1-v2) << endl;

system("pause");

return 0;
}
Last edited on
Hi,

Try hanging the type of your variables to double

Do you realise what the difference between int and double is?

http://www.cplusplus.com/doc/tutorial/variables/

Please always use code tags:

http://www.cplusplus.com/articles/z13hAqkS/
Thank you. I used double m1, v1, m2... instead of int, and it worked.
Topic archived. No new replies allowed.