Click here to Skip to main content
14,766,265 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am working the menu system with categories like drink, appetizer, entree and dessert using class and objects. I am having trouble output the specific purchased items form the menu (that is from the categories on the output/ cout flie reciept.
when i output it on the output file i get all the menu items instead of purchased items.
I need to output only the purchased items and i am stuck at how do i exctly edit my outfile code for loop to output it

app.txt - input file
nachos
2.5
rolls
2.5


same way other txt file

What I have tried:

#include <string>
#include <iostream>
#include<stdlib.h>
#include<fstream>
using namespace std;

class Menu {
private:

public:

    int choice = 0;
    int output()
    {
        system("cls");
        cout << '\n';
        cout << "                     Welcome to Baxies Chicken !                        " << endl;
        cout << '\n' << " Enter your selection below -" << endl;
        cout << '\n' << ".............................Menu.............................. " << endl;
        cout << '\n' << " 1. Appetizer ";
        cout << '\n' << " 2. Drinks ";
        cout << '\n' << " 3. Entree ";
        cout << '\n' << " 4. Desserts ";

        cout << '\n' << " Tax of 10% on total price of order will be charged." << endl;

        cout << "Enter Selection (9 to exit): ";
        cin >> choice;
        return choice;
    }
};

class Category :public Menu
{
private:
    ifstream iFile;
    int cho = 0;
    string Name[100];
    double Price[100];
    int quantity = 0;

public:

    ofstream OutFile;
    void ofileOpen(string name)
    {
        OutFile.open(name);
    }
    void ofileCheck()
    {
        if (!OutFile.is_open())
        {
            cout << "input file not found" << endl;
            cin.get();
            exit(1);
        }
    }

    int counter = 0;
    double subtotalsec = 0; double subtotal = 0;
    void fileOpen(string name)
    {
        iFile.open(name);
    }
    void fileCheck()
    {
        if (!iFile.is_open())
        {
            cout << "input file not found" << endl;
            cin.get();
            exit(1);
        }
    }
    void getData(int& counter)
    {
        while (!iFile.eof())
        {
            getline(iFile, Name[counter]);
            iFile >> Price[counter];
            iFile.ignore();
            counter++;
        }
    }

    double output(int counter, string Text, ofstream& OutFile)
    {
        system("cls");

        cout << "*************** " << Text << " " << "Menu " << "***************" << endl;
        cout << '\n';

        cout << "Please enter your selection from this category!!!" << endl;
        cout << '\n';


        for (int a = 0; a < counter; a++)
        {
            cout << a + 1 << ": " << Name[a] << "     $" << Price[a] << endl;
        }

        do {
            cout << '\n';
            cout << "* -*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-" << endl;
            cout << "Enter Selection (99 to exit): ";
            cin >> cho;

            if (cho < 99 && cho > 0 && cho <= counter && cho != 99)
            {
                cout << "Enter Quantity: ";
                cin >> quantity;
                subtotalsec += Price[cho - 1] * quantity;

                for (int b = 0; b < counter; b++)
                {
                    OutFile << cho << "     " << Name[b] << "  @   " << quantity << "   $" << Price[b] << endl;
                }
            }

            else if (cho == 99)
                break;
        } while (choice != 9);
        return subtotalsec;
    }
};


class TotalOutput :public Category {
private:
    double Tax = 0.10;
    double tip = 0;
    double paid = 0;
    double total = 0;

public:
    double subtotal = 0;

    double receipt(double subtotala, double subtotald, double subtotale, double subtotals, ofstream& OutFile)
    {
        system("cls");
        cout << endl;
        cout << "**************------------------BAXIS CHICKEN------------------**************" << endl;
        cout << '\n';
        cout << "____________________******************Order Reciept****************____________________" << endl;
        cout << '\n' << endl;
        OutFile << "**************------------------BAXIS CHICKEN------------------**************" << endl;
        OutFile << '\n';
        OutFile << "____________________******************Order Reciept****************____________________" << endl;
        OutFile << '\n' << endl;

        { cout << "*******************************************************" << '\n';
        cout << endl;
        OutFile << "*****************************************************************" << '\n';


        subtotal = subtotala + subtotald + subtotale + subtotals;
        Tax *= subtotal;

        cout << " Subtotal: $" << subtotal << endl;
        OutFile << " Subtotal: $" << subtotal << endl;

        cout << " Tax: $" << Tax << endl;
        OutFile << " Tax: $" << Tax << endl;
        cout << " Enter Tip: $";
        cin >> tip;
        cout << '\n';
        OutFile << " Tip: $" << tip << endl;
        cout << " Tip: $" << tip << endl;
        cout << '\n';
        OutFile << '\n';

        total = Tax + subtotal + tip;
        cout << '\n';
        cout << "*--*--*--*--*--*--*--*--*--*--*--*--*--*--*--*--*--*--*--*--*--";
        cout << '\n';
        OutFile << "*--*--*--*--*--*--*--*--*--*--*--*--*--*--*--*--*--*--*--*--*--";
        OutFile << '\n';

        cout << " Total Bill: $" << total << endl;
        OutFile << " Total Bill: $" << total << endl;
        cout << '\n';
        cout << "*--*--*--*--*--*--*--*--*--*--*--*--*--*--*--*--*--*--*--*--*--";
        cout << '\n';
        OutFile << "*--*--*--*--*--*--*--*--*--*--*--*--*--*--*--*--*--*--*--*--*--";
        OutFile << '\n';

        cout << "Enter To Pay: $";
        cin >> paid;
        cout << '\n';
        OutFile << '\n';

        OutFile << " Paid: $" << paid;
        OutFile << '\n';

        cout << endl;

        if (paid >= total)
        {
            cout << "Change Due: $" << paid - total << endl;
            OutFile << "Change Due: $" << paid - total << endl;
        }
        else
        {
            while (paid <= total)
            {
                double AddPay;

                cout << "You need to pay more $" << total - paid << endl;
                OutFile << "You need to pay more $" << total - paid << endl;
                OutFile << '\n';
                cout << "Enter Payment Amount: $";
                cin >> AddPay;
                paid += AddPay;
                cout << " Total after paying again:  $" << paid << endl;
                OutFile << " Total after paying again:  $" << paid << endl;

                if (paid >= total)
                {

                    cout << "Change Due: $" << paid - total << endl;
                    OutFile << "Change Due: $" << paid - total << endl;
                    break;
                }
            }
        }
        }
        cout << '\n';
        OutFile << '\n';
        cout << "_____________*-*-*-*-*-Thank You For Your order*-*-*-*-*-_____________" << endl;
        cout << '\n';
        cout << "                 *-*-*-*-*-Enjoy Your Food !!!!*-*-*-*-*-                 " << endl;
        OutFile << "_____________*-*-*-*-*-Thank You For Your order*-*-*-*-*-_____________" << endl;
        OutFile << '\n';
        OutFile << "                 *-*-*-*-*-Enjoy Your Food !!!!*-*-*-*-*-                 " << endl;

        return subtotal;
    }
};


int main()
{
    Menu m;

    Category a;
    a.fileOpen("app.txt");
    a.fileCheck();
    a.getData(a.counter);

    Category d;
    d.fileOpen("drink.txt");
    d.fileCheck();
    d.getData(d.counter);

    Category e;
    e.fileOpen("entree.txt");
    e.fileCheck();
    e.getData(e.counter);

    Category s;
    s.fileOpen("des.txt");
    s.fileCheck();
    s.getData(s.counter);

    TotalOutput T;

    Category o;
    o.ofileOpen("OutFile1.txt");
    o.ofileCheck();
    do
    {
        m.choice = m.output();
        system("cls");

        switch (m.choice)
        {
            system("cls");
        case 1:
            a.subtotalsec = a.output(a.counter, " Appetizer ", o.OutFile);
            break;

        case 2:
            d.subtotalsec = d.output(d.counter, " Drink ", o.OutFile);
            break;

        case 3:
            e.subtotalsec = e.output(e.counter, " Entree ", o.OutFile);
            break;

        case 4:
            s.subtotalsec = s.output(s.counter, " Dessert ", o.OutFile);
            break;
        }

    } while (m.choice != 9);
    T.receipt(a.subtotalsec, d.subtotalsec, e.subtotalsec, s.subtotalsec, o.OutFile);

    return 0;
}
Posted
Updated 3-Dec-20 1:20am
v3

1 solution

with
OutFile << " Subtotal: $" << subtotal << endl;
and so on you are sending the string to the output file stream.
A better solution is build strings which you can send to out or write in an extra function to the file.

You better refurbish your code to make it more understandable. OutFile is a bad name for a variable and you have even a doublette as class member. exit(1); is also a bad idea.

Tip: improve your knowledge about class design and learn to use the debugger.
   

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900