C++ HELP! Fixed Notation

For some reason, it will not display in fixed notation, it still displays in scientific notation. Also it will not display a precision of 2.

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
#include <iostream>
#include <vector>
using namespace std;

int main()
{
	std::fixed;
	cin.precision(2);
	double totalDonations = 0;
	double averageDonations = 0;
	int donLargAvg = 0; //donations larger than average
	int donSmalAvg = 0;
	int donEquAvg = 0;
	vector <double> donations(10);
	cout << "Enter 10 donations: ";
	for (int i = 0; i < 10; i++)
	{
		if (cin >> donations[i])
			totalDonations += donations[i];
		else if (!(cin >> donations[i]))
		{
			cout << "Please enter a numeric value." << endl;
			exit(EXIT_FAILURE); 
		}
	}
	averageDonations = totalDonations / 10;
	for (int i = 0; i < 10; i++)
	{
		if (donations[i] > averageDonations)
			donLargAvg++;
		else if (donations[i] < averageDonations)
			donSmalAvg++;
		else if (donations[i] == averageDonations)
			donEquAvg;
	}
	cout << "Total Donations: $" << totalDonations << endl;
	cout << "Average Donations: $" << averageDonations << endl;
	cout << "Donations smaller than average: " << donSmalAvg << endl;
	cout << "Donations larger than average: " << donLargAvg << endl;
	cout << "Donations equal to average: " << donEquAvg << endl;
}


Edit: AHHH I SEE, I used cin.precision instead of cout.precision. hahahaha!
Last edited on
@MienTommy

Just wanted to mention that this will almost never work :

1
2
		else if (donations[i] == averageDonations)
			donEquAvg;


Doubles are stored as binary fractions and cannot exactly represent every real number, even ones like 0.1, so equality tests like this will certainly fail.

To test for equality within a certain precision, check the absolute value of the difference between the 2 numbers, is less than the precision :

1
2
3
4
5
6
7
8
const double PRECISION = 0.001;

if(std::abs(donations[i] - averageDonations) < PRECISION ) {
   std::cout << "The two values are equal within " << PRECISION << "\n";
}
else {
    std::cout << "The two values are not equal."  << "\n";
}


However, with your code, you have already tested for less than & greater than, so the only other remaining possibility is that of equality, so this should be with an else clause - not else if. On line 34, you didn't increment the donEquAvg variable, but as I said, this line would never be executed any way.

There is also this value :

std::numeric_limits<double>::epsilon()

http://www.cplusplus.com/reference/limits/numeric_limits/


Which is the difference between 1.0 and the next representable number. You should scale this value up for the number range you want to use. For example, if your number is 1000.0 then the epsilon for it would be :

constexpr double Epsilon1000 = 1000.0 * std::numeric_limits<double>::epsilon();

C++11 also has functions like std::nextafter & variations of, described here:

http://en.cppreference.com/w/cpp/numeric/math/nextafter


Another pedantic thing : If I mean to use a double, then I always specify them explicitly as doubles, like this :

averageDonations = totalDonations / 10.0;

Except that I would do this, to avoid "magic numbers" throughout the code:

1
2

averageDonations = totalDonations / static_cast<double>(donations.size);


Because you are using vectors, you should consider using a while loop instead of a for loop. The end condition would some sentinel value like zero say.

If you were using a fixed size container like an array for example, then make use of const declared variables, like this :

1
2
3
const int SIZE = 10;

double Donations[SIZE];


Line 20 should be else not else if because the cin either works or it doesn't. The exit on line 23 is rather rude - make one mistake and the program terminates!

There are other things to check - like cin.fail, and make use of a while loop until good input is achieved.

Hope all this helps - I look forward to seeing how you get on. 8+)
Thanks. I learned alot from that and I appreciate you for pointing out the little things that will make my code better.

I'm actually just starting programming reading from the book "C++ Primer Plus." So we really haven't gotten in-depth with cin.fail() and error checking and whatnot.

I will favorite this and use this as reference :)
Last edited on
No worries.

I forgot to mention the reference section at the top left of this page. There is a wealth of info there, plus a really good tutorial.

Cheers
Topic archived. No new replies allowed.