How do I round?

I've tried everything possible and the final answer won't round up. This is what I have so far and this is what it is telling me to do.

"Enter pounds: 7
Enter shillings: 17
Enter pence: 9
Result in Decimal pounds = £7.89

In ASCII the '\x9c' represents the pound sign. Also, display the result with two decimal places (display £7.89, not £7.88xxxx) by using the fixed, showpoint and setprecision() manipulators"

I got the code working perfectly, the one issue I am having is that I keep getting £7.88 not £7.89

I got it rounded to 2 decimal places at least. If anyone can help that would be highly appericated. Thank You

-Giorgio




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

int main(){

	//Declare Variables
	int Pounds;
	int Shillings; 
	int Pence;
	string User_Answer;

	//InitializeVariables
	Pounds = 0;
	Shillings = 0;
	Pence = 0;
	User_Answer = "C";

	while ( User_Answer == "C" || User_Answer == "c" ){
		cout << "Enter pounds     : ";
		cin >> Pounds;
		cout << "Enter shillings  : ";
		cin >> Shillings;
		cout << "Enter pence      : ";
		cin >> Pence;
		Pence = (((Shillings * 12) + Pence) * 100)/240;
		//If statement (Pence will not exceed 100)
		if (Pence >= 100){
			//Calculating expressions
			Shillings = Pence % 100;
			Pounds = Pounds + ((Pence-Shillings) / 100);
			Pence = Shillings;
		}
	
		cout << "Decimal pounds   = \x9c" << fixed << setprecision(3) << Pounds << "." << Pence << endl;
		cout << "Press C to continue or E to exit.\t\t";
		cin >> User_Answer;
		}

	system("pause");
	return 0;

}

the one issue I am having is that I keep getting £7.88 not £7.89
What is your input?
closed account (2UD8vCM9)
Well part of your issue is that integers are always rounded down by default.
Another issue is even if integers were rounded up, Pence is an integer in itself, so you will not be able to perform proper rounding on it.

Solution: Change pence to a float.
Add 0.5 to your pence calculation to make it round correctly.
When calculating shilling, recast your Pence as an integer so you can use mod (%).

Example:
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
#include <iostream>
#include <string>
#include <iomanip>
using namespace std;

int main(){

	//Declare Variables
	int Pounds;
	int Shillings;
	int Pence;
	string User_Answer;

	//InitializeVariables
	Pounds = 0;
	Shillings = 0;
	Pence = 0;
	User_Answer = "C";

	while (User_Answer == "C" || User_Answer == "c"){
		cout << "Enter pounds     : ";
		cin >> Pounds;
		cout << "Enter shillings  : ";
		cin >> Shillings;
		cout << "Enter pence      : ";
		cin >> Pence;
		Pence = (((Shillings * 12) + Pence) * 100) / 240 + 0.5; //We add 0.5 to round up when necessary
		//If statement (Pence will not exceed 100)
		if (Pence >= 100){
			//Calculating expressions
			Shillings = (int)Pence % 100;
			Pounds = Pounds + ((Pence - Shillings) / 100);
			Pence = Shillings;
		}

		cout << "Decimal pounds   = \x9c" << fixed << setprecision(3) << Pounds << "." << Pence << endl;
		cout << "Press C to continue or E to exit.\t\t";
		cin >> User_Answer;
	}

	system("pause");
	return 0;

}

@Pindrought I tried your code and actually copied it and pasted to it to see if it would work. It still did not work.
closed account (2UD8vCM9)
What do you mean it did not work? o.O

Worked fine for me.
@Pindrought It's still giving me 7.88

I'm using Microsoft Visual Studios professonial 2012. I copied and pasted the code and it still gace me 7.88

I changed pence to a float and it gave me a bunch of numbers right after. Something isn't right and I'm trying to figure out what.
Can someone please help me? I've been struggling through this for hours trying so many different methods. :/
Topic archived. No new replies allowed.