online compiler and debugger for c/c++

code. compile. run. debug. share.
Source Code   
Language
#include <iostream> #include <limits> class CashRegister { private: int cashOnHandCents; public: CashRegister() : cashOnHandCents(500) {} explicit CashRegister(int initialCents) : cashOnHandCents(initialCents) {} int getCurrentBalance() const { return cashOnHandCents; } void acceptAmount(int amountCents) { cashOnHandCents += amountCents; } }; class Dispenser { private: int numberOfItems; int costCents; public: Dispenser() : numberOfItems(50), costCents(50) {} Dispenser(int costInCents, int itemCount) : numberOfItems(itemCount), costCents(costInCents) {} int getNoOfItems() const { return numberOfItems; } int getCost() const { return costCents; } bool isInStock() const { return numberOfItems > 0; } void makeSale() { if (numberOfItems > 0) { --numberOfItems; } } }; static int readInt(const char* prompt) { int value = 0; while (true) { std::cout << prompt; if (std::cin >> value) { return value; } std::cout << "Invalid input. Please enter a number.\n"; std::cin.clear(); std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n'); } } static void showMenu() { std::cout << "*** Welcome to the Candy Shop ***\n" << "To select an item, enter:\n" << "1 for Candy\n" << "2 for Chips\n" << "3 for Gum\n" << "4 for Cookies\n" << "5 to Exit\n"; } static void dispenseMessage() { std::cout << "Collect your item at the bottom and enjoy!!\n" << "*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*\n\n"; } static void sellProduct(Dispenser& item, CashRegister& reg) { const int cost = item.getCost(); std::cout << "Please deposit " << cost << " cents.\n"; int deposited = 0; while (deposited < cost) { int add = readInt("Deposit cents: "); if (add < 0) { std::cout << "Please deposit a nonnegative amount.\n"; continue; } deposited += add; if (deposited < cost) { std::cout << "Please deposit another " << (cost - deposited) << " cents.\n"; } } item.makeSale(); reg.acceptAmount(deposited); } int main() { CashRegister register1; Dispenser candy; // default: 50 items, 50 cents Dispenser chips; // default: 50 items, 50 cents Dispenser cookies; // default: 50 items, 50 cents Dispenser gum(10, 50); // 50 items, 10 cents each int choice = 0; while (choice != 5) { showMenu(); choice = readInt("Choice: "); while (choice < 1 || choice > 5) { std::cout << "Error, please select a number between 1 and 5.\n"; choice = readInt("Choice: "); } switch (choice) { case 1: if (candy.isInStock()) { sellProduct(candy, register1); dispenseMessage(); } else { std::cout << "Sorry, this item is sold out.\n\n"; } break; case 2: if (chips.isInStock()) { sellProduct(chips, register1); dispenseMessage(); } else { std::cout << "Sorry, this item is sold out.\n\n"; } break; case 3: if (gum.isInStock()) { sellProduct(gum, register1); dispenseMessage(); } else { std::cout << "Sorry, this item is sold out.\n\n"; } break; case 4: if (cookies.isInStock()) { sellProduct(cookies, register1); dispenseMessage(); } else { std::cout << "Sorry, this item is sold out.\n\n"; } break; case 5: break; } } return 0; }

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