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 ===|