0

I have created a menu system for a restaurant. Using arrays, I have created a system for adding new items to the menu and having them printed in a text file. This all works fine.

The issue arises when I need to edit an item, for example, if I have added 3 dishes and use the edit option, only the first line of the text file is edited and the item number returns to 0 when the minimum should always be 1.

How can I make this work so if I edit item 3, the edit will be displayed in the text file on line 3 instead of overwriting line 1?

I am not sure where the issue lies so I am attaching all the project files as it is not too long.

Thank you in advance

The .h file:

 #ifndef RESTAURANTMENU_ITEMS_H
#define RESTAURANTMENU_ITEMS_H

#define MAX_ITEMS 20
#include <iostream>
#include <string>
#include <limits>
#include <fstream>


class Item{
private:
    int itemNumber;
    std::string itemCategory;
    std::string itemDescription;
    double itemPrice;

public:
    Item();

    //setter function
    void setItemDetails();
    void editItemDetails();

    //getter function
    void printItemDetails();

    //save to file
    void save(std::ofstream &outfile);
};
#endif //RESTAURANTMENU_ITEMS_H

The implementation file:

    #include "item.h"

//constructor
Item::Item(){
    itemNumber = 0;
    itemCategory = "Item not categorised.";
    itemDescription = "No description written.";
    itemPrice = 0.0;
}

//setter functions
void Item::setItemDetails() {
    int choice;

    static int counter = 1;
    itemNumber = counter++;

    std::string copyCategory;
    std::cout << "What category is this item?" << std::endl;
    std::cout << "1. Meat Dish\n2. Fish Dish\n"
                 "3. Vegetarian Dish\n4. Drink\n";
    std::cin >> choice;
    switch (choice) {
        case 1:
            itemCategory = "Meat";
            break;
        case 2:
            itemCategory = "Fish";
            break;
        case 3:
            itemCategory = "Vegetarian";
            break;
        case 4:
            itemCategory = "Drink";
            break;
        default:
            std::cout << "Error. Please enter a number between 1-4: " << std::endl;
            std::cin >> choice;
        }

            std::cout << "Please enter a short description: " << std::endl;
            std::cin.ignore(100, '\n');
            std::getline(std::cin, itemDescription);

            std::cout << "Please set the price: " << std::endl;
            std::cout << "£";
            if (!(std::cin >> itemPrice)) {
                std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
                std::cout << "Error. Please enter a number: ";
            }
}

void Item::editItemDetails() {
    int choice;

    std::string copyCategory;
    std::cout << "What category is this item?" << std::endl;
    std::cout << "1. Meat Dish\n2. Fish Dish\n"
                 "3. Vegetarian Dish\n4. Drink\n";
    std::cin >> choice;
    switch (choice) {
        case 1:
            itemCategory = "Meat";
            break;
        case 2:
            itemCategory = "Fish";
            break;
        case 3:
            itemCategory = "Vegetarian";
            break;
        case 4:
            itemCategory = "Drink";
            break;
        default:
            std::cout << "Error. Please enter a number between 1-4: " << std::endl;
            std::cin >> choice;
    }

    std::cout << "Please enter a short description: " << std::endl;
    std::cin >> itemDescription;
    //std::cin.ignore(100, '\n');
    //std::getline(std::cin, itemDescription);

    std::cout << "Please set the price: " << std::endl;
    std::cout << "£";
    //if (!(std::cin >> itemPrice)) {
        //std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
        //std::cout << "Error. Please enter a number: ";
        std::cin >> itemPrice;
    //}
}

//getter functions
void Item::printItemDetails(){
    std::cout << "\nItem Number: " << itemNumber << "| Category: "
    << itemCategory << "| Description: " << itemDescription << "| Price: £" << itemPrice;
}

//save to file
void Item::save(std::ofstream &outfile) {
    outfile << "\nItem Number: " << itemNumber << "| Category: "
              << itemCategory << "| Description: " << itemDescription << "| Price: £" << itemPrice;
}

main.cpp

    #include <iostream>
#include "item.h"

