visual currency converter [fltk]

Bjarne Stroustrup “Programming Principles and Practice Using C++”
Chapter 16 Exercise 8
Using GUI library called FLTK (Fast Light Tool Kit, “full tick”).

```Output:

```

[code language=”cpp”]
// Philipp Siedler
// Bjarne Stroustrup’s PP
// Chapter 16 Exercise 8

#define _USE_MATH_DEFINES
#include "Simple_window.h"
#include "Graph.h"
#include <cmath>
#include "GUI.h"
#include <time.h>

// layout
constexpr int xmax = 400;
constexpr int ymax = 400;

enum currencies
{
euro = 0, pound
};

struct application_window : Window {
application_window(Point xy, int w, int h, const string& title, const string& conversion_rate_file_location);

private:

//————- DATA

currencies from;
currencies to;

// data
float conversion_rate;

// methods
float get_conversion(const string& crfl);

//————- DATA END

//————- LAYOUT

// quit button
Button quit_button;

// convert button
Button convert_button;

// in
In_box value_in;
Out_box value_out;
Out_box value_in_currency;
Out_box value_out_currency;

// actions invoked by callbacks
void quit() { hide(); }

void convert();

void from_euro_pressed() { hide_from_menu(); value_in_currency.put("euro"); from = euro; }
void from_pound_pressed() { hide_from_menu(); value_in_currency.put("pound"); from = pound; }

void to_euro_pressed() { hide_to_menu(); value_out_currency.put("euro"); to = euro; }
void to_pound_pressed() { hide_to_menu(); value_out_currency.put("pound"); to = pound; }

// callback functions

//————- LAYOUT END
};

application_window::application_window(Point xy, int w, int h, const string& title, const string& conversion_rate_file_location)
: Window(xy, w, h, title)
, quit_button(Point(x_max() – 70, 0), 70, 20, "Quit", cb_quit)
, convert_button(Point(x_max() – 70, 20), 70, 20, "Convert", cb_convert)
, value_in(Point(0, 60), 70, 20, "enter value")
, value_out(Point(x_max() / 2, 60), 70, 20, "")
, value_in_currency(Point(70, 60), 70, 20, "")
, value_out_currency(Point(x_max() / 2 + 70, 60), 70, 20, "")
{
conversion_rate = get_conversion(conversion_rate_file_location);

from = euro;
to = euro;

attach(quit_button);
attach(convert_button);

attach(value_in);
attach(value_out);

attach(value_in_currency);
attach(value_out_currency);

value_in_currency.put("euro");
value_out_currency.put("euro");

from_menu.attach(new Button(Point(0, 0), 0, 0, "euro", cb_from_euro));
from_menu.attach(new Button(Point(0, 0), 0, 0, "pound", cb_from_pound));

to_menu.attach(new Button(Point(0, 0), 0, 0, "euro", cb_to_euro));
to_menu.attach(new Button(Point(0, 0), 0, 0, "pound", cb_to_pound));

}

float application_window::get_conversion(const string& crfl)
{
string iname = crfl;
ifstream ist(iname);

float rate;
if (!ist) { error("File could not be read: ", iname); }
else { ist >> rate; }

return rate;
}

void application_window::convert()
{
float input = strtof((value_in.get_string()).c_str(), 0);

if (from == euro && to == pound)
{
input *= conversion_rate;

ostringstream ss;
ss << input;
string s(ss.str());

value_out.put(s);
}
else if (from == pound && to == euro)
{
input *= 1.0 + (1.0 – conversion_rate);

ostringstream ss;
ss << input;
string s(ss.str());

value_out.put(s);
}
else
{
ostringstream ss;
ss << input;
string s(ss.str());

value_out.put(s);
}
}

int main()
try
{
application_window win(Point(100, 100), xmax, ymax + 20, "currency converter", "Data/conversion_rate.txt");
return gui_main();
}
catch (exception& e) {
cerr << "error: " << e.what() << ‘\n’;
keep_window_open();
return 1;
}
catch (…) {
cerr << "Unknown exception!\n";
keep_window_open();
return 2;
}
[/code]

```Input:
Text file: Data/conversion_rate.txt
0.87
```