Help Needed with currency converter in PPP 2

I have code here that converts Yen, Pounds, Yuan and Euro to US Dollars that I wrote for Stroustrup's book, PPP 2, and I need to add conversions from Kroner to it, but I see when I Google "kroner" that there's only those Swedish and Norwegian currencies called Krone. Does the book have a typo, or is there still something I can do here?

It's for a "Try this" exercise in Chapter 4 Section 4.4.1.3; here's what it says:

"Rewrite your currency converter program from the previous Try this to use a
switch -statement. Add conversions from yuan and kroner. Which version of
the program is easier to write, understand, and modify? Why?"

I'd written it to convert from Yen, Euro or Pounds to Dollars originally and then I changed it to also do it for Yuan and also changed it to use a switch-statement for the conversion instead of an if-statement, but I'm stuck on the Kroner part.

This is my code that I wrote (I tried to make the changes specified, but of course I also need the conversion rates for the Kroner):

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
51
52
53
54
55
56
57
58
59
60
61
62
// Osman Zakir
// 12 / 5 / 2016
// Bjarne Stroustrup: Programming: Principles and Practice Using C++ 2nd Edition
// Chapter 4 Section 4.4.1.1 Try This Exercise
// This program converts from Yen, Euros or Pounds to Dollars

#include "../../std_lib_facilities.h"

double convert(const char unit, const double amount);
bool check_input(const char unit);

int main()
{
	cout << "Please enter a money amount followed by currency (y(en), e(uro), p(ound),\n"
		<< "or Y(uan) (must be an uppercase Y, to make sure to not clash with\nlowercase y for y(en)):\n";
	double amount = 0;
	char unit = ' ';
	cin >> amount >> unit;
	if (!check_input(unit))
	{
		cout << "Sorry, I don't know a unit called '" << unit << "'\n";
		return 1;
	}
	double dollars = convert(unit, amount);
	cout << "Your money converted to US Dollars is $" << dollars << ".\n";
	keep_window_open();
}

double convert(const char unit, const double amount)
{
	constexpr double yen_to_dollar = 114.239139;
	constexpr double euro_to_dollar = 0.927889;
	constexpr double pound_to_dollar = 0.785687;
	constexpr double yuan_to_dollar = 0.143708;
	double dollars = 0;

	switch (unit)
	{
	case 'y':
		dollars = amount / yen_to_dollar;
		break;
	case 'e':
		dollars = amount / euro_to_dollar;
		break;
	case 'p':
		dollars = amount / pound_to_dollar;
		break;
	case 'Y':
		dollars = amount * yuan_to_dollar;
		break;
	}
	return dollars;
}

bool check_input(const char unit)
{
	if (unit == 'y' || unit == 'e' || unit == 'p' || unit == 'Y')
	{
		return true;
	}
	return false;
}
Last edited on
Hi,

I was pretty confused when I read your question at first :)

DragonOsman wrote:
when I Google "kroner" that there's only those Swedish and Norwegian currencies called Krone. Does the book have a typo, or is there still something I can do here?

The Swedish currency is kronor and not kroner, so basically you have either Danish or Norvergian, so again ambiguity error.

I would go with Danish kroner because it works in Denmark, Greenland and the Faroe Islands, the Norvergian works only in Norway.

DragonOsman wrote:
I also need the conversion rates for the Yuan and Kroner

Hail google!

Last edited on
Stroustrup is Danish, so go with that one
https://www.bloomberg.com/quote/USDDKK:CUR
edit: at least he was born there, I don't know what's his current nationality
Last edited on
Wikipedia check: Yup he is Danish
Alright, thanks. I'll go with the Danish currency, then.

By the way, if I want to use Wt or one of the C++ Web frameworks to have this program get the conversions from the Web, what tutorial or recourse should I try?

I recently followed and a tutorial on GitHub for the C++ REST SDK and wrote a console app that makes a request to Google and writes the search results to an HTML file. I was thinking maybe I could tweak the functionality a bit for this currency conversion app and have it get conversions rates from a currency conversion site like X-rates (or any other good site that does this) and calculate the conversion using that.

Here's the code for that other app:

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
51
52
53
54
55
56
57
58
59
60
// Osman Zakir
// 12 / 16 / 2016
// C++ REST SDK practice app

#include <iostream>
#include <stdexcept>
#include <cpprest/http_client.h>
#include <cpprest/filestream.h>

using namespace utility;
using namespace web;
using namespace web::http;
using namespace web::http::client;
using namespace concurrency::streams;

int main()
{
	auto fileStream = std::make_shared<ostream>();

	// Open stream to output file
	pplx::task<void> requestTask = fstream::open_ostream(U("results.html")).then([=](ostream outFile)
	{
		*fileStream = outFile;

		// Create http_client to send the request
		http_client client(U("https://www.google.com/"));

		// Build request URI and start the request
		uri_builder builder(U("/search"));
		builder.append_query(U("q"), U("cpprestsdk github"));
		return client.request(methods::GET, builder.to_string());
	})
	
	// Handle response headers arriving
	.then([=](http_response response)
	{
		using namespace std;
		cout << "Received response status code:" << response.status_code() << "\n";

		// Write response body into the file
		return response.body().read_to_end(fileStream->streambuf());
	})
	
	// Close the file stream
	.then([=](size_t)
	{
		return fileStream->close();
	});

	using namespace std;
	// Wait for all the outstanding I/O to complete and handle any exceptions
	try
	{
		requestTask.wait();
	}
	catch (const exception &e)
	{
		cerr << "Error exception: " << e.what() << "\n";
	}
}


I've installed Clang for Windows on my machine, and I'd like to also try to compile this with it using that on the command line, but I'm pretty sure it'd give me an error since I've installed the REST package natively into just the Visual Studio Solution itself and it doesn't seem like the files were installed into the directory.

I did successfully compile and run it from Visual Studio itself, I just also want to compile it on the command line with Clang to see how to do it with the C++ REST SDK.
Last edited on
Topic archived. No new replies allowed.