online compiler and debugger for c/c++

code. compile. run. debug. share.
Source Code   
Language
#include <iostream> #include <string.h> #include <iomanip> #define MAX 8 using namespace std; // I would avoid using namespace std, explanation ahead struct menuItemType { string menuItem; double menuPrice; }; //menuItemType menulist[MAX]; //does not need to be global void getData(menuItemType *menulist); void showMenu(); int printCheck(double total); // pass the total to the printCheck() function int main() { menuItemType menulist[MAX]; // go local when possible cout << "Welcome to Mavel's Restaurant\n\n"; cout << "------------ Menu ------------ \n"; showMenu(); // here you could also pass menulist and print the menu using the data //it would be easier to refactor if you add a menu item, try it getData(menulist); int choice; char add; double total = 0; do { cout << "Enter choice: "; cin >> choice; // do the math in the cycle so you can keep track of the sum of selected items if(choice > 0 && choice <= MAX) total += menulist[choice - 1].menuPrice; switch (choice) { case 1: cout << "You ordered Plain Egg.\n"; break; case 2: cout << "You ordered Bacon and Egg.\n"; break; case 3: cout << "You ordered a muffin.\n"; break; case 4: cout << "You ordered French Toast.\n"; break; case 5: cout << "You ordered Fruit Basket.\n"; break; case 6: cout << "You ordered Cereal.\n"; break; case 7: cout << "You ordered Coffee.\n"; break; case 8: cout << "You ordered Tea.\n"; break; default: cout << "Invalid Choice."; break; } cout << "Would you like to order another item? [Y]es / [N]o : "; cin >> add; if (add == 'N' || add == 'n') { printCheck(total); } } while (add == 'Y' || add == 'y'); } void getData(menuItemType *menulist) { menulist[0].menuItem = "Plain Egg"; menulist[0].menuPrice = 140.50; menulist[1].menuItem = "Bacon and Egg"; menulist[1].menuPrice = 245.00; menulist[2].menuItem = "Muffin"; menulist[2].menuPrice = 295.00; menulist[3].menuItem = "French Toast"; menulist[3].menuPrice = 495.00; menulist[4].menuItem = "Fruit Basket"; menulist[4].menuPrice = 555.00; menulist[5].menuItem = "Cereal"; menulist[5].menuPrice = 385.00; menulist[6].menuItem = "Coffee"; menulist[6].menuPrice = 415.00; menulist[7].menuItem = "Tea"; menulist[7].menuPrice = 333.00; } void showMenu() { cout << "[1] Plain Egg\t\tPhp140.50\n"; cout << "[2] Bacon and Egg\tPhp245.00\n"; cout << "[3] Muffin\t\tPhp295.00\n"; cout << "[4] French Toast\tPhp495.00\n"; cout << "[5] Fruit Basket\tPhp555.00\n"; cout << "[6] Cereal\t\tPhp385.00\n"; cout << "[7] Coffee\t\tPhp415.00\n"; cout << "[8] Tea\t\t\tPhp333.00\n\n"; } // now can simply print the check int printCheck(double total) { double tax = total * 0.05; double totalbill = total + tax; cout << "----------------------------------------------\n"; cout << "Tax\t\t" << tax << endl; cout << "Amount Due\tPhp" << totalbill << endl; cout << "----------------------------------------------\n"; return total; // you're not using this return value, you may aswell make the function return type void }

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