-1

This post is a continuation on my post from yesterday: How to output a vector.

So I am in the process of writing a short program that will take a list of strings into a vector, export them to a file, view them before outputting them and lastly remove them if possible. I have managed to get all of the functions working properly except for case 3 (remove a book from the list). The errors I am getting in visual studio as follows:

1.) "No instance of the overloaded function "remove" matches the argument list. [LN 76]

2.) "'Remove': function does not take 2 arguments". [LN 76]

As you can probably tell, I am trying to remove by value instead of index. I am still learning here so be gentle, but why exactly am I getting these errors?

Here is my full code:

#include <iostream>
#include <cctype>
#include <ctype.h>
#include <algorithm>
#include <string>
#include <math.h>
#include <windows.h>
#include <iomanip>
#include <vector>
#include <iostream>
#include <sstream>
#include <fstream>
#include <istream>

// common namespace
using namespace std;

int main()
{

    int option;
    bool iWannaLeave = false;

    vector<string> bookCollection;
    string entryVal = " ";
    int anotherOption;
    do
    {
        cout << "Welcome to MyBook! - A text recorder framework application." << endl;
        cout << "-----------------------------------------------------------" << endl;
        cout << "Main Menu:" << endl;
        cout << "1 - Add a book to the collection." << endl;
        cout << "2 - Display all books currently in the collection." << endl;
        cout << "3 - Remove books from the collection." << endl;
        cout << "4 - Write stored collection to file." << endl;
        cout << "5 - Quit" << endl;
        cout << "Make your selection: ";
        cin >> option;
        cin.ignore();

        switch (option)
        {
        case 1:
        {
            bool wannaMoreBooks = false;
            // the next loop will execute at least one time so you could enter a book
            do
            {
                wannaMoreBooks = false;
                cout << "Add a book title to the collection: ";
                getline(cin, entryVal);
                bookCollection.push_back(entryVal);

                cout << "Would you like to enter another book?(1 - yes, 0 - no): ";

                cin >> anotherOption;
                cin.ignore();
                if (anotherOption == 1) wannaMoreBooks = true;
            } while (wannaMoreBooks == true);
        }
        break;

        case 2:
        {
            for (int i = 0; i < bookCollection.size(); i++)
                cout << bookCollection[i] << " | ";
            cout << endl;
            break;
        }

        case 3: 
        {
            string vecVal;
            cout << "Enter the value you would like to remove: " << endl;
            cin >> vecVal;
            bookCollection.erase(remove(bookCollection.begin(), vecVal), bookCollection.end());
        }
            // remove a book from the collection
            break;

        case 4:
        {
            ofstream fileOut("Collection.txt");
            fileOut << "Your MyBook Collection: [Begin] - | ";
            auto first = true;
            for (string x : bookCollection)
            {
                if (!first) { fileOut << " | "; }
                first = false;
                fileOut << x;
            }
            fileOut << " | - [End]" << endl;
            cout << "Collection.txt has been successfully written." << endl;
            break;
        }
        case 5:
        {
            //Nested IF to kill program properly
            int quitVar;
            cout << "Are you sure you want to exit the program?: ";

            cin >> quitVar;
            cin.ignore();

            if (quitVar == 1)
            {
                cout << "The program will now be terminated." << endl;
                iWannaLeave = true;
            }
            else if (quitVar == 0)  cout << "Returning to the main menu." << endl;
        }
        break;
        }
    } while (iWannaLeave == false);

    return 0;
}

I am aware that this is no where near perfect code so in addition to finding out why I am getting these errors I would also like some constructive criticism as to how I can improve.

Additionally: If I wanted to go about using functions in a header file as opposed to a switch, would I just move the case contents to a header file?

Thanks in advance! :)

Community
  • 1
  • 1
Change
  • 11
  • 3
  • 2
    You are not using the correct number of arguments. You should double check the [reference](http://en.cppreference.com/w/cpp/algorithm/remove). – NathanOliver Sep 28 '16 at 14:29
  • 2
    When you have fixed the functional problem with your code, you'll want to head over to [codereview.se] for comments on your style. Remember to read the introductory materials there in order to present it in the way that will attract the best answers. – Toby Speight Sep 28 '16 at 15:15

1 Answers1

0

Virtually all STL-functions take one or more pairs of iterators. Since you just pass begin, there is no viable overload. You need to call

remove(bookCollection.begin(), bookCollection.end(), vecVal)

It is always a good idea to check the reference, which typically also contains a basic usage example.

midor
  • 5,487
  • 2
  • 23
  • 52