Vector, iterators and char int's

Pages: 12
Hello! I'm quite new to programming but not a complete beginner, however i'm trying to make a program that let's you store game titles using vectors and iterators, but i get a problem.

Here's the code:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
#include <iostream>
#include <vector>
#include <string>
#include <algorithm>

using namespace std;

int main()
{
    int mode = 0;
    vector<string>list;
    list.reserve(20);
    
    vector<string>::iterator remove;
    vector<string>::const_iterator iter;
    while(mode != 4)
    cout << "\n\nWelcome to your list of games!";
    cout << "\nWhat would you like to do?";
    cout << "\nPress '1' to insert a game title.";
    cout << "\nPress '2' to remove a game title.";
    cout << "\nPress '3' to view your games."; 
    cout << "\nPress '4' to exit.";
    cin >> mode;
    if(mode == 1)
    {
        char title;
        const char add;
        cout << "Please type in the name of the game title that you would like to add(no spaces!)";        
        cin >> title;
        add = title;
        list.insert(list.begin(), add)
    }
    else if(mode == 2)
    {
        int pos;
        for(int i = 0; iter = list.begin(); iter != list.end(); ++iter; ++i)
        {
            cout << i << " ";
            cout << *iter <<endl;    
        }   
        cout << "Choose witch one to remove by the number behind them";
        cin >> pos;
        pos --;
        remove = list.begin(pos);
        list.erase((*remove));
    }
    else if(mode == 3;)
    {
       for(int i = 0; iter = list.begin(); iter != list.end(); ++iter; ++i)
        {
            cout << i << " ";
            cout << *iter <<endl;    
        }  
    }
    else if(mode == 4)
    {
        cout << "Later!"
        break;    
    }
    else
    {
        cout << "Please enter a valid number";    
    }
        
}

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
Compiler: Default compiler
Executing  g++.exe...
g++.exe "C:\Users\Alx101\Documents\C++\GameList.cpp" -o "C:\Users\Alx101\Documents\C++\GameList.exe"    -I"C:\Dev-Cpp\lib\gcc\mingw32\3.4.2\include"  -I"C:\Dev-Cpp\include\c++\3.4.2\backward"  -I"C:\Dev-Cpp\include\c++\3.4.2\mingw32"  -I"C:\Dev-Cpp\include\c++\3.4.2"  -I"C:\Dev-Cpp\include"   -L"C:\Dev-Cpp\lib" 
C:\Users\Alx101\Documents\C++\GameList.cpp: In function `int main()':
C:\Users\Alx101\Documents\C++\GameList.cpp:27: error: uninitialized const `add'