int main() {
    Item newDish[MAX_ITEMS];
    bool exit = false;
    int choice;
    int count = 0;
    std::ofstream saveFile;
    saveFile.open("menu.txt", std::ios::in | std::ios::out);

    while (!exit) {
        std::cout << "Menu Creation Terminal\n\n" << std::endl;
        std::cout << "\tWelcome to Wrapid™ Restaurants\n\n\t\tMenu Creation Terminal\n\n" << std::endl;
        std::cout << "1. Add a new dish\n2. Edit Current Menu\n3. Quit\n" << std::endl;
        std::cout << "Please select an option: ";
        std::cin >> choice;

        switch (choice) {
            case 1: {
                int option = true;
                int i;


                //create items
                std::cout << "Item Creation Menu";
                for (i = 0; i < MAX_ITEMS; i++) {
                    count += 1;
                    std::cout << "\n\nItem number: " << i+1 << "\n\n";
                    newDish[i].setItemDetails();
                    newDish[i].save(saveFile);


                    std::cout << "Would you like to add another item?" << std::endl;
                    std::cout << "1. Yes\n2. No" << std::endl;
                    std::cin >> option;
                    if (option == 2) {
                        break;
                    }
                    std::cout << "You have added the following items: " << std::endl;
                    newDish[i].printItemDetails();
                }
            }
                break;
            case 2: {
                int editOpt;
                int i;

                //edit items
                std::cout << "Edit Current Menu\n\n" << std::endl;
                for (i = 0; i < count; i++) {
                    newDish[i].printItemDetails();
                }
                std::cout << "\n\nPlease enter the item number of the item you would like to edit: " << std::endl;
                std::cin >> editOpt;
                while(editOpt > 20) { std::cout << "Error. Limited to 20 items.\n"
                                                   "Please try again: "; std::cin >> editOpt; }
                i = editOpt-1;
                newDish[i].editItemDetails();
                newDish[i].save(saveFile);
            }
                break;
            case 3: {
                std::cout << "Thanks for using this terminal. Have a nice day." << std::endl;
                exit = true;
            }
            break;
            default: {
                std::cout << "Error. Invalid selection. Please select a valid option: " << std::endl;
                std::cin >> choice;
            }
        }
    }
    saveFile.close();
    return 0;
}
4
  • And when you used your debugger to run your program, what did you see? This is what a debugger is for. If you don't know how to use a debugger this is a good opportunity to learn how to use it to run your program one line at a time, monitor all variables and their values as they change, and analyse your program's logical execution flow. Knowing how to use a debugger is a required skill for every C++ developer, no exceptions. With your debugger's help you should be able to quickly find all bugs in this and all future programs you write, without having to ask anyone for help. Apr 10, 2020 at 12:33
  • Hi there, no warnings or errors are shown.
    – user11452330
    Apr 10, 2020 at 12:33
  • Right. When there are "no warnings or errors", and the problem still doesn't work correctly, that's exactly what a debugger is for. Just because your program compiles with "no warnings or errors" doesn't mean it will work right. C++ does not work this way. This is why you use a debugger, to observe exactly what happens and how it diverges from your expected behavior. There's never any need to ask anyone else why your program doesn't work. You can always figure it out using your debugger. That's exactly it's job. Apr 10, 2020 at 12:35
  • The program works as expected while in the terminal, editing items and all. The issue is copying those edits into the text file.
    – user11452330
    Apr 10, 2020 at 12:37

1 Answer 1

0

change this line in main because everytime you are overwriting the file so always open a file in append mode

std::ofstream saveFile;
saveFile.open("menu.txt", std::ios::in | std::ios::out | std::ios_base::app);

Hopefully that will solve your problem

3
  • Hi! thanks for your response. I'd like to overwrite the lines that have been edited in the program rather than appending them as there should be a limit of only 20 items and with appending there will be redundant data within the text file
    – user11452330
    Apr 10, 2020 at 13:01
  • The problem was you were overwriting every-time first you need to open the file in append mode. ok make git repo and will do that . is that fine? Apr 10, 2020 at 13:03
  • using std::ios::in | std::ios::out meant that the file wasn't being overwritten as its in read and write mode, or at least it looks that way on my computer as the file isn't cleared when I save data then run the program again.
    – user11452330
    Apr 10, 2020 at 13:06

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.