Outputting a map

So I'm trying to create a program that reads in a list of words and then reads in a document and compares that document to the list of words and if a word from the document doesn't appear in the list its added to a map along with its line number. So I managed to get that coded i think but I'm having issues on outputting it correctly. If anyone could point me in the right direction that would be great. Here is the code:

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
  #include "document.h"


document::document(void)
{
	map<string,vector<int> > misspelled;
}
document::~document(void){}

void document::missingMap(string word, int lineNum)//adds words and line numbers
{
	misspelled[word].push_back(lineNum);		
}
void document::displayMap()
{
for (map<string, vector<int>>::iterator i = misspelled.begin(); i !=     misspelled.end(); i++) //function that displays the map
{
	cout << i->first << ": ";
	for (vector<int>::iterator j = i->second.begin(); j != i->second.end(); j++)
	{
		cout << *j << endl;
	}
}



}


so my biggest issue is that its currently outputting like:

1
2
3
4
5
6
debugging: 1
process: 2
removing: 2
programming: 3
process: 4
putting: 4


but i need it to output as follows:

1
2
3
4
5
debugging: 1
process: 2 4
programming: 3
putting: 4
removing: 2


anything will help!
I don't believe you. Provide a complete minimal example that reproduces the issue and maybe we can see what is really going on.
Line 6: I assume you have a member variable called misspelled, so this line is pointless (just shadows the variable).

Also, that output you have there makes very little sense... it seems to be implying that your map has duplicate keys. Also, considering that maps automatically sort your data in alphabetical order ('<' operator for string), it makes no sense that the data isn't in order. As @L B said, we need to find out what is going on with the rest of the code to see how you have managed to break this.
Yeah thats why i've been so confused, i wasn't believing it either! Here is my main function

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
72
73
74
75
76
77
78
79
80
81
#include <iostream>
#include <string>
#include <vector>
#include <set>
#include <algorithm>
#include <fstream>
#include "dictionary.h"
#include "document.h"

using namespace std;

void sentancetoword(string sentance, set<string> words, int lineNum)
{
	dictionary d;
	document doc;
	bool wordCheck;
	string word;
	stringstream ss(sentance);

	while (ss >> word)
	{
		wordCheck = d.findWord(word, words);
		if(!wordCheck)
		{
			doc.missingMap(word, lineNum);
		}
	}
	doc.displayMap();

}
string letterCheck(string sentance)
{
	for(unsigned i = 0; i < sentance.length(); i++)
		{
			if (!isalpha(sentance[i]))
			{
				sentance[i] = ' ';
			}
		}
	return sentance;
}
int main(int argc, char* argv[])
{
	dictionary dic;
	document doc;
	set<string> words;
	set<string>::iterator it;
	string doc_word;
	int lineNum = 1;
	ifstream in;
	in.open(argv[1]);
	string word;

	while (in >> word)
	{
		transform(word.begin(), word.end(), word.begin(), ::tolower);
		words.insert(word);
	}
	
	in.close();

	//dic.makeSet(words);
	
	ifstream in2;
	in2.open(argv[2]);
	while (getline(in2, doc_word))
	{
		transform(doc_word.begin(), doc_word.end(), doc_word.begin(), ::tolower);
		doc_word = letterCheck(doc_word);
		sentancetoword(doc_word, words, lineNum);
		lineNum++;

	}

	in2.close();
	
	system("pause");
	return 0;

}


Like LB and NT3 I'm having trouble seeing how the output could possibly be what you say it is.

With a few modifications so your code makes sense:
http://ideone.com/4TTIRg
You can't give us engine and the exhaust pipe and then ask us why your car is having unintended accelerations. We need to see the whole thing.
here are the text files that im reading in:
list of words
1
2
3
4
5
6
7
8
9
10
if
is
in
of
be
bugs
the
then
them
must


document to word check
1
2
3
4
If debugging is the
process of removing bugs.
Then programming must be the
process of putting them in.


The program should read in the files here in the main as well as make a set of the word list. Then it reads in the document line by line breaking it up to check each word if it is found on the list. If its not, then its put into a map as the word with the line number. Here is all the code i have starting with the main function:

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
72
73
74
75
76
77
78
79
80
#include <iostream>
#include <string>
#include <vector>
#include <set>
#include <algorithm>
#include <fstream>
#include "dictionary.h"
#include "document.h"

using namespace std;

