basicly my project is making a shopping list which allow the user to:

  • type 1 to add a new shopping list
  • type 2 to add an item on existing shopping list
  • type 3 to check certain items in a certain list as bought
  • type 4 to display a shopping list on certain date
  • type 5 to exit

i made some research and i learned how to put every list the user enter into a file but i don't know how to display the lists names and let the user choose a certain list to add to it a new item

that's what i have so far

#include <iostream>
#include <string>
#include <fstream>                       //item name, quantity for each item, checked, date to buy the list, name of store 
using namespace std;

void makelist();
void addtolist();
void checkitem();           // still don't know how to write it
void displaylist();         // still don't know how to write it 

struct list
{
    int quant;
    string items;
    string place;
    string date;
};

int main()
{
    int a;
    cout << "                        *Welcome to your shopping list*" << endl;
    cout << "please enter: " << endl;
    do
    {
        cout << "     (1) If you want to make a list." << endl;
        cout << "     (2) If you want to add an item to an existing list." << endl;
        cout << "     (3) If you want to check a certain item as bought" << endl;
        cout << "     (4) If you want to diplay a certaina shopping list in a certain date" << endl;
        cout << "     (5) If you want to exit" << endl;

        cin >> a;
        switch (a)
        {
        case 1:
            makelist();
            break;
        case 2:
            addtolist();
            break;
            /*case 3:
            checkitem();
            break;
            case 4:
            displaylist();
            break;
            */case 5:
                cout << "...CLOSING...";
                return 0;
            default:
                cout << "..ERROR..plz enter numbers from 1 to 5";
                break;
        }
    } while (a != 5);

    return 0;
}


void makelist()
{
    string FileName;
    cout << "enter the list name: ";
    cin >> FileName;
    ofstream WriteToFile;
    WriteToFile.open(FileName);
    list L;
    int n;
    cout << "Enter number of items:" << endl;
    cin >> n;
    for (int i = 0; i <= n; i++)
    {
        cout << "Enter items and quantity:" << endl;
        cin >> L.items >> L.quant;
        WriteToFile << L.items << L.quant;
    }
    cout << "Enter the store's name:" << endl;
    cin >> L.place;
    WriteToFile << L.place;
    cout << "Enter date:" << endl;
    cin >> L.date;
    WriteToFile << L.date;
}

void addtolist()
{
    cout << "ur lists:" << endl;     // and display the saved lists for the user to choose
    ifstream readfromfile;
    if (readfromfile.is_open())
    {
        makelist();              //the user adding new item
    }
    else
        cout << "ERROR opening the file" << endl;
}

/*void checkitem()          // function for displaying list from file for the user to chech them which i don't know how to do it
{}

void displaylist()          // function displaying list from file which i also don't know how 
{}*/

ps:i only know loops, array, functions, structs that's only my knowledge so far
i don't know anything except the above so i will need a full explanation i barely know how to write to a file.

and that's a sample of the supposed result
sample_1.jpgsample_2.jpg

thanks in advance.

Recommended Answers

All 4 Replies

Look, this is your project and here at DaniWeb we are here to help you learn not complete your assignments or projects.
I could guide you to such people if you want a short way out,C++ Projects, and if you want a discount tell'em mach-25 sent you.

If you require help in any topic related to C++ or programming, we'll be more than happy to help you.

CSprogA: While I would have worded it rather differently, I have to agree with Hassan_12 on this. You would have to tell us what help you need, and ask meaningful questions about how you can solve the problems you are having, before we can have any hope of helping you in a way that wouldn't constitute a hand-out. While I am pretty sure that you honestly did come for advice rather than a quick fix, given some of what you said, it did come off as begging rather than a serious request. Try to explain what the problems you are having in more detail, if nothing else.

Schol-R-LEA Hassan_12 i don't need anyone to do my homework for me and i'm offended by the fact that you understood it that way i thought i should put my code and a sample of the supposed result so anyone could help would be able to understand the whole thing not be confused about my question or think that i need a 'quick fix' and about my ps it's because when i usually ask people on the internet they tell me to include libraries and other stuff without saying what these stuff are and of course i won't write a line of code that i don't understand that's why i said "i need a full explanation of what you're gonna help me with"
i included everything so that anyone would understand what exactly i'm talking about not the opposite

i'm not asking anyone to write a code for me to complete my project
that's simply my question in another words:
i know how to write and read from a file but how can i display the names of files that the user had entered?

I apologize for having hurt your feelings; as I said, I was pretty sure you hadn't meant it that way, but it can be hard to tell sometimes. We do get a lot of people who are asking for their work to be done for them, so there's a bit of a tendency to assume the worst. I am sorry for doing that to you.

As for displaying the names of the files... well, we'd need more information about just what you are trying to do. If it were simply a matter of displaying the names of the files used during the current run of the program, it would be very simple: you could simply keep a vector<string> of the file names that have been entered, and print the strings in a loop (vector is a template class which acts as an array that can automatically grow or shrink as you need it to). However, I am assuming you want to be able to get the files that the program has previously generated, which presents a few problems.

First off, you would have to know where the files in question are, either by checking the current working path, or by having some directory where you can look for them, or by letting the user choose where they should be stored. You would also want to have some way of telling which files are the data files for this program, which usually means picking a file extension for them.

But that's not the real problem. The real problem is that you would need to get a directory listing, and that is system-dependent. There is no standard library for directory (folder) listings, so we would have to know whether you are using Windows, MacOS, Linux, etc. before we could give any advice.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.