Search code examples
c++fstreamifstreamgetline

Issue reading line from file with getline()


I have my readFile() method set up so that my 'myList' vector stays updated, but when I run the program my vector is empty despite my addItem() method properly writing to the file.

I've tried looking around for issues regarding ifstream and getline but most of the solutions amount to workarounds where the number of items being read per line is known in advance.

If someone can tell me what I'm doing wrong I'd appreciate it.

#include <iostream>
#include <fstream>
#include <cstdlib>
#include <vector>

using namespace std;

void readFile(vector<string> myList);
void writeToFile(vector<string> myList);
void mainMenu();
void displayList(vector<string> myList);
void addItem(vector<string> myList);
void removeItem(vector<string> myList);

string fileName = "C:\\Users\\Owner\\Documents.todo_list.txt";

int main()
{
    vector<string> myList;

    int choice;

    do
    {
        readFile(myList);

        mainMenu();

        cin >> choice;

        cout << endl;

        switch(choice)
        {
            case 1:
            {
                displayList(myList);
                break;
            }
            case 2:
            {
                addItem(myList);
                break;
            }
            case 3:
            {
                removeItem(myList);
                break;
            }
            default:
            {

            }
        }

        cout << endl;
    } while (choice != 0);

    return 0;
}

void readFile(vector<string> myList)
{
    ifstream read(fileName.c_str(), ios::in);

    if(!read)
    {
        cout << "Could not open file." << endl;
        exit(1);
    }

    if (myList.size() > 0)
    {
        myList.clear();
    }
    else
    {
        string item;

        getline(read,item);

        myList.push_back(item);
    }

    read.close();
}

void writeToFile(vector<string> myList)
{
    ofstream rewrite(fileName.c_str(), ios::out);

    for (int i = 0; i < myList.size(); ++i)
    {
        rewrite << myList[i] << endl;
    }

    rewrite.close();
}

void mainMenu()
{
    cout << " ========== Main Menu ==========" << endl;
    cout << " 1. Display To-do List" << endl;
    cout << " 2. Add item to the To-do List" << endl;
    cout << " 3. Remove item from the To-do List" << endl;
    cout << "\n 0. Exit program" << endl;
    cout << ": ";
}

void displayList(vector<string> myList)
{
    cout << " ========== To-do List =========" << endl;

    if (myList.empty())
    {
        cout << "      ----- empty list -----" << endl;
    }
    else
    {
        for (int i = 0; i < myList.size(); ++i)
        {
            cout << " " << (i + 1) << ". " << myList[i] << endl;
        }
    }
}

void addItem(vector<string> myList)
{
    ofstream write(fileName.c_str(), ios::app);

    string item;

    cout << "Please enter an item to add to the list: ";

    cin.ignore();
    getline(cin, item);

    write << item << endl;

    write.close();
}

void removeItem(vector<string> myList)
{
    int item;

    displayList(myList);

    cout << "\nPlease select an item to delete: ";
    cin >> item;

    cout << endl;
    cout << "...removing \"" << myList[item - 1] << "\"" << endl;

    myList.erase(myList.begin() + (item - 1));

    writeToFile(myList);
}

Solution

  • The parameter of the readFile seems to be a reference:

    void readFile(vector<string>& myList)
    

    BTW, the current readFile just reads a single line.