-2

I am a first-year college student and I am currently having trouble with this program I am working on, It is about a billing system. I managed to do everything but the final part, which is printing the total, tax, and the final bill. Anything I try to do either gives me a wrong answer or a 0. Please help.

#include <iostream>
#include <string.h>
#include <iomanip>
#define MAX 8

using namespace std;

struct menuItemType{
    string menuItem;
    double menuPrice;
};

menuItemType menulist[MAX];

void getData();
void showMenu();
int printCheck(void);
//int b[8] = {0,0,0,0,0,0,0,0};

int main(){
    cout << "Welcome to Mavel's Restaurant\n\n";
    cout << "------------ Menu ------------ \n";
    showMenu();
    getData();
    int choice;
    char add;
    
    do {
    cout << "Enter choice: ";
    cin >> choice;
    
        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();
}

}
while (add == 'Y'|| add == 'y');

}


void getData(){
    
    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";
}

double total = 0;

int printCheck(){
    
    getData();
    double total = 0 , tax, totalbill;
    
    for (int i = 0; i < 8; i++){
        
    
        total += menulist[i].menuPrice;
        
    }   
    
    
    
    tax = total * 0.05; 
    totalbill = total+tax; 
    
        cout << "----------------------------------------------\n";
       cout << "Tax\t\t" << tax<< endl;
       cout << "Amount Due\tPhp" << totalbill << endl;
       cout << "----------------------------------------------\n";
    
    return total;
}
  • 3
    Please read [How to debug small programs](https://ericlippert.com/2014/03/05/how-to-debug-small-programs/) and provide debugging details –  Jul 14 '21 at 16:45
  • 4
    From what I can tell, your `printCheck` will always assume you bought exactly one of every item on the menu. You need to actually save which specific entries your customer ordered so you can recall that information later--right now you print something like `"You ordered Plain Egg."` to the console and then immediately forget that the user ordered an egg. – Nathan Pierson Jul 14 '21 at 16:50
  • 1. Read about `enum class`. 2) Use a `std::vector` as the check. 3) Use `std::map` or a function to convert `enum class` items to their prices. – Thomas Matthews Jul 14 '21 at 16:53
  • 1
    less severe of a problem, it makes no sense to call `getData` inside of `printCheck`. `getData` initializes the menu. You've already done that at the beginning, no need to do it again. – yano Jul 14 '21 at 16:53
  • Suggestion: You have `menuItemType::menuItem` as a name for all these things, and then you... don't actually use it anywhere. You have to redundantly specify in `main`, `getData`, and `showMenu` that the first menu item is called `"Plain Egg"`, the second is called `"Bacon and Egg"`, etc. – Nathan Pierson Jul 14 '21 at 16:54
  • @ThomasMatthews In this case I think the `menuItemType` used is actually more appropriate than an `enum class`. Bundling together the name and price of a menu item makes sense. It's just not really being used in this code. – Nathan Pierson Jul 14 '21 at 16:55
  • It seems like you've learned about structs/classes, so there's little excuse to use global variables. There are 2 `total` variables: one in `printCheck` and one in the namespace scope. In this case the one in namespace scope doesn't seem to be used, but it's easily possible to get this wrong. It's much harder to reason about the lifecycle of a variable at namespace scope than it's the case for variables at class scope. Also it's best to avoid repeating information: the price/item name are encoded both in the `showMenu` and the `getData` function so any change needs to happen in 2 places... – fabian Jul 14 '21 at 16:57
  • also recommend getting rid of the `if (add == 'N'||add =='n'){ printCheck(); }` block. Simply loop while the user enters `'Y'` or `'y'` (_saving_ each choice), and when that condition no longer holds, the loop will break and you can unconditionally call `printCheck`. – yano Jul 14 '21 at 17:01
  • You didn't ask a specific question, which makes it hard to give you an answer. Do you understand that you have no code to keep track of what was ordered? If so, what issue are you having adding code to do that? – David Schwartz Jul 14 '21 at 17:05

1 Answers1

1

Your code is a bit all over the place, with some small reorganization you can get the intended result, here is a possible fix, with comments where needed:

Live sample

#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) // if you want to separate logic from UI, this could 
                        // be a separate function, i.e. printChoice(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') // this condition is also refactorable, you could  
                                      // simply move the print to after the loop ends
        {
            printCheck(total);
        }

    } while (add == 'Y' || add == 'y');

    //printCheck(total); //here
}
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
}

For more information whether you should use using namespace std; check this link:

Why is "using namespace std;" considered bad practice?

Note that the code still have some weaknesses, namely the lack of input validation, if, as an example, your input is not a number when you are prompted to give a choice you have a problem. If you want to learn more about this there are many threads in SO you can find, for instance:

Good input validation loop using cin - C++

anastaciu
  • 23,467
  • 7
  • 28
  • 53
  • 1
    @user15765250 I'm glad I could help. Good luck. Just added a check to the sum routine to avoid getting values out of range, make sure you add that also. – anastaciu Jul 14 '21 at 18:19