online compiler and debugger for c/c++

code. compile. run. debug. share.
Source Code   
Language
/* Title: Currency Translation Machine Author: Diego Zamudio Description: Convert between currencies and save the result to a file. */ #include <iostream> #include <fstream> #include <iomanip> #include <unordered_map> #include <string> #include <cctype> #include <limits> class CurrencyConverter { public: CurrencyConverter() { // Approximate current mid-market rates (relative to 1 USD) rates_ = { {"USD", 1.00}, {"EUR", 0.86}, {"GBP", 0.75}, {"JPY", 158.6}, {"CAD", 1.34}, {"MXN", 17.65} }; } bool has(const std::string& code) const { return rates_.count(toCode(code)) > 0; } double convert(double amount, const std::string& from, const std::string& to) const { std::string f = toCode(from); std::string t = toCode(to); double inUsd = amount / rates_.at(f); return inUsd * rates_.at(t); } static std::string toCode(std::string s) { for (char& c : s) c = static_cast<char>(std::toupper(static_cast<unsigned char>(c))); return s; } void listSupported(std::ostream& os) const { os << "Supported codes: USD"; for (const auto& kv : rates_) { if (kv.first != "USD") os << ", " << kv.first; } os << '\n'; } private: std::unordered_map<std::string, double> rates_; }; struct IO { static void intro(std::ostream& os) { os << "Currency Translation Machine\n"; os << "Enter an amount and two currency codes. I will convert and save the result.\n\n"; } static std::string promptCode(const std::string& msg, const CurrencyConverter& cc) { while (true) { std::cout << msg; std::string code; std::cin >> code; if (cc.has(code)) return CurrencyConverter::toCode(code); std::cout << "Unknown code. "; cc.listSupported(std::cout); } } static double promptAmount(const std::string& msg) { while (true) { std::cout << msg; double x; if (std::cin >> x && x >= 0.0) return x; std::cout << "Enter a nonnegative number.\n"; std::cin.clear(); std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n'); } } }; int main() { CurrencyConverter cc; IO::intro(std::cout); cc.listSupported(std::cout); double amount = IO::promptAmount("Amount: "); std::string from = IO::promptCode("From currency code: ", cc); std::string to = IO::promptCode("To currency code: ", cc); double result = cc.convert(amount, from, to); std::cout << std::fixed << std::setprecision(2); std::cout << "\nResult: " << amount << ' ' << from << " = " << result << ' ' << to << "\n"; std::ofstream out("currency_output.txt"); if (out) { out << "Currency Translation Receipt\n"; out << "----------------------------\n"; out << std::fixed << std::setprecision(2); out << "Amount: " << amount << ' ' << from << '\n'; out << "Converted: " << result << ' ' << to << '\n'; out << "From code: " << from << '\n'; out << "To code: " << to << '\n'; out << "Saved by: Currency Translation Machine\n"; std::cout << "Saved to currency_output.txt\n"; } else { std::cerr << "Could not write currency_output.txt\n"; } return 0; }
Currency Translation Receipt ---------------------------- Amount: 10.00 USD Converted: 176.50 MXN From code: USD To code: MXN Saved by: Currency Translation Machine

Compiling Program...

Command line arguments:
Standard Input: Interactive Console Text

                

                

Program is not being debugged. Click "Debug" button to start program in debug mode.

#FunctionFile:Line
VariableValue
RegisterValue
ExpressionValue