I have gotten it to compile, using only the Dinkumware online compiler!
Code:
#include <iostream>
#include <iomanip>
#include <string>
#include <vector>
#include <exception>
namespace jsw {
class Password {
private:
std::string password;
const int tries;
public:
Password(std::string pw, int t) : password(pw), tries(t) {}
bool Get(std::string prompt);
};
bool Password::Get(std::string prompt) {
std::string pw;
for(int x = 0; x < tries && pw != password; x ++) {
std::cout << prompt;
std::getline(std::cin, pw);
}
return pw == password;
}
class Selection {
private:
struct item_t {
int value;
std::string prompt;
};
std::vector<item_t> prompts;
public:
void Add(int rv, std::string prompt);
int Get(std::string prompt);
};
void Selection::Add(int rv, std::string prompt) {
item_t item;
item.value = rv;
item.prompt = prompt;
prompts.push_back(item);
}
int Selection::Get(std::string prompt) {
int v;
for(;;) {
for(int x = 0; x < prompts.size(); x ++) {
std::cout << prompts[x].prompt << std::endl;
}
std::cout << prompt;
if((std::cin >> v) == 0) throw std::exception();
for(int x = 0; x < prompts.size(); x ++) {
if(v == prompts[x].value) return x;
}
}
}
class Input {
public:
template <typename T>
static T Get(istream &i) { T x; i >> x; return x; }
};
class MonetaryConversion {
private:
static const double euroRate = 1.27155;
public:
static double EuroToDollar(double x) { return x * euroRate; }
static double DollarToEuro(double x) { return x / euroRate; }
};
}
// start of Prelude's code
int main() try
{
const int tries = 3;
// TODO: Not hardcode requested password
jsw::Password password ( "skip", tries );
if ( password.Get ( "Please enter your password: " ) )
{
const std::string prompt[] = { "Euros: ", "Dollars: " };
const std::string result[] = { "In Dollars: ", "In Euros: " };
enum { euro_to_dollar = 1, dollar_to_euro };
jsw::Selection menu; // Menu controller
int selector; // Menu selector
double value; // Value to convert
// Set up and process menu
menu.Add ( euro_to_dollar, "1) Euros to Dollars" );
menu.Add ( dollar_to_euro, "2) Dollars to Euros" );
selector = menu.Get ( "Selection: " );
// Set up and process conversion
std::cout<< prompt[selector - 1];
value = jsw::Input::Get<double> ( std::cin );
std::cout<< result[selector - 1];
std::cout.setf ( std::ios::fixed, std::ios::floatfield );
std::cout.precision ( 2 );
if ( selector == euro_to_dollar )
std::cout<< jsw::MonetaryConversion::EuroToDollar ( value ) <<'\n';
else
std::cout<< jsw::MonetaryConversion::DollarToEuro ( value ) <<'\n';
}
else
std::cerr<<"Too many incorrect passwords. Closing...\n";
}
catch ( const std::exception& ex ) {
std::cerr<< ex.what() <<'\n';
}
Does it count, Prelude? I throw an exception if the user enters an invalid number. It's probably not the best place to throw an exception; but I wanted to keep it simple (the text-entering box for Dinkumware doesn't have syntax highlighting; you don't know how much you depend on it until it's missing).
Why did you choose the name "jsw", by the way? Just curious.