C:\Users\Alx101\Documents\C++\GameList.cpp:30: error: assignment of read-only variable `add'
C:\Users\Alx101\Documents\C++\GameList.cpp:31: error: invalid conversion from `const char' to `const char*'
C:\Users\Alx101\Documents\C++\GameList.cpp:31: error:   initializing argument 1 of `std::basic_string<_CharT, _Traits, _Alloc>::basic_string(const _CharT*, const _Alloc&) [with _CharT = char, _Traits = std::char_traits<char>, _Alloc = std::allocator<char>]'
C:\Users\Alx101\Documents\C++\GameList.cpp:32: error: expected `;' before '}' token
C:\Users\Alx101\Documents\C++\GameList.cpp:36: error: could not convert `((*&iter) <unknown operator> ((const __gnu_cxx::__normal_iterator<const std::string*, std::vector<std::string, std::allocator<std::string> > >&)(&__normal_iterator<const std::basic_string<char, std::char_traits<char>, std::allocator<char> >*,std::vector<std::basic_string<char, std::char_traits<char>, std::allocator<char> >, std::allocator<std::basic_string<char, std::char_traits<char>, std::allocator<char> > > > >(((const __gnu_cxx::__normal_iterator<std::string*, std::vector<std::string, std::allocator<std::string> > >&)((const __gnu_cxx::__normal_iterator<std::string*, std::vector<std::string, std::allocator<std::string> > >*)(&(&list)->std::vector<_Tp, _Alloc>::begin [with _Tp = std::string, _Alloc = std::allocator<std::string>]())))))))' to `bool'
C:\Users\Alx101\Documents\C++\GameList.cpp:36: error: expected `)' before ';' token
C:\Users\Alx101\Documents\C++\GameList.cpp:36: error: name lookup of `i' changed for new ISO `for' scoping
C:\Users\Alx101\Documents\C++\GameList.cpp:36: error:   using obsolete binding at `i'
C:\Users\Alx101\Documents\C++\GameList.cpp:36: error: expected `;' before ')' token
C:\Users\Alx101\Documents\C++\GameList.cpp:44: error: no matching function for call to `std::vector<std::string, std::allocator<std::string> >::begin(int&)'
C:/Dev-Cpp/include/c++/3.4.2/bits/stl_vector.h:314: note: candidates are: __gnu_cxx::__normal_iterator<typename _Alloc::pointer, std::vector<_Tp, _Alloc> > std::vector<_Tp, _Alloc>::begin() [with _Tp = std::string, _Alloc = std::allocator<std::string>]

C:/Dev-Cpp/include/c++/3.4.2/bits/stl_vector.h:322: note:                 __gnu_cxx::__normal_iterator<typename _Alloc::const_pointer, std::vector<_Tp, _Alloc> > std::vector<_Tp, _Alloc>::begin() const [with _Tp = std::string, _Alloc = std::allocator<std::string>]
C:\Users\Alx101\Documents\C++\GameList.cpp:45: 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> >&)'
C:/Dev-Cpp/include/c++/3.4.2/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>]
C:/Dev-Cpp/include/c++/3.4.2/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>]
C:\Users\Alx101\Documents\C++\GameList.cpp:47: error: expected `)' before ';' token
C:\Users\Alx101\Documents\C++\GameList.cpp:47: error: expected primary-expression before ')' token
C:\Users\Alx101\Documents\C++\GameList.cpp:47: error: expected `;' before ')' token
C:\Users\Alx101\Documents\C++\GameList.cpp:55: error: expected primary-expression before "else"
C:\Users\Alx101\Documents\C++\GameList.cpp:55: error: expected `;' before "else"
C:\Users\Alx101\Documents\C++\GameList.cpp:60: error: expected primary-expression before "else"
C:\Users\Alx101\Documents\C++\GameList.cpp:60: error: expected `;' before "else"

Execution terminated

It's not impossible that it is just a little error but i don't think it is, iv'e spent some time to try to fix it but i can't. Thank's
For starters you're missing semicolons on lines:
31
57

Also your for loops are messed:
1
2
3
for(int i = 0; iter = list.begin(); iter != list.end(); ++iter; ++i)
should be
for(int i = 0, iter = list.begin(); iter != list.end(); ++iter, ++i)
Last edited on
The for loop is made as i have been learned XL, And they are not the problem. Its the char at line 27.
Last edited on
You never gave that constant variable a value.

You're saying the value will never change, yet it hasn't been given a value to not change.
Last edited on
Well when i make it a non constant it says i cant use it "cant convert non const to const" Im trying to make it so that the player can add a string to the vector.
You realize that's only going to hold one character aswell.

Do something like this.

1
2
3
4
char* title;
cout << "Please type in the name of the game title that you would like to add(no spaces!)";        
cin >> title;
list.insert(list.begin(), (const char*)title)
Well i feared that. Well ive read somewhere of a getString function but i have no idea how to use it. And it worked but now the for loop is struggeling, it says it cant convert the iterator and i dont belive that i have tried to do that Xd.
Last edited on
The great thing about stream operators is they interact with types of data.

int input;
cin >> input;

//Following are both strings
char* input;
cin >> input;

std::string input;
cin >> input;

Check my previous post, I've used "char* title" as the cstring, and am casting that pointer into a "(const char*)" data type.

I've got your code in an IDE and fixing those errors. Do you want to use iterators instead of index values to access the vector array? I'm guessing you're using it like this to lean. Currently in class. ;)
Last edited on
Yes I want iterators and yes im using it to learn, its an exercise on chapter 4 in the "Beginning C++ throught game programming 3rd edition" book, And no im not in class im learning as a hobby and to be able to earn some extra money (Thinking of making indie games).
I'm in class, so may be slow getting back to you. ;)
Oh ok XD, What class? (just curious)
Software Engineering Architecture.
Wow...... So like umm... like psuedocode and stuff? or like structure of different programs?
More how to go about planning and design before getting stuck in.

Just fixed current errors, haven't added the iterator bit yet. Just thought I'd update you.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
int main()
{
    int mode = 0;
    vector<string>list;
    list.reserve(20);

    vector<string>::iterator remove;
    vector<string>::const_iterator iter;
    while(mode != 4)
    {
       cout << "\n\nWelcome to your list of games!";
       cout << "\nWhat would you like to do?";
       cout << "\nPress '1' to insert a game title.";
       cout << "\nPress '2' to remove a game title.";
       cout << "\nPress '3' to view your games.";
       cout << "\nPress '4' to exit.";
       cin >> mode;
       if(mode == 1)
       {
           char* title;
           cout << "Please type in the name of the game title that you would like to add(no spaces!)";
           cin >> title;
           list.insert(list.begin(), (const char*)title);
       }
       else if(mode == 2)
       {
           int pos;
           for(int i = 0; i < list.size(); ++i)
           {
               cout << i << " ";
               //cout << *iter <<endl;
           }
           cout << "Choose witch one to remove by the number behind them";
           cin >> pos;

           list.erase(list.begin() + pos);
       }
       else if(mode == 3)
       {
           for(int i = 0; i < list.size(); ++i)
           {
               cout << i << " ";
               //cout << *iter <<endl;
           }
       }
       else if(mode == 4)
       {
           cout << "Later!";
           break;
       }
       else
       {
           cout << "Please enter a valid number";
       }
    }

   return 0;
}
Last edited on
Thanks man! And heres what im trying to acheive: Write a program using vectors and iterators that allows a user to maintain
a list of his or her favorite games. The program should allow the
user to list all game titles, add a game title, and remove a game title.
Last edited on
Afterwards we will talk about improving the design of this program.
Ok :D
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
///
template<class T>
   void Print(T array)
{
   typename T::iterator it = array.begin();
   for(int i = 0; i < array.size() && it != array.end(); ++i, ++it)
   {
      cout << i << " ";
      cout << *it << endl;
   }
}

///
int main()
{
   int mode = 0;
   vector<string> list;
   list.reserve(20);

   do
   {
      cout << "\n\nWelcome to your list of games!"
         << "\nWhat would you like to do?"
         << "\nPress '1' to insert a game title."
         << "\nPress '2' to remove a game title."
         << "\nPress '3' to view your games."
         << "\nPress '4' to exit.";

      cin >> mode;
      switch(mode)
      {
         case 1:
            {
               string title;
               cout << "Please type in the name of the game title that you would like to add(no spaces!)";
               cin >> title;
               list.push_back(title);
            }
            break;

         case 2:
            {
               int pos;

               Print(list);

               cout << "Please input the position of the item to remove:";
               cin >> pos;

               list.erase(list.begin() + pos);
            }
            break;

         case 3:
            Print(list);
            break;

      default:
         cout << "Please enter a valid number";
      }
   } while(mode != 4);

   return 0;
}


I simplified the code even further.
Last edited on
It crashed when i tried to add a game title :C
Ok fixed the above code, using strings instead of cstrings to match the type held in vector.

Also rather than writing the same piece of code to print the contents in the vector array, just made a reusable function. Note the template, this is a generic function, what ever your array holds "vector<int>, vector<string>, ..." it can be passed to that function to print.

Will fix that issue you get with cin and spaces.
Last edited on
Pages: 12