If this is your first visit, be sure to
check out the FAQ by clicking the
link above. You may have to register
before you can post: click the register link above to proceed. To start viewing messages,
select the forum that you want to visit from the selection below.

[RESOLVED] Trees (Maps) problem

Hello, I am trying to use a red-black tree, STL Map, to read in a file that counts the words while ignoring punctuation and converting all words to lowercase. My problem occurs when I try to print and nothing outputs to the screen. I think it has something to do with my for loop in my print function. I have looked over it many times along with some of my fellow students and everyone seems to think it too should be working. I probably have done something stupid and really would appreciate an expert or someone who knows this more than I do to point out my mistake, anything, thanks.

Re: [RESOLVED] Trees (Maps) problem

When something doesn't work as it should rather than trying to guess the problem and just trying things 'to see if they work', use the debugger to trace through the code to see where it deviates from what was expected. You should become as familiar with the debugger as writing/compiling programs. Debugging programs is a skill that has to be learnt. Some of us who have been around programming for a long time seem to spend half our life in the debugger!

All advice is offered in good faith only. You are ultimately responsible for effects of your programs and the integrity of the machines they run on.

Re: [RESOLVED] Trees (Maps) problem

1) Although this does not affect your program, you
should not put "using namespaec std;" in your
header file. It can cause name clash problems.
You should fully qualify the variable type. Example:

Code:

void print(std::map<std::string, int> &tree);// print

2) print() does not modify any variable in the Map class.
Therefore it should be a const function:

Code:

void print(std::map<std::string, int> &tree) const; // print

3) additionally, print does not modify the passed argument,
so you should pass by const reference, not by reference.
You would need to use const_iterator instead of iterator
in the function.

Code:

void print(const std::map<std::string, int> &tree) const; // print

4) in main, you call:

Code:

mapper.print(words);

But words is a local variable to main that is never modified.
That is why you did not see any output.

5) in your header, you comment out the line:

Code:

//map<string, int> words;

And instead you have "words" a local variable in the inputfile
function. I think you should keep the member variable words
and remove the local variable from inputfile. You would not
need to pass the map to print() and insertTree. Just make
sure those functions use the member variable, words.

6) There is nothing wrong with the way you coded insertTree,
but it can be simplified to one line (but it requires a little
more advanced knowledge on how operator [] works for std::map).

* The Perfect Platform for Game Developers: Android
Developing rich, high performance Android games from the ground up is a daunting task. Intel has provided Android developers with a number of tools that can be leveraged by Android game developers.

* The Best Reasons to Target Windows 8
Learn some of the best reasons why you should seriously consider bringing your Android mobile development expertise to bear on the Windows 8 platform.