I'm trying to store the contents of a file containing names of herbs and spices (items) in a 2D array called PantryContents. Then I'm trying to add an item to that array. It seems like only one item is being stored at a time, but the output is correct but when I check the content of PantryContents, only one item shows up (like it's not 2D).
This will be split into functions eventually.. (that's why the format is a little funky), I just needed to make it work all together first. Any ideas?

This is what's in the file "Pantry.txt":
-ground cumin
-cayenne pepper
-salt
-pepper
-dill
-ground cinnamon
-garlic powder

#include <iostream>
#include <fstream>

using std::cin;
using std::cout;
using std::ifstream;

int main()
{
    const int size = 100;
    ifstream data_file ( "Pantry.txt" );
    char items[size];
    char ** PantryContents = nullptr;
    int ** frequencyArray = nullptr;
    int num_items = 0;

    if ( data_file.is_open () )
    {
        while ( !data_file.eof() )
        {
            data_file.getline( items, size ); //reads file

            num_items++;

            //cout << num_items << items << '\n'; //displays contents

            PantryContents = new char * [num_items + 1]; //increase array by one?

            for ( int i = 0 ; i < num_items; i++)
            {
                PantryContents[i] = &items[i]; //stick items in PantryContents array
            }

            PantryContents[num_items] = new char [strlen(items) + 1]; //new spot has enough space for the new items
            strcpy(PantryContents[num_items], items); //copy the items into the PantryContents
            num_items++; //increase the number

            cout << *PantryContents <<'\n';

        }

        data_file.close();

    }


    char ** temp = 0;
    char addition[255];
    char confirm = 'Y';
    int num = 0;

    while (confirm == 'Y' || confirm == 'y')
    {
        cout << "Please enter item: ";
        cin >> addition;

        PantryContents = new char * [num_items + 1];

        for (int i = 0; i < num; i++)
        {
            PantryContents[i] = temp[i];
        }

        PantryContents[num_items] = new char[strlen(addition) + 1];
        strcpy(PantryContents[num_items], addition);
        num_items++;

        delete[] temp;
        temp = PantryContents;


        cout << "\nWould you like to enter another item? Y/N :";
        cin >> confirm;

        for (int i = 0; i < num_items; i++)
        {
            cout << *PantryContents << '\n';
        }
    }

    for (int i = 0; i < num_items; i++)
    {
        delete[] PantryContents[i];
    }

    delete[] PantryContents;

    return 0;
}

Recommended Answers

All 2 Replies

For what you're doing string and vector would be much easier to use than creating arrays out of raw memory:

#include <iostream>
#include <fstream>
#include <string>
#include <vector>

using std::string;
using std::vector;
using std::cin;
using std::cout;
using std::ifstream;
int main()
{
    const int size = 100;
    ifstream data_file ( "Text.txt" );
    string items;
    vector<string> PantryContents;
    vector<int> frequencyArray;
    int num_items = 0;
    if ( data_file.is_open () )
    {
        while ( !data_file.eof() )
        {
            getline(data_file, items); //reads file
            PantryContents.push_back(items); //stick items in PantryContents array
        }
        data_file.close();
        for(auto str : PantryContents)
            cout << str <<'\n';
    }
    string addition;
    char confirm = 'Y';
    int num = 0;
    while (confirm == 'Y' || confirm == 'y')
    {
        cout << "Please enter item: ";
        getline(cin, addition);
        PantryContents.push_back(addition);
        cout << "\nWould you like to enter another item? Y/N :";
        cin >> confirm;
        cin.ignore();
        for(auto str : PantryContents)
            cout << str <<'\n';
    }
    return 0;
}

What I have included in this file is all I can use.. I haven't "learned" those include statements yet, so I have to work with arrays.

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.