### Cin restriction and not rounding the results.

I have been trying to figure out how to limit the input up to two decimal points (e.g it would not accept 5.432 only 5.43). I have tried to do :

cin >> fixed >> showpoint >> setprecision(2);
(which didn't work obviously)

From reading forums and help sites I get the feeling that I should change the ans1 and ans2 from doubles to strings or floats but can't figure out the restrictions I would need to limit the input to two decimals.

The second question I cannot even find a lead in any forums or help sites. I need to know how to make it so the results are not rounded up and the result are shown in two decimal points (which I got, just not the rounding). I know int will not round but it also will not display the two decimal points.
--------------------------------------------------------------------------------

 ``1234567891011121314151617181920212223242526272829303132333435363738394041424344454647`` `````` double num1, num2 case '1' :cout << "Input first number: ";//First number input cin >> fixed >> showpoint >> setprecision(2); cin >> num1; cout << "\n"; //Number1 validation while (!cin) { cin.clear(); cin.ignore(256,'\n'); cout << "Input first number: "; cin >> num1; cout << "\n"; }//End validation cout << "Input second number: ";//Second number input cin >> num2; cout << "\n"; //Number2 validation while (!cin) { cin.clear(); cin.ignore(256,'\n'); cout << "Input second number: "; cin >> num2; cout << "\n"; }//End validation cout << fixed << showpoint << setprecision(2);//Set point cout << "--------------------------------\n\n" ; cout << num1 << " + "<< num2 << " = "<< num1 + num2 << "\n";//Solution break; ``````

Last edited on
Topic archived. No new replies allowed.