Building a simple program? Help with output

I am building a simple program that finds the average of three grades. The program runs fine. But, I am trying get my answer stored in fixed point notation with two decimal points of precision. Thus far, I have tried to using the: cout << setprecision(2); code. But, I have not been successful.

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
 

// This program finds the average of three grades


#include <iostream>
#include <iomanip>
#include <cmath>
#include <limits>

using namespace std;

int main()

{
	float gradeOne;
	float gradeTwo;
	float gradeThree;
	float average;
	
	// Prompts the user for grades 
	
	cout << "Please input the first grade: ";
	cin  >> gradeOne;
	cout << "\n";

        cout << "Please input the second grade: ";
	cin  >> gradeTwo;
	cout << "\n";
	
	cout << "Please input the third grade: ";
	cin  >> gradeThree;
	cout << "\n";
	
	
	// Finds the average and displays the result
	
	average = (gradeOne + gradeTwo + gradeThree) / 3;
	
	cout << "So your average is " << average << endl;
	cout << endl;
	
system ("pause");

return 0;

}
Last edited on
You said you've tried using cout << setprecision(2);?
Correct notation cout << cout::setprecision(2);

You don't need to pass the function into the stream, you can just call the function from the stream object, i.e. cout.setpresision(2);

EDIT: Also I don't see why you need half of those includes at the top, all you need is the IO
Last edited on
closed account (48T7M4Gy)
Another way which I find is handy is use printf() which you can look up in the reference section here.

eg printf("Average %4.2f", average);
@Satsumbenji:

Shouldn't it be cout<<std::setprecision(2);??

 
cout << "So your average is " << setprecision(.2) << average << endl;
Shouldn't it be cout<<std::setprecision(2);??

I don't know, I've never had a use for it and was reading from an example tutorial XD
Last edited on
Topic archived. No new replies allowed.