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.
--------------------------------------------------------------------------------

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47

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.