fork download
  1. // Test - Display inventory
  2.  
  3. #include <iostream>
  4. #include <string>
  5. #include <vector>
  6.  
  7. using namespace std;
  8.  
  9. class Invent {
  10. public:
  11. void Invent::displayInventory();
  12. void Invent::addItem(string item);
  13. void Invent::removeItem(string item);
  14. private:
  15. vector<string> Inventory;
  16. vector<string>::const_iterator iter;
  17. };
  18.  
  19. int main() {
  20.  
  21. Invent invent;
  22. unsigned int userChoice;
  23. bool quit = false;
  24.  
  25. cout << "\nWelcome to your item inventory" << endl;
  26.  
  27. while (quit == false) {
  28. cout << "\n1. Display inventory" << endl;
  29. cout << "2. Add item" << endl;
  30. cout << "3. Remove item" << endl;
  31. cout << "4. Quit\n" << endl;
  32. cout << "Option: ";
  33. cin >> userChoice;
  34.  
  35. string item = "";
  36.  
  37. switch (userChoice) {
  38. case 1:
  39. invent.displayInventory();
  40. break;
  41. case 2:
  42. cout << "\nPlease type the item name: ";
  43. cin.get();
  44. getline(cin, item);
  45. invent.addItem(item);
  46. break;
  47. case 3:
  48. cout << "\nPlease type the item name to remove: ";
  49. cin.get();
  50. getline(cin, item);
  51. invent.removeItem(item);
  52. break;
  53. case 4:
  54. quit = true;
  55. break;
  56. default:
  57. cout << "\nYou entered an incorrect value!\n";
  58. break;
  59. }
  60. }
  61.  
  62. return 0;
  63. }
  64.  
  65. void Invent::displayInventory() {
  66. if (Inventory.empty()) {
  67. cout << "\nSorry but your inventory is currently empty.\n";
  68. }
  69. else {
  70. cout << "\nDisplaying all the current items in your inventory.\n" << endl;
  71. for (iter = Inventory.begin(); iter != Inventory.end(); ++iter) {
  72. cout << "- " << *iter << endl;
  73. }
  74. }
  75. }
  76.  
  77. void Invent::addItem(string item) {
  78. Inventory.push_back(item);
  79. cout << "\nThe item \"" << item << "\" was successfully added to your inventory." << endl;
  80. }
  81.  
  82. void Invent::removeItem(string item) {
  83. iter = find(Inventory.begin(), Inventory.end(), item);
  84. if (iter != Inventory.end()) {
  85. Inventory.erase(item); // ERROR something about overloaded op
  86. }
  87. }
  88.  
Compilation error #stdin compilation error #stdout 0s 0KB
stdin
Standard input is empty
compilation info
prog.cpp:11:7: error: extra qualification ‘Invent::’ on member ‘displayInventory’ [-fpermissive]
  void Invent::displayInventory();
       ^
prog.cpp:12:7: error: extra qualification ‘Invent::’ on member ‘addItem’ [-fpermissive]
  void Invent::addItem(string item);
       ^
prog.cpp:13:7: error: extra qualification ‘Invent::’ on member ‘removeItem’ [-fpermissive]
  void Invent::removeItem(string item);
       ^
prog.cpp: In member function ‘void Invent::removeItem(std::string)’:
prog.cpp:83:54: error: no matching function for call to ‘find(std::vector<std::basic_string<char> >::iterator, std::vector<std::basic_string<char> >::iterator, std::string&)’
  iter = find(Inventory.begin(), Inventory.end(), item);
                                                      ^
prog.cpp:83:54: note: candidate is:
In file included from /usr/include/c++/4.8/bits/locale_facets.h:48:0,
                 from /usr/include/c++/4.8/bits/basic_ios.h:37,
                 from /usr/include/c++/4.8/ios:44,
                 from /usr/include/c++/4.8/ostream:38,
                 from /usr/include/c++/4.8/iostream:39,
                 from prog.cpp:3:
/usr/include/c++/4.8/bits/streambuf_iterator.h:369:5: note: template<class _CharT2> typename __gnu_cxx::__enable_if<std::__is_char<_CharT2>::__value, std::istreambuf_iterator<_CharT> >::__type std::find(std::istreambuf_iterator<_CharT>, std::istreambuf_iterator<_CharT>, const _CharT2&)
     find(istreambuf_iterator<_CharT> __first,
     ^
/usr/include/c++/4.8/bits/streambuf_iterator.h:369:5: note:   template argument deduction/substitution failed:
prog.cpp:83:54: note:   ‘__gnu_cxx::__normal_iterator<std::basic_string<char>*, std::vector<std::basic_string<char> > >’ is not derived from ‘std::istreambuf_iterator<_CharT>’
  iter = find(Inventory.begin(), Inventory.end(), item);
                                                      ^
prog.cpp:85:23: error: no matching function for call to ‘std::vector<std::basic_string<char> >::erase(std::string&)’
   Inventory.erase(item); // ERROR something about overloaded op
                       ^
prog.cpp:85:23: note: candidates are:
In file included from /usr/include/c++/4.8/vector:69:0,
                 from prog.cpp:5:
/usr/include/c++/4.8/bits/vector.tcc:134:5: note: std::vector<_Tp, _Alloc>::iterator std::vector<_Tp, _Alloc>::erase(std::vector<_Tp, _Alloc>::iterator) [with _Tp = std::basic_string<char>; _Alloc = std::allocator<std::basic_string<char> >; std::vector<_Tp, _Alloc>::iterator = __gnu_cxx::__normal_iterator<std::basic_string<char>*, std::vector<std::basic_string<char> > >; typename std::_Vector_base<_Tp, _Alloc>::pointer = std::basic_string<char>*]
     vector<_Tp, _Alloc>::
     ^
/usr/include/c++/4.8/bits/vector.tcc:134:5: note:   no known conversion for argument 1 from ‘std::string {aka std::basic_string<char>}’ to ‘std::vector<std::basic_string<char> >::iterator {aka __gnu_cxx::__normal_iterator<std::basic_string<char>*, std::vector<std::basic_string<char> > >}’
/usr/include/c++/4.8/bits/vector.tcc:146:5: note: std::vector<_Tp, _Alloc>::iterator std::vector<_Tp, _Alloc>::erase(std::vector<_Tp, _Alloc>::iterator, std::vector<_Tp, _Alloc>::iterator) [with _Tp = std::basic_string<char>; _Alloc = std::allocator<std::basic_string<char> >; std::vector<_Tp, _Alloc>::iterator = __gnu_cxx::__normal_iterator<std::basic_string<char>*, std::vector<std::basic_string<char> > >; typename std::_Vector_base<_Tp, _Alloc>::pointer = std::basic_string<char>*]
     vector<_Tp, _Alloc>::
     ^
/usr/include/c++/4.8/bits/vector.tcc:146:5: note:   candidate expects 2 arguments, 1 provided
stdout
Standard output is empty