void sentancetoword(string sentance, set<string> words, int lineNum)
{
	dictionary d;
	document doc;
	bool wordCheck;
	string word;
	stringstream ss(sentance);

	while (ss >> word)
	{
		wordCheck = d.findWord(word, words);
		if(!wordCheck)
		{
			doc.missingMap(word, lineNum);
		}
	}
	doc.displayMap();

}
string letterCheck(string sentance)
{
	for(unsigned i = 0; i < sentance.length(); i++)
		{
			if (!isalpha(sentance[i]))
			{
				sentance[i] = ' ';
			}
		}
	return sentance;
}
int main(int argc, char* argv[])
{
	dictionary dic;
	document doc;
	set<string> words;
	set<string>::iterator it;
	string doc_word;
	int lineNum = 1;
	ifstream in;
	in.open(argv[1]);
	string word;

	while (in >> word)
	{
		transform(word.begin(), word.end(), word.begin(), ::tolower);
		words.insert(word);
	}
	
	in.close();

	//dic.makeSet(words);
	
	ifstream in2;
	in2.open(argv[2]);
	while (getline(in2, doc_word))
	{
		transform(doc_word.begin(), doc_word.end(), doc_word.begin(), ::tolower);
		doc_word = letterCheck(doc_word);
		sentancetoword(doc_word, words, lineNum);
		lineNum++;

	}

	in2.close();
	
	system("pause");
	return 0;

}

dictionary class
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
#include "dictionary.h"


dictionary::dictionary(void){}
dictionary::~dictionary(void){}

set<string> dictionary::makeSet(set<string>insert)
{
	set<string>::iterator it;
	for (it = insert.begin(); it != insert.end(); it++)
	{
		words.insert(*it);
	}
	return words;
}

bool dictionary::findWord(string word, set<string> words)
{
	bool verify;
	if (words.count(word) == 0)
	{
		verify = false;
	}
	else if (words.count(word) == 1)
	{
	    verify = true;
	}
	return verify;
}

the document class
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#include "document.h"


document::document(void){}
document::~document(void){}

void document::missingMap(string word, int lineNum)
{
	misspelled[word].push_back(lineNum);		
}
void document::displayMap()
{
	for (map<string, vector<int>>::iterator i = misspelled.begin(); i != misspelled.end(); i++)
	{
		cout << i->first << ": ";
		for (vector<int>::iterator j = i->second.begin(); j != i->second.end(); j++)
		{
			cout << *j << " ";
		}
		cout << "\n";
	}

}


This is all the code ive done. I hope this can help piece together the problem. It all comes down to that output, i just dont understand how it could be outputting what it is so hopefully this helps
if anyone could see any issues let me know that would be awesome
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
#include <iostream>
#include <string>
#include <sstream>
#include <set>
#include <cctype>
#include <map>
#include <vector>

std::string& tolower( std::string& str )
{
    for( char& c : str ) c = std::tolower(c) ;
    return str ;
}

std::string trim_punct( std::string str )
{
    while( !str.empty() && std::ispunct( str.back() ) ) str.pop_back() ;
    return str ;
}

bool not_in_dictionary( const std::string& word, const std::set<std::string>& dict )
{ return dict.find(word) == dict.end() ; }

int main()
{
    std::set<std::string> dict ;

    // initalize dictionary
    {
        std::istringstream dict_stm( "if is in of be bugs the then them must" ) ;
        std::string word ;
        while( dict_stm >> word ) dict.insert( tolower(word) ) ;
    }

    std::map< std::string, std::vector<int> > misspelt ;

    // read file
    {
        std::istringstream doc_stm
        (
            "If debugging is the\n"
            "process of removing bugs.\n"
            "Then programming must be the\n"
            "process of putting them in.\n"
            "for testing: puTTing process, debugging++ reMOVing programmING.\n"
        );

        int line_number = 1 ;
        std::string line ;
        while( std::getline( doc_stm, line ) )
        {
            std::istringstream stm(line) ;
            std::string word ;
            while( stm >> word )
            {
                word = trim_punct( tolower(word) ) ;
                if( not_in_dictionary(word,dict) ) misspelt[word].push_back(line_number) ;
                ++line_number ;
            }
        }
    }

    // print misspelt
    for( const auto& pair : misspelt )
    {
        std::cout << pair.first << " [ " ;
        for( int n : pair.second ) std::cout << n << ' ' ;
        std::cout << "]\n" ;
    }
}

http://coliru.stacked-crooked.com/a/10ae68c7868dfc1e
Topic archived. No new replies allowed.