Help with assignment

Hi! For one of my assignments, it requires that I code a program that can store, delete, and view an archive of the users favorite games. It's a chapter introducing the use of vectors from the book 'Beginning C++ through game programming'.

Anyway, here is my code. I can't quite figure out why it's returning errors. Very new at this....

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
  // This is for C4-HW section C. Question 1
// Demonstrating Vectors

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

using namespace std;
int main()
{
	vector<string>library;
	library.push_back("Halo: 3");
	library.push_back("Fable: Lost Chapters");
	library.push_back("World of Warcraft");
	
	cout << "You have " << library.size() << " games.\n";
	
	cout << "\nYour games: \n";
	for (unsigned int i=0;i<library.size();++i)
	{
		cout << library[i] <<endl;
	}
	string use = "use";
	string input = use;
	string add = "add";
	string view = "view";
	string remove = "remove";
	string add_game = "add";
	string quit = "quit";
	
	do
	{
	
	cout << "\n\nWhat would you like to do? add, view, or remove? \n";
	cin >> input;
    
	if (input == add)
	{
		cout << "What would you like to add? \n";
		cin >> add_game;
		library.push_back(input);
		input = use;
	}
	if (input == view)
	{
			cout << "You have " << library.size() << " games.\n";
    }
	cout << "\nYour games: \n";
	for (unsigned int i=0;i<library.size();++i)
	{
		cout << library[i] <<endl;
	}
	if (input == remove)
	{
		library.pop_back();
	}
	
	while (input=="quit");
}
return 0;
}
The condition for the do-while should be outside the block; and it should be while( input != quit ) ;
1
2
3
4
5
6
do
{
    // ....
    // while (input=="quit");
}
while( input != quit ) ;


On line 43; this is what you intended to do:
1
2
// library.push_back(input); // line 43
library.push_back(add_game);
Super helpful! Thank you so much!

Although that completes the assignment, I'd like to know more about how I might remove certain titles from the vector. As it stands 'remove' only removes the last one on the list.

Once again, thank 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
59
60
61
62
63
64
65
66
67
68
69
70
71
#include <iostream>
#include <string>
#include <vector>

int main()
{
    // C++11: http://www.stroustrup.com/C++11FAQ.html#init-list
    std::vector< std::string > library { "Halo: 3", "Fable: Lost Chapters", "World of Warcraft" } ;

    std::cout << "You have " << library.size() << " games.\n";
    std::cout << "\nYour games: \n";
    // C++11: http://www.stroustrup.com/C++11FAQ.html#for
    for( const std::string& game : library ) std::cout << game << '\n' ;

    std::string input ;
    do
    {
        std::cout << "\n\nWhat would you like to do? add/view/remove/quit? ";
        std::cin >> input;

        if( input == "add" )
        {
            std::cout << "What would you like to add? ";
            std::string game_to_be_added ;

            // we want to read in the name of the game which may contain spaces
            // so, use std::getline http://en.cppreference.com/w/cpp/io/basic_istream/getline
            // before that, we need to extract and discard the new line remaining in the input buffer
            // so, std::cin >> std::ws http://en.cppreference.com/w/cpp/io/manip/ws
            // more information: http://www.cplusplus.com/forum/general/196903/#msg945238
            std::getline( std::cin >> std::ws, game_to_be_added ) ;
            library.push_back(game_to_be_added);
        }

        else if( input == "view" )
        {
            std::cout << "\nYou have " << library.size() << " games.\n";
            std::cout << "\nYour games: \n";
            for( const std::string& game : library ) std::cout << game << '\n' ;
        }

        else if( input == "remove" )
        {
            // can't remove anything from an empty vector
            if( library.empty() ) std::cout << "can't remove from empty library\n" ;

            // if there is only one game in the library, remove it
            else if( library.size() == 1 ) library.pop_back();

            else // ask the user which game is to be removed
            {
                for( std::size_t i = 0 ; i < library.size() ; ++i )
                    std::cout << i+1 << ". " << library[i] << '\n' ;
                std::size_t pos = 0 ;
                std::cout << "which game do you want to remove? [1-" << library.size() << "]? "  ;
                std::cin >> pos ; // for now, we will assume that the user enters a valid number

                --pos ; // make the position zero based

                // if the user entered a valid position, remove the item
                // http://en.cppreference.com/w/cpp/container/vector/erase (1)
                // http://en.cppreference.com/w/cpp/container/vector/begin
                if( pos < library.size() ) library.erase( library.begin() + pos ) ;
                else std::cout << "invalid input\n" ;
            }
        }

        else if( input != "quit" ) std::cout << "invalid input\n" ;
    }
    while (input != "quit");
}
Topic archived. No new replies allowed.