online compiler and debugger for c/c++

code. compile. run. debug. share.
Source Code   
Language
#include <iostream> #include "bank.h" int main() { Bank bank; while (true) { std::cout << "Welcome to the Bank of the Universe" << "\n"; std::cout << "Please press a button" << "\n"; std::cout << "'c' - Create new account" << "\n"; std::cout << "'d' - Deposit" << "\n"; std::cout << "'w' - Withdraw" << "\n"; std::cout << "'s' - Show balance" << "\n"; std::cout << "'q' - Quit program" << "\n"; char c; std::cin >> c; switch (tolower(c)) { case 'q': return 0; case 'c': bank.CreateAccount(); break; case 'd': bank.Deposit(); break; case 'w': bank.Withdraw(); break; case 's': std::cout << "Function is not available." << "\n"; break; default: break; } } }
#ifndef ACCOUNT_H_ #define ACCOUNT_H_ // Standard Library Header Files // #include <string> class Account { private: double balance_; std::string id_; public: explicit Account(std::string id); double GetBalance() const noexcept; void SetBalance(double balance) noexcept; std::string GetId() const; }; #endif // ACCOUNT_H_
#include "account.h" // Standard Library Header Files // #include <utility> Account::Account(std::string id) : balance_{0.0} , id_{std::move(id)} {} double Account::GetBalance() const noexcept { return balance_; } void Account::SetBalance(const double balance) noexcept { balance_ = balance; } std::string Account::GetId() const { return id_; }
#ifndef BANK_H_ #define BANK_H_ // Standard Library Header Files // #include <map> #include <memory> #include <string> // Forward Declarations // class Account; class Bank { private: std::map<std::string, std::shared_ptr<Account>> accounts_; public: void CreateAccount(); void Deposit(); void Withdraw(); }; #endif // BANK_H_
// Standard Library Header Files // #include <fstream> #include <iomanip> #include <iostream> #include <sstream> // Project Specific Header Files // #include "account.h" #include "bank.h" #include "tools.h" void Bank::CreateAccount() { std::ostringstream account_id; account_id << std::setw(4) << std::setfill('0') << (accounts_.size() + 1); auto success = accounts_.insert(std::make_pair( account_id.str(), std::make_shared<Account>(account_id.str()))); if (!success.second) { std::cout << "Account: " << account_id.str() << " already exists" << "\n\n"; return; } std::cout << "Account: " << account_id.str() << " created." << "\n\n"; auto temp_directory = GetTempPath() + "\\"; auto customer_receipt = temp_directory + "customer_receipt_" + account_id.str() + ".txt"; auto bank_receipt = temp_directory + "customer_receipt.txt"; std::ofstream customer_writer(customer_receipt, std::ios_base::app); customer_writer << "Account: " << account_id.str() << " created." << "\n\n"; std::ofstream bank_writer(bank_receipt, std::ios_base::app); bank_writer << "Account: " << account_id.str() << ":" << "\n"; bank_writer << "Account: " << account_id.str() << " created." << "\n\n"; } void Bank::Deposit() { std::cout << "Please enter your Account number: "; std::string account_id; std::cin >> account_id; std::shared_ptr<Account> account; if (accounts_.find(account_id) != accounts_.end()) { account = accounts_[account_id]; } else { std::cout << "Account number " << account_id << " does not exist!" << "\n\n"; return; } double amount; do { std::cout << "Enter the amount: "; std::cin >> amount; std::cout << "\n"; if (!std::cin.fail()) { auto balance = account->GetBalance(); balance += amount; account->SetBalance(balance); break; } std::string temp; std::cin.clear(); std::cin >> temp; std::cout << "Please enter a number" << "\n"; } while (true); std::cout << amount << " EUR deposited." << "\n"; std::cout << "Balance: " << account->GetBalance() << " EUR." << "\n\n"; auto temp_directory = GetTempPath() + "\\"; auto customer_receipt = temp_directory + "customer_receipt_" + account->GetId() + ".txt"; auto bank_receipt = temp_directory + "customer_receipt.txt"; std::ofstream customer_writer(customer_receipt, std::ios_base::app); customer_writer << amount << " EUR deposited." << "\n"; customer_writer << "Balance: " << account->GetBalance() << " EUR." << "\n\n"; std::ofstream bank_writer(bank_receipt, std::ios_base::app); bank_writer << "Account: " << account->GetId() << ":" << "\n"; bank_writer << amount << " EUR deposited." << "\n"; bank_writer << "Balance: " << account->GetBalance() << " EUR." << "\n\n"; } void Bank::Withdraw() { std::cout << "Please enter your Account number: "; std::string account_id; std::cin >> account_id; std::shared_ptr<Account> account; if (accounts_.find(account_id) != accounts_.end()) { account = accounts_[account_id]; } else { std::cout << "Account number " << account_id << " does not exist!" << "\n\n"; return; } double amount; do { std::cout << "Enter the amount: "; std::cin >> amount; std::cout << "\n"; if (!std::cin.fail()) { auto balance = account->GetBalance(); balance -= amount; account->SetBalance(balance); break; } else { std::string temp; std::cin.clear(); std::cin >> temp; std::cout << "Please enter a number" << "\n"; } } while (true); std::cout << amount << " EUR paid." << "\n"; std::cout << "Balance: " << account->GetBalance() << " EUR." << "\n\n"; auto temp_directory = GetTempPath() + "\\"; auto customer_receipt = temp_directory + "customer_receipt_" + account->GetId() + ".txt"; auto bank_receipt = temp_directory + "customer_receipt.txt"; std::ofstream customer_writer(customer_receipt, std::ios_base::app); customer_writer << amount << " EUR paid." << "\n"; customer_writer << "Balance: " << account->GetBalance() << " EUR." << "\n\n"; std::ofstream bank_writer(bank_receipt, std::ios_base::app); bank_writer << "Account: " << account->GetId() << ":" << "\n"; bank_writer << amount << " EUR paid." << "\n"; bank_writer << "Balance: " << account->GetBalance() << " EUR." << "\n\n"; }
#ifndef TOOLS_H_ #define TOOLS_H_ #include <string> std::string GetTempPath(); #endif // TOOLS_H_
#include "tools.h" std::string GetTempPath() { const char* const keys[] = {"TEMP", "TMP", "TEMPDIR", "TMPDIR"}; for (auto const& key : keys) { const auto value = getenv(key); if (value != nullptr) { std::string result(value); return result; } } return ""; }

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