Set Prec

Hey guys :)

I'm suppose to write a C++ code that computes and displays the area of a square or a triangle after prompting its user to type in the first letter of the desired figure name. The program MUST use a switch statement.

Now I did this and this is the code:

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
48
49
50
#include <iostream>
#include <cmath>
#include <iomanip>

using namespace std;

int main ()
{
	float h, b, a;
	char figure;

	

	
	cout << "Please enter the type of figure (t) for triangle and (s) for square \n" << endl;
	cin >> figure;

	switch (figure)

	{

case 't':

		cout << "Please enter a positive integer value for the base in inches \n"<<  setprecision (2) << endl;
		cin >> b;
		
		cout <<"Please enter a positive integer value for the height in inches \n" <<  setprecision (2)<<  endl;
		cin >> h;
		
		cout  <<"The area of the triangle is \n" << (1.0/2.0)*h*b <<  setprecision (2)<< endl;
		break;

case 's':
		cout  <<  "Please enter the  length of a side in inches \n" <<  setprecision (2)<< endl;
		cin >> a;
		

		cout  << "The area of the square is \n" << a*a <<  setprecision (2)<< endl;
		break;

default:
		cout  << "The type of figure you have selected is invalid \n" << endl;
		break;

	}

	return 0;

}


The problem I am experiencing is the precision to two decimal places. Help?
Well, for one thing, if that were the correct way to use it, why on earth would you use it in couts that only print text? Even more so, why would you put it AFTER the number that requires the precision?
Revised code (Sorry! Just messing around trying to figure it out)

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
48
#include <iostream>
#include <cmath>
#include <iomanip>

using namespace std;

int main ()
{
	float h, b, a;
	char figure;
	

	
	cout << "Please enter the type of figure (t) for triangle and (s) for square \n" << endl;
	cin >> figure;

	switch (figure)

	{

case 't':

		cout << "Please enter a positive integer value for the base in inches \n"<<  endl;
		cin >> b;
		
		cout <<"Please enter a positive integer value for the height in inches \n" <<  endl;
		cin >> h;
		
		cout  <<"The area of the triangle is \n" << 0.5*b*h <<  endl;
		break;

case 's':
		cout  <<  "Please enter the  length of a side in inches \n" << endl;
		cin >> a;
		

		cout  << "The area of the square is \n" << a*a << endl;
		break;

default:
		cout  << "The type of figure you have selected is invalid \n" << endl;
		break;

	}

	return 0;

}
Well, deleting all setprecisions won't help you...

Doesn't it make more sense to put the setprecision BEFORE the number that needs it?

Alternatively, you can set it once by using: cout.setprecision(<precision>); somewhere early in the code (so obviously BEFORE any numbers are printer...).
Our teacher kinda forbade us from using cout.setprecision. I got two decimal places for the area of a square :) Triangle is kinda iffy. But I'm working on it.
Topic archived. No new replies allowed.