According the exercise, which makes us practice with vectors, i am supposed to write a program that allows the user to enter his/her favorite games, view them and remove any he likes. Obviously, to remove a piece of text, it has to be of the same case as the one entered by the user. So i had to use transform() from the algorithm include file to convert it.
Unfortunately, there seems to be a transform() in the vector include file too. And by the looks of it, my compiler is confusing the two with each other. Error generated is at the bottom. Heres the code i got:

#include <iostream>
#include <string>
#include <vector>
#include <algorithm>

using namespace std;

int main()
{
    vector<string> gameName;
    int choice = 0;
    string game = " ";
    vector<string>::iterator myIterator;
    vector<string>::const_iterator iter;

    cout<<"This allows you to maintain your favorate games \n to get started, press enter."<<endl;
    cin.get();
    cout<<"What do you want to do?"<<endl;
    cout<<"1: Add a game \n 2: Remove a game \n 3: View games \n 4: Exit"<<endl;
    cin>>choice;
    while(choice >= 1 && choice < 4){
        switch(choice){
            case 1:
                cout<<"Enter a new favorate game: "<<endl;
                getline(cin, game);
                transform(game.begin(), game.end(), game.begin(), tolower);
                gameName.push_back(game);
                break;

            case 2:
                cout<<"Enter the name of the game you wish to remove:";
                getline(cin, game);
                transform(game.begin(), game.end(), game.begin(), tolower);
                for(myIterator = gameName.begin(); myIterator !=  gameName.end(); myIterator++){
                    if(*myIterator == game){
                        gameName.erase(*myIterator);
                    }else{
                        cout<<"Invalid name; try again later"<<endl;
                    }
                }
                break;

            case 3:
                for(iter=gameName.begin(); iter != gameName.end(); iter++){
                    cout<<*iter<<endl;
                }
                break;
        }
        cout<<"What do you want to do?"<<endl;
        cout<<"1: Add a game \n 2: Remove a game \n 3: View games \n 4: Exit"<<endl;
        cin>>choice;
    }
    cin.get();
}
||=== this, Debug ===|
C:\~~\main.cpp||In function `int main()':|
C:\~~\main.cpp|26|error: no matching function for call to `transform(__gnu_cxx::__normal_iterator<char*, std::basic_string<char, std::char_traits<char>, std::allocator<char> > >, __gnu_cxx::__normal_iterator<char*, std::basic_string<char, std::char_traits<char>, std::allocator<char> > >, __gnu_cxx::__normal_iterator<char*, std::basic_string<char, std::char_traits<char>, std::allocator<char> > >, <unknown type>)'|
C:\~~\main.cpp|33|error: no matching function for call to `transform(__gnu_cxx::__normal_iterator<char*, std::basic_string<char, std::char_traits<char>, std::allocator<char> > >, __gnu_cxx::__normal_iterator<char*, std::basic_string<char, std::char_traits<char>, std::allocator<char> > >, __gnu_cxx::__normal_iterator<char*, std::basic_string<char, std::char_traits<char>, std::allocator<char> > >, <unknown type>)'|
C:\~~\main.cpp|36|error: no matching function for call to `std::vector<std::string, std::allocator<std::string> >::erase(std::basic_string<char, std::char_traits<char>, std::allocator<char> >&)'|
F:\Program Files\CodeBlocks\MinGW\bin\..\lib\gcc\mingw32\3.4.5\..\..\..\..\include\c++\3.4.5\bits\vector.tcc|108|note: candidates are: typename std::vector<_Tp, _Alloc>::iterator std::vector<_Tp, _Alloc>::erase(__gnu_cxx::__normal_iterator<typename _Alloc::pointer, std::vector<_Tp, _Alloc> >) [with _Tp = std::string, _Alloc = std::allocator<std::string>]|
F:\Program Files\CodeBlocks\MinGW\bin\..\lib\gcc\mingw32\3.4.5\..\..\..\..\include\c++\3.4.5\bits\vector.tcc|120|note:                 typename std::vector<_Tp, _Alloc>::iterator std::vector<_Tp, _Alloc>::erase(__gnu_cxx::__normal_iterator<typename _Alloc::pointer, std::vector<_Tp, _Alloc> >, __gnu_cxx::__normal_iterator<typename _Alloc::pointer, std::vector<_Tp, _Alloc> >) [with _Tp = std::string, _Alloc = std::allocator<std::string>]|
||=== Build finished: 3 errors, 0 warnings ===|

This gets rid of the error in visual studio 2008, but it doesn't mean
your program works. Try it :

#include <iostream>
#include <string>
#include <vector>
#include <algorithm>

using namespace std;

int main()
{
    vector<string> gameName;
    int choice = 0;
    string game = " ";
    vector<string>::iterator myIterator;
    vector<string>::const_iterator iter;

    cout<<"This allows you to maintain your favorate games \n to get started, press enter."<<endl;
    cin.get();
    cout<<"What do you want to do?"<<endl;
    cout<<"1: Add a game \n 2: Remove a game \n 3: View games \n 4: Exit"<<endl;
    cin>>choice;
	

    while(choice >= 1 && choice < 4){
        switch(choice){
            case 1:
                cout<<"Enter a new favorate game: "<<endl;
                getline(cin, game);
                transform(game.begin(), game.end(), game.begin(), tolower);
                gameName.push_back(game);
                break;

            case 2:
                cout<<"Enter the name of the game you wish to remove:";
                getline(cin, game);
                transform(game.begin(), game.end(), game.begin(), tolower);
                for(myIterator = gameName.begin(); myIterator !=  gameName.end(); myIterator++){
                    if(*myIterator == game){
						gameName.erase(myIterator);
                    }else{
                        cout<<"Invalid name; try again later"<<endl;
                    }
                }
                break;

            case 3:
                for(iter=gameName.begin(); iter != gameName.end(); iter++){
                    cout<<*iter<<endl;
                }
                break;
        }
        cout<<"What do you want to do?"<<endl;
        cout<<"1: Add a game \n 2: Remove a game \n 3: View games \n 4: Exit"<<endl;
        cin>>choice;
    }
    cin.get();
}

Ok the original post was about gcc, which has a tolower function that
does not return a char, it returns an int and takes an int. That is because it works with internationalized character sets. So you do a functional composite to convert the form both ways or just write a quick function like this (you can use a functor if you prefer).

char charLower(const char& P)
{ 
     return static_cast<char>(tolower(P));
}

transform(A.begin(),A.end(),A.begin(),charLower);

Additionally you do also need to remove the pointer redirection (the *) in the erase line gameName.erase(*myIterator); as pointed out by firstPerson. And once that is working you can start debugging.... Enjoy.

Edited 7 Years Ago by StuXYZ: n/a

Did both. Added the function StuXyz gave at the beginning of my main.cpp.

It still doesn't work and no difference :(

This article has been dead for over six months. Start a new discussion instead.