Adding currency symbol before the output of a floating point variable?

Hi all, brand new here. I'm just finishing up my second project for uni, and have managed to hit a brick wall whilst trying to add the £ sign before variableShippingCost in this line of code:

cout << "The cost of the shipment over " << number << " miles is: " << variableShippingCost << "\n";

If anybody could point me in the right direction I'd be extremely grateful. Here's the rest of the code if it's relevant;

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


double number; 
float variableShippingCost; 


void calculateVariableCost()
{
	if (number <= 100)
		variableShippingCost = 50 + (number*5.5);
	else if ((number > 100) && (number <= 500))
		variableShippingCost = 50 + (100 * 5.5) + ((number - 100)*4.0);
	else if (number > 500)
		variableShippingCost = 50 + (100 * 5.5) + (400 * 4.0) + ((number - 500)*2.5);
}

int main()
{

cout << "Enter distance in miles: ";
cin >> number;

if (number <= 0)
	cout << "ERROR: The distance must be a positive value.";
else
	calculateVariableCost();
cout << "The cost of the shipment over " << number << " miles is: " << variableShippingCost << "\n";
	system("PAUSE");
	return 0;
}



Also, just a quick unimportant question, I was previously lead to believe that procedures must be 'prototyped' before being called, is this true? If so, could somebody tell me why the code above works without prototype for 'calculateVariableCost'?

Once again really appreciate any guidance,

Regards,
James
Last edited on
Can't you not just put £ inside the preceding string literal?
 
cout << "The cost of the shipment over " << number << " miles is: £" << variableShippingCost << "\n";


If you had defined calculateVariableCost() after the point where it was used you would have to declare (prototype) it above.
Thanks for the reply, Peter.

I did try that but it appears Visual Studio doesn't recognize the £ symbol. Managed to resolve the issue by using (char)156 instead of £.

I've now encountered another problem, when I input a negative value as the distance in miles, I am getting the following output:

1
2
3
4
Enter distance in miles: -20
ERROR: The distance must be a positive value.The cost of the shipment over -20 m
iles is: £0
Press any key to continue . . .


Really baffled as to why it is printing "The cost of the shipment over -20 m
iles is: £0" even when number is <= 0... :(

1
2
3
4
5
6
7
8
9
10
11
12
13
14
int main()
{

cout << "Enter distance in miles: ";
cin >> number;

if (number <= 0)
	cout << "ERROR: The distance must be a positive value.";
else if (number > 0)
	calculateVariableCost();
	cout << "The cost of the shipment over " << number << " miles is: " << (char)156 << variableShippingCost << "\n";
	system("PAUSE");
	return 0;
}
Last edited on
If you want more than one statement to be part of an if or else you need to surround them with braces.
1
2
3
4
5
6
7
...
else
{
	calculateVariableCost();
	cout << ...
}
...
Topic archived. No new replies allowed.