Sign in to follow this  
Followers 0
Christopher Murray

Need help with an excercise from Beginning C++ 2nd edition

7 posts in this topic

The excerise is to 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 games, and remove them.

Right now I'm working on adding the game titles, which I have figured out, however I want to loop it with a menu so that if the user hits 1, they can add another game, 2 they can remove a game, so on. So I figure to do this I need a loop, so I try a [i]do[/i] loop. However it's not allowing me to add ther part where I make the menu and get the input from the user, I'm assuming because I do not initialize the variable that holds the user's input, and if I initialize it, for example I tried to set it to 0 before getting input, it still didn't work. Perhaps I'm just trying to use the wrong loop? I don't quite understand [i]for [/i]loops unless I want the program to count for me.

Sorry for the block of text, just wanted to make clear what I'm having a problem with. Also I've only been into the book for about a week so a lot hasn't sunk in yet so the mistake may be obvious... Here's my code:

[code]//Program that allows a user to maintain a list of his or her favorite games.
#include <iostream>
#include <string>
#include <vector>
using namespace std;
int main()
{
vector<string> gameList;

vector<string>::iterator myIterator;
vector<string>::const_iterator iter;

do
{

unsigned short int options;
cout << "Select an option:\n";
cout << "1 - Add a game to your favorites list.\n";
cin >> options;

string game;


cout << "\nAdd a game to your favorites: ";
cin >> game;


gameList.insert(gameList.end(), game);

cout << "\nYour current list:\n";
for (iter = gameList.begin(); iter != gameList.end(); ++iter)
cout << *iter << endl;
} while (options == 1);

char theEnd;
cin >> theEnd;
return 0;
}[/code]

PS The variable theEnd is just there so the program doesn't automatically close. Using Dev-C++ and don't want to use .bat file every time I check a program, figured it'd be a simple way.
0

Share this post


Link to post
Share on other sites
First off let me say I have pretty basic knowledge of C++ and I'm pretty much a beginner in general, but my guess is if you want to do this what you need to do is have a way to escape the loop.

For example if your menu is

1 - Add Game
2 - Remove Game

You need to have something like:

9 - Quit

Then you can have your do loop do something like

do {
stuff;
}
while ( userAnswer != 9);
1

Share this post


Link to post
Share on other sites
[quote name='Christopher Murray' timestamp='1329271350' post='4913215']
PS The variable theEnd is just there so the program doesn't automatically close. Using Dev-C++ and don't want to use .bat file every time I check a program, figured it'd be a simple way.
[/quote]
Dev-C++ is years old, although I think someone's forked a newer IDE from it. I'd strongly recommend MSVC Express. It's quality software and it's free. If you're absolutely against Microsoft then there's Code::Blocks, also free. I'd provide links if I weren't on my phone, but Google is your friend.

I've never seen anyone recommend Dev-C++ for any reason.
1

Share this post


Link to post
Share on other sites
a do-while loop would be fine in this case, but your check after the while is the problem:
[code]
do
{
int options;
cout << "Select an option:\n";
cout << "1 - Add a game to your favorites list.\n";
cout << "2 - Remove a game to your favorites list.\n";
cout << "3 - List all games.\n"
cout << "0 - Quit program.\n"
cin >> options;

if (option = 1) {
string game;

cout << "\nAdd a game to your favorites: ";
cin >> game;

gameList.push_back(game);
}
else if ... // check all other options

} while (options != 0);
[/code]
1

Share this post


Link to post
Share on other sites
The problem lies on two lines...

[code] do
{

unsigned short int options; // <-- THIS ONE[/code]

and

[code]} while (options == 1); // <-- AND THIS ONE[/code]

The problem lies in something called SCOPE.

Lets look at this...

[code]do
{
int options;
//... other code here...
} while (options == 0)[/code]

Now options is contained within bracers, these control the SCOPE of the local variable. The variable options ONLY EXISTS within the curly bracers in which it was defined.

[code]// options doesn't exist
{
int options;
// options exists
}
// options doesn't exist anymore.[/code]

Since you are trying to check your do/while loop with a variable that goes OUT OF SCOPE before the conditional statement, the compiler should flag an error and it DOES on Visual Studio 2010.

The easiest fix is to simply define options BEFORE the do-while loop.

[code]//Program that allows a user to maintain a list of his or her favorite games.
#include <iostream>
#include <string>
#include <vector>
using namespace std;
int main()
{
vector<string> gameList;

vector<string>::iterator myIterator;
vector<string>::const_iterator iter;

// DEFINED BEFORE THE DO-WHILE.
unsigned short int options;

do
{

cout << "Select an option:\n";
cout << "1 - Add a game to your favorites list.\n";
cin >> options;

string game;


cout << "\nAdd a game to your favorites: ";
cin >> game;


gameList.insert(gameList.end(), game);

cout << "\nYour current list:\n";
for (iter = gameList.begin(); iter != gameList.end(); ++iter)
cout << *iter << endl;
} while (options == 1);

char theEnd;
cin >> theEnd;
return 0;
}[/code]

You'll find this code now works.

If you still don't understand, please let me know and I'll try to explain it more clearly.

Can someone please recommend/write a decent tutorial that explains scope in C++? There used to be a perfect one on about.com but I'll be blasted if I can find it now...
3

Share this post


Link to post
Share on other sites
[quote name='Christopher Murray' timestamp='1329334262' post='4913438']
Thank you everyone, got it working now = )
[/quote]
You're welcome.
1

Share this post


Link to post
Share on other sites
As a little addition, you can use functions for each option:
[code]
void add()
{
// Print out the add message and get the input
// Push the input onto the vector
}
void remove()
{
// Print out the remove message and get the input
// Pop the input off the vector
}
void view()
{
// Print the vector with iterators
}

int main()
{
int optionChoice = 0;
do
{
view();
// Get input choice and set to optionChoice;
switch(optionChoice)
{
case 1:
add();
break;
case 2:
remove();
break;
default:
break;
}
} while(optionChoice != 3);
return 0;
}[/code]
0

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0