Jump to content

  • Log In with Google      Sign In   
  • Create Account


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


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
7 replies to this topic

#1 Christopher Murray   Members   -  Reputation: 100

Like
0Likes
Like

Posted 14 February 2012 - 08:02 PM

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 do 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 for 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:

//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;
}

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.

Sponsor:

#2 Spyderzone   Members   -  Reputation: 159

Like
1Likes
Like

Posted 14 February 2012 - 09:44 PM

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);

#3 yckx   Prime Members   -  Reputation: 1163

Like
1Likes
Like

Posted 15 February 2012 - 02:39 AM

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.

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.

#4 BeerNutts   Crossbones+   -  Reputation: 2516

Like
1Likes
Like

Posted 15 February 2012 - 08:01 AM

a do-while loop would be fine in this case, but your check after the while is the problem:
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);

My Gamedev Journal: 2D Game Making, the Easy Way

---(Old Blog, still has good info): 2dGameMaking
-----
"No one ever posts on that message board; it's too crowded." - Yoga Berra (sorta)

#5 TehOwn   Members   -  Reputation: 134

Like
3Likes
Like

Posted 15 February 2012 - 11:26 AM

The problem lies on two lines...

do
		{

		unsigned short int options; // <-- THIS ONE

and

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

The problem lies in something called SCOPE.

Lets look at this...

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

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.

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

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.

//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;
}

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...

#6 Christopher Murray   Members   -  Reputation: 100

Like
0Likes
Like

Posted 15 February 2012 - 01:31 PM

Thank you everyone, got it working now = )

#7 TehOwn   Members   -  Reputation: 134

Like
1Likes
Like

Posted 15 February 2012 - 04:12 PM

Thank you everyone, got it working now = )

You're welcome.

#8 Dragonsoulj   Crossbones+   -  Reputation: 2008

Like
0Likes
Like

Posted 15 February 2012 - 06:47 PM

As a little addition, you can use functions for each option:
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;
}





Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS