Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Confusing Error Message With Multimap

Status
Not open for further replies.

Dario1984

Technical User
Aug 23, 2005
12
AU
I'm having a problem with my multimap. I've looked on the internet, and I can't see anything different to what other people have done. I get one of those long and unhelpful errors. I know it's against the policy to post uni questions on here, but our C++ lecturer has gone to Italy and China for 4 weeks and the other guy isn't so familiar in C++. Could anyone help me out please ?

Code:
#include <iostream>
#include <fstream>
#include <string>
#include <map>
#include <cstdlib>

using namespace std;

void readFile(multimap<string, string>& words);

int main()
{
	typedef multimap<string, string>::const_iterator mConstItr;
	
	multimap<string, string> words;
	string searchWord;
	string originalWord;
	pair<mConstItr, mConstItr> rangeOfKey;

	readFile(words);

	cout << "What word would you like to see anagrams of, if they exist ? ";
	
	cin >> searchWord;

	while(!cin.eof())
	{
		originalWord = searchWord;
		sort(searchWord.begin(), searchWord.end());
		rangeOfKey = words.equal_range(searchWord);
		if (rangeOfKey.first != 0)
		{
			for (mConstItr index = rangeOfKey.first; index != rangeOfKey.second; index++)
			{
				if (index->second != originalWord)
					cout << index->second << endl;
			}
		}
		else
		{
			cout << "No anagrams found." << endl;
		}
		
		cout << endl << "What is the next word to see anagrams of ? ";
		cin >> searchWord;
	}

	return 0;
}

void readFile(multimap<string, string>& words)
{
	char dictFile[20];
	string tempWord;
	ifstream inData;
	int wordCounter = 0;
	pair<string, string> row;

	cout << "What is the name of the dictionary file ? ";
	cin >> dictFile;

	inData.open(dictFile);
	if (!inData.good())
	{
		cerr << "Data file not found or corrupted";
		exit(1);
	}

	inData >> tempWord;
	while (!inData.eof())
	{
		wordCounter++;
		row.second = tempWord;
		sort(tempWord.begin(), tempWord.end());
		row.first = tempWord;
		words.insert(row);

		inData >> tempWord;
	}
	
	inData.close();

	cout << wordCounter << " words read in from file." << endl;
}
 
Several things:

1. You didn't post the error message. It might be helpful to someone else even if it you don't understand it.

2. I compiled this on the only compiler I have at the moment (VC++ on Windows) and there was only one error - you need to add #include <algorithm> since you are using sort. I didn't notice anything else that might not compile on other platforms.

3. This thread will likely be deleted, maybe even before you read this, it would be better to post on a website that allows these types of questions. Remember to post the full text of the error message as well.

Good luck.
 
C++ forum we typically don't "Red Flag" just warn them and don't give an answer, so that other students will see that there is application of the rules.

4. When posting code, use code tags for readability.

[plug=shameless]
[/plug]
 
Thanks anyway guys. It was actually an error I got when trying to put the <string, string> into the multimap. On the Sun CC compiler, it turns out you can only have <const string, string>. So that's why no one else was able to see those errors. CC is quite weird !
 
According to Scott Meyers in "Effective STL" (Item 22) the C++ standard requires map and multimap to hold pair<const K, V> values.

uolj: Which version of VC++ did you use? If it compiled, it must not be completely standard conforming.
 
I don't remember which I used (I'm not sure why I didn't mention it before). I have only 6.0 on my current machine, and I think I tested it from here, which would make sense as to why it allowed it to compile even though it shouldn't.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top