Need help with a small list program

Started by
2 comments, last by neshtak 15 years, 5 months ago
Hi. Basically I just finished reading about iterators, vectors and some of the basic algorithms. The excercise in the book is to write a little program that will allow user to store and maintain list of his/her favorite games. User should be able to display, add and remove the names in the list. The source: http://files.filefront.com/sourcetxt/;12291060;/fileinfo.html What my code does is add, and display, but can't remove from the list. I tried to understand why is this happening but no luck. Also when you type names with spaces the message "Enter: " turns into "Enter: Enter: " Where did i go wrong? ps Can you somehow make it so when user types in "r 'nameofthegameinthelist'" the program removes it, instead of my way?
Advertisement
post source in SOURCE tags
[ source ] [ / source ]
Here's the code (from the downloaded file):

#include <iostream>#include <string>#include <vector>using namespace std;int main(){	vector<string> list;	string name;	do	{		cout << "Enter: ";cin >> name;				if (name == "display")		{			for (iter = list.begin(); iter != list.end(); ++iter)				cout << *iter << endl;		}		if (name == "remove")		{		cout << "Delete: ";		cin >> name;		if (name != list.find(name))		{			cout << "No game found\n";		}		else		{		list.erase(name);		}		else			list.push_back(name);	}while (name != "quit");	return 0;}


This doesn't compile. Did you post an earlier version of your code?


Quote:
Also when you type names with spaces the message "Enter: " turns into "Enter: Enter: "


cin >> name; only extracts a single word from cin. Try using getline( cin, name, '\n' ); instead.

Quote:
What my code does is add, and display, but can't remove from the list.


Looking at the code, you're trying to use vector<string>::find(), which doesn't exist. You are probably thinking of std::find() which is defined in <algorithm>. To use in your code, you: iter = find( list.begin(), list.end(), name );. If name wasn't found in list, find returns list.end().

Also, vector<string>::erase() doesn't take a string-value as a parameter, only an iterator (which you can obtain from the find algorithm in the above paragraph). ^^

Quote:
Can you somehow make it so when user types in "r 'nameofthegameinthelist'" the program removes it, instead of my way?


Sure. Look up on how to use stringstreams. You can use them just as you would cin and cout. With the getline you can then insert into a stringstream (my_stringstream << my_read_line;) then you can extract tokens (words) out of the stream (my_stringstream >> my_token).

#include <string>#include <sstream>#include <iostream>using namespace std;int main(){	string line;	string token;	do	{		cout << "Enter: ";		getline( cin, line, '\n' ); // read an entire line				// display what the user entered:		cout << "line is now: \"" << line << "\"\n";				stringstream ss;		ss << line; // insert what the user entered into the stream		int count = 0;		while ( !ss.eof() ) // eof is "end of line"		{			count ++;			// read a token:			ss >> token; 			// display the token:			cout << "token: " << count << " is \"" << token << "\"\n";		}		cout << endl;	}	while ( line != "quit" );}


Enter: the quick brown fox jumps over the lazy dogline is now: "the quick brown fox jumps over the lazy dog"token: 1 is "the"token: 2 is "quick"token: 3 is "brown"token: 4 is "fox"token: 5 is "jumps"token: 6 is "over"token: 7 is "the"token: 8 is "lazy"token: 9 is "dog"Enter:


Does that give you any ideas on how to do it yourself? [smile]

[Edited by - _fastcall on November 8, 2008 3:27:07 PM]
Yep.
Thx man.
Ill try to utilize these more often.

ps yes that is the latest version of my code

=)

This topic is closed to new replies.

Advertisement