Check my plan for a game list program

Hi, I'd like to get your opinions on my psuedocode plan for a simple program for creating a games list. It's an exercise from a book I'm reading and this is the first time I've used psuedocode.

Could you tell me if a) the program looks like it would work, and b) do you have any tips/pointers to improve my writing of psuedocode?

Here it is:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
Create game_list vector
Create exit variable and set to false

		***MAIN LOOP START***
While exit variable is false
	Show menu offering ADD GAME, REMOVE GAME, LIST GAMES, and EXIT
	While choice doesn't equal a viable option
		Show menu again

	If choice is ADD GAME
		Prompt for name of game to be added
		Add game to game_list vector
	Else if choice is REMOVE GAME
		Prompt for index number of listed game to remove
		Delete the specified entry in the game_list vector after subtracting 1 from it
	Else if choice is LIST GAMES
		Print all elements of game list vector with corresponding index value (+1)
	Else
		Set exit variable to true

		***MAIN LOOP END*** 
Last edited on
I finally finished the program, here it is:

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
#include <iostream>
#include <cstdlib>
#include <vector>
using namespace std;

int main()
{
    vector<string> games_list;
    //vector<string>::cont_iterator iter;
    bool quit = false;
    unsigned short int choice, game_index;
    string game_name;

    while (!quit)   {

        system("cls");

        cout << "1: Add game to list.\n";
        cout << "2: Remove game from list.\n";
        cout << "3: View current list.\n";
        cout << "4: Quit\n";
        cout << "\nEnter choice: ";
        cin >> choice;

        switch (choice) {
            case 1:     cout << "Enter the name of the game: ";
                        cin >> game_name;
                        games_list.push_back(game_name);

            case 2:     cout << "Enter the index number of the game: ";
                        cin >> game_index;
                        games_list.erase(game_index+1);

            case 3:     system("cls");
                        for (int i = 0; i < games_list.size(); i++)   {
                            cout << i+1 << ": " << games_list[i] << endl;
                        }

                        cout << "\nPress any key to return to menu.";
                        cin;

            case 4:     quit = true;

            default:    cout << "Invalid Input";
                        system("PASUE");
        }

    }

    return 0;
}


But I get a compile error I don't understand:

1
2
3
4
5
6
7
C:\Users\Calvin\Documents\C++ Projects\gamelist\main.cpp||In function 'int main()':|
C:\Users\Calvin\Documents\C++ Projects\gamelist\main.cpp|32|error: no matching function for call to '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> > > >::erase(int)'|
e:\program files (x86)\codeblocks\mingw\bin\..\lib\gcc\mingw32\4.4.1\include\c++\bits\vector.tcc|133|note: candidates are: __gnu_cxx::__normal_iterator<typename std::_Vector_base<_Tp, _Alloc>::_Tp_alloc_type::pointer, std::vector<_Tp, _Alloc> > std::vector<_Tp, _Alloc>::erase(__gnu_cxx::__normal_iterator<typename std::_Vector_base<_Tp, _Alloc>::_Tp_alloc_type::pointer, std::vector<_Tp, _Alloc> >) [with _Tp = std::basic_string<char, std::char_traits<char>, std::allocator<char> >, _Alloc = std::allocator<std::basic_string<char, std::char_traits<char>, std::allocator<char> > >]|
e:\program files (x86)\codeblocks\mingw\bin\..\lib\gcc\mingw32\4.4.1\include\c++\bits\vector.tcc|145|note:                 __gnu_cxx::__normal_iterator<typename std::_Vector_base<_Tp, _Alloc>::_Tp_alloc_type::pointer, std::vector<_Tp, _Alloc> > std::vector<_Tp, _Alloc>::erase(__gnu_cxx::__normal_iterator<typename std::_Vector_base<_Tp, _Alloc>::_Tp_alloc_type::pointer, std::vector<_Tp, _Alloc> >, __gnu_cxx::__normal_iterator<typename std::_Vector_base<_Tp, _Alloc>::_Tp_alloc_type::pointer, std::vector<_Tp, _Alloc> >) [with _Tp = std::basic_string<char, std::char_traits<char>, std::allocator<char> >, _Allo|
C:\Users\Calvin\Documents\C++ Projects\gamelist\main.cpp|35|warning: comparison between signed and unsigned integer expressions|
C:\Users\Calvin\Documents\C++ Projects\gamelist\main.cpp|40|warning: statement has no effect|
||=== Build finished: 1 errors, 2 warnings ===|
closed account (10oTURfi)
You need to include <string>.

Suggestion? Store list to file.
It is good to see you took an organised approach and thought about the code before writing it. I have added a few notes and corrections:
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
#include <iostream>
#include <cstdlib>
#include <vector>
using namespace std;

int main()
{
    vector<string> games_list;
    //vector<string>::cont_iterator iter;
    bool quit = false;
    unsigned short int choice, game_index;
    string game_name;

    while (!quit)   {

        system("cls");

        cout << "1: Add game to list.\n";
        cout << "2: Remove game from list.\n";
        cout << "3: View current list.\n";
        cout << "4: Quit\n";
        cout << "\nEnter choice: ";
        cin >> choice;

        switch (choice) {
            case 1:     cout << "Enter the name of the game: ";
                        cin >> game_name;
                        games_list.push_back(game_name);
            break; // Dont forget this

            case 2:     cout << "Enter the index number of the game: ";
                        cin >> game_index;
                        // erase needs an iterator and you need index -1 (not +1)
                        // games_list.erase(game_index+1);
                        games_list.erase(games_list.begin() + (game_index - 1));
            break; // Dont forget this

            case 3:     system("cls");
                        for (int i = 0; i < games_list.size(); i++)   {
                            cout << i+1 << ": " << games_list[i] << endl;
                        }

                        cout << "\nPress any key to return to menu.";
                        // cin;
                        cin.get(); // I think you  maybe meant this?
            break; // Dont forget this

            case 4:     quit = true;
            break; // Dont forget this

            default:    cout << "Invalid Input";
                        system("PASUE");
        }

    }

    return 0;
}
Writing this program out first in psuedocode made writing the program soo much easier. I'm doing everytime from now on.

Krofna: Yes, writing the list to disk would very beneficial but this is an exercise from a book. I don't know how to write files to disk yet. Also I still get the same error even if I include <string> (I even tried <cstring> and it didn't work.

Galik: Man I just knew there was something I was forgetting at the end of case statements :/ And yes, by using "cin;" I was just try to wait for a single key press.

Here is my updated code which to be honest should look exactly like what you posted:

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
#include <iostream>
#include <cstdlib>
#include <vector>
#include <string>
using namespace std;

int main()
{
    vector<string> games_list;
    bool quit = false;
    unsigned short int choice, game_index;
    string game_name;

    while (!quit)   {

        system("cls");

        cout << "1: Add game to list.\n";
        cout << "2: Remove game from list.\n";
        cout << "3: View current list.\n";
        cout << "4: Quit\n";
        cout << "\nEnter choice: ";
        cin >> choice;

        switch (choice) {
            case 1:     cout << "Enter the name of the game: ";
                        cin >> game_name;
                        games_list.push_back(game_name);
                        break;
            case 2:     cout << "Enter the index number of the game: ";
                        cin >> game_index;
                        games_list.erase(game_index+1);
                        break;
            case 3:     system("cls");
                        for (int i = 0; i < games_list.size(); i++)   {
                            cout << i+1 << ": " << games_list[i] << endl;
                        }

                        cout << "\nPress any key to return to menu.";
                        cin.get();
                        break;
            case 4:     quit = true;

            default:    cout << "Invalid Input";
                        system("PASUE");
        }

    }

    return 0;
}


And here is the error message. To me it looks like I am missing another #include as it's complaing about a function not being declared:

1
2
3
4
5
6
C:\Users\Calvin\Documents\C++ Projects\gamelist\main.cpp||In function 'int main()':|
C:\Users\Calvin\Documents\C++ Projects\gamelist\main.cpp|33|error: no matching function for call to '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> > > >::erase(int)'|
e:\program files (x86)\codeblocks\mingw\bin\..\lib\gcc\mingw32\4.4.1\include\c++\bits\vector.tcc|133|note: candidates are: __gnu_cxx::__normal_iterator<typename std::_Vector_base<_Tp, _Alloc>::_Tp_alloc_type::pointer, std::vector<_Tp, _Alloc> > std::vector<_Tp, _Alloc>::erase(__gnu_cxx::__normal_iterator<typename std::_Vector_base<_Tp, _Alloc>::_Tp_alloc_type::pointer, std::vector<_Tp, _Alloc> >) [with _Tp = std::basic_string<char, std::char_traits<char>, std::allocator<char> >, _Alloc = std::allocator<std::basic_string<char, std::char_traits<char>, std::allocator<char> > >]|
e:\program files (x86)\codeblocks\mingw\bin\..\lib\gcc\mingw32\4.4.1\include\c++\bits\vector.tcc|145|note:                 __gnu_cxx::__normal_iterator<typename std::_Vector_base<_Tp, _Alloc>::_Tp_alloc_type::pointer, std::vector<_Tp, _Alloc> > std::vector<_Tp, _Alloc>::erase(__gnu_cxx::__normal_iterator<typename std::_Vector_base<_Tp, _Alloc>::_Tp_alloc_type::pointer, std::vector<_Tp, _Alloc> >, __gnu_cxx::__normal_iterator<typename std::_Vector_base<_Tp, _Alloc>::_Tp_alloc_type::pointer, std::vector<_Tp, _Alloc> >) [with _Tp = std::basic_string<char, std::char_traits<char>, std::allocator<char> >, _Allo|
C:\Users\Calvin\Documents\C++ Projects\gamelist\main.cpp|36|warning: comparison between signed and unsigned integer expressions|
||=== Build finished: 1 errors, 1 warnings ===|


The top line says the error is on line 33 but in codeblocks it's line 32 that is highlighted red.
You're still not erasing the game properly:
 
games_list.erase(game_index+1);

The erase() function needs an iterator, not an int:
 
games_list.erase(games_list.begin() + (game_index - 1));

That works because I take the iterator returned from games_list.begin() and add the index of the game I want to remove to it (game_index - 1).

Note also that you need to subtract 1 from the index, not add one.
Last edited on
Sorry about that I completely missed it.

I used your suggestions and realized the program wasn't quite complete so am back again with another error I can't solve, although I'm pretty sure I'm passing a string to cin.getline in the wrong way. Take a look:

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 <cstdlib>
#include <vector>
#include <string>
using namespace std;

int main()
{
    vector<string> games_list;
    bool quit = false;
    unsigned short int choice, game_index;
    string game_name;

    while (!quit)   {

        system("cls");

        cout << "1: Add game to list.\n";
        cout << "2: Remove game from list.\n";
        cout << "3: View current list.\n";
        cout << "4: Quit\n";
        cout << "\nEnter choice: ";
        cin >> choice;

        switch (choice) {
            case 1:     cout << "Enter the name of the game: ";
                        cin.getline(game_name, 50);
                        games_list.push_back(game_name);
                        system("cls");
                        for (int i = 0; i < games_list.size(); i++)   {
                                cout << i+1 << ": " << games_list[i] << endl;
                        }
                        cout << endl << game_name << " has been added to the list.\n\n";
                        cin.clear();
            break;

            case 2:     cout << "Enter the index number of the game: ";
                        cin >> game_index;
                        games_list.erase(games_list.begin() + (game_index - 1));
            break;

            case 3:     system("cls");
                        if (games_list.empty()) {
                            cout << "List is empty." << endl;
                        } else {
                            for (int i = 0; i < games_list.size(); i++)   {
                                cout << i+1 << ": " << games_list[i] << endl;
                            }
                        }
            break;

            case 4:     quit = true;

            default:    cout << "Invalid Input";
                        system("PAUSE");
        }

        cout << "\nPress any key to return to menu.";
        cin.get();
        cin.get();

    }

    return 0;
}


1
2
3
4
5
6
7
C:\Users\Calvin\Documents\C++ Projects\gamelist\main.cpp||In function 'int main()':|
C:\Users\Calvin\Documents\C++ Projects\gamelist\main.cpp|27|error: no matching function for call to 'std::basic_istream<char, std::char_traits<char> >::getline(std::string&, int)'|
e:\program files (x86)\codeblocks\mingw\bin\..\lib\gcc\mingw32\4.4.1\include\c++\istream|593|note: candidates are: std::basic_istream<_CharT, _Traits>& std::basic_istream<_CharT, _Traits>::getline(_CharT*, std::streamsize, _CharT) [with _CharT = char, _Traits = std::char_traits<char>]|
e:\program files (x86)\codeblocks\mingw\bin\..\lib\gcc\mingw32\4.4.1\include\c++\istream|405|note:                 std::basic_istream<_CharT, _Traits>& std::basic_istream<_CharT, _Traits>::getline(_CharT*, std::streamsize) [with _CharT = char, _Traits = std::char_traits<char>]|
C:\Users\Calvin\Documents\C++ Projects\gamelist\main.cpp|30|warning: comparison between signed and unsigned integer expressions|
C:\Users\Calvin\Documents\C++ Projects\gamelist\main.cpp|46|warning: comparison between signed and unsigned integer expressions|
||=== Build finished: 1 errors, 2 warnings ===|


Line 27.
Yeah that's the wrong version of getline()
 
cin.getline(game_name, 50);

That is the member function that expects a char*. You need the other function:
 
std::getline(cin, game_name);
Hey Galic, thanks for the help.

I replaced line 27 with the std version and it compiled fine but I'm sorry to say I've run into yet another problem. When I run the program and press 1 to add a game, the program seems to completely skip line 27.

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 <cstdlib>
#include <vector>
#include <string>
using namespace std;

int main()
{
    vector<string> games_list;
    bool quit = false;
    unsigned short int choice, game_index;
    string game_name;

    while (!quit)   {

        system("cls");

        cout << "1: Add game to list.\n";
        cout << "2: Remove game from list.\n";
        cout << "3: View current list.\n";
        cout << "4: Quit\n";
        cout << "\nEnter choice: ";
        cin >> choice;

        switch (choice) {
            case 1:     cout << "Enter the name of the game: ";
                        std::getline(cin, game_name);
                        games_list.push_back(game_name);
                        for (int i = 0; i < games_list.size(); i++)   {
                                cout << i+1 << ": " << games_list[i] << endl;
                        }
                        cout << endl << game_name << " has been added to the list.\n\n";
                        cin.clear();
            break;

            case 2:     cout << "Enter the index number of the game: ";
                        cin >> game_index;
                        games_list.erase(games_list.begin() + (game_index - 1));
            break;

            case 3:     system("cls");
                        if (games_list.empty()) {
                            cout << "List is empty." << endl;
                        } else {
                            for (int i = 0; i < games_list.size(); i++)   {
                                cout << i+1 << ": " << games_list[i] << endl;
                            }
                        }
            break;

            case 4:     quit = true;
            break;

            default:    cout << "Invalid Input";
                        system("PAUSE");
        }

        cout << "\nPress any key to return to menu.";
        cin.get();
        //cin.get();

    }

    return 0;
}
The problem there is that cin >> choice; does not remove the end of line character, so the following std::getline() grabs only up to the previous end of line and doesn't ask you for a new one.

There are a few ways round this but the easiest one to explain involves calling std::getline(); immediately after cin >> choice; to finish off reading that line before asking for a new one:
1
2
3
4
		cin >> choice; // doesn't finish reading line

		std::string ignore; // any dummy string will do
		std::getline(cin, ignore); // finish reading line into the dummy string 







I always though that getline() always stripped out the '\n' character?

http://ideone.com/kcych
Lynx876 wrote:
I always though that getline() always stripped out the '\n' character?

It does, but cin >> var; leaves the '\n' character in place. This is why you need the std::getline() to remove it ready for the next read.
Thanks again Galik, the programs works fine now :)
Topic archived. No new replies allowed.