Incorrect output.

==============PLEASE NOTE THAT THIS IS A CONTINUATION OF THE PROBLEM at:
http://www.experts-exchange.com/Programming/Programming_Languages/C/Q_21180841.html
for this reason i prefer to stay in this forum although all the code is mostly C++:
===========================================================
Instead of print a string i am printing a negative num. why?

================= wc.h from file==============================

class wc
{
 public:
  typedef  map<std::string, int> datastructure_type;
  typedef  datastructure_type::iterator iterator;

  //  void display();
  //  void read(ifstream & f);
  void read(istream & f);
  void mostCommonWord();
  void display(map<std::string, int>::iterator,
                      map<std::string, int>::iterator);

  int getTotalCount()const {return totalCount;}
  int getDistinctCount() const {return distinctCount;}
  int getMostCommonCount()const {return theMostCommonCount;}
  std::string getMostCommonWord()const { return themostCommonWord;} <===========here is the call
  void helpMenu();
  iterator begin() {return words.begin();}
  iterator end(){ return words.end();}

 private:
  datastructure_type words;
  int totalCount;
  int distinctCount;
  int theMostCommonCount;
  std::string themostCommonWord;
};
==============================read( ) and ... ===========================
void wc::read(istream &f)
{
   std::string word;
   int count = 0;

   //   f->seekg(0, ios::beg);
   f.seekg(0, ios::beg);
   f >> word;
   //  f >> word;

     //cout<<"Reading from file...\n"; //prompting

  //  while(f.peek() != EOF){
   //   while(f>>word){
     while(!f.eof()){
    removePunc(word);

if(passWordTest(word)){
  if( words.find(word) == words.end()  )
   words[word] = 0; //word is not in map set it count to 0 for the first time.
 words[word]++; //word is there, increment its count.
 count++; //count the number of word stored.

}
 f >> word;
}
  if(words.size() == 0)
{
  std::cout<<"File failed to open\n";
  exit(0);
}
std::cout<<endl;
  totalCount = count;
  distinctCount = words.size();
}

=======================wc::mostCommonWord()===================

void wc::mostCommonWord()
{
  map<std::string, int>::iterator itr = words.begin();
  map<std::string, int>::iterator last = words.end();

  std::string maxWord = itr->first;
  int max = itr->second;
  int saveMax = itr->second;

  itr++;
  while(itr != last)
    {
      if(itr->second > max)
      {
          max = itr->second;
          maxWord = itr->first;
      }
        itr++;
    }
  if(max == saveMax){
    std::cout<<"Conflict or no most commonly word found\n";
    exit(0);
  }

  themostCommonWord = maxWord;
  theMostCommonCount = max;
}
komlaaaAsked:
Who is Participating?
 
van_dyConnect With a Mentor Commented:
try this in your main it should work

       w.mostCommonWord();
 std::cout<<"MOST COMMON WORD: "<< w.getMostCommonWord() << w.getMostCommonCount()<<" \n";
0
 
jkrCommented:
And where are you calling 'getMostCommonWord()'? Be sure to use string::c_str() when writing a string to cout.
0
 
Jaime OlivaresSoftware ArchitectCommented:
Try to check if your map object is correctly filled. Create a printing function to print entire map contents.
0
Prepare for an Exciting Career in Cybersecurity

Help prevent cyber-threats and provide solutions to safeguard our global digital economy. Earn your MS in Cybersecurity. WGU’s MSCSIA degree program curriculum features two internationally recognized certifications from the EC-Council at no additional time or cost.

 
van_dyCommented:
ok post the part of the code where u are printing out the most common word
0
 
komlaaaAuthor Commented:

===============HERE THE FUNCTION THAT DISPLAY ===============
void wc::display(map<std::string,int>::iterator start,
map<std::string, int>::iterator stop)
{
  for (map<std::string,int>::iterator itr = start; itr != stop; itr++)
    std::cout <<itr->first << " " << itr->second << endl;

}
0
 
jkrCommented:
>>std::cout <<itr->first << " " << itr->second << endl;

That should be

std::cout <<itr->first.c_str() << " " << itr->second << endl;

As I wrote in my 1st comment: "Be sure to use string::c_str() when writing a string to cout"
0
 
van_dyCommented:
does it produce the right output?
i think you arent calling in your main
0
 
jkrCommented:
>> try this in your main it should work

No. That should be

 std::cout<<"MOST COMMON WORD: "<< w.getMostCommonWord().c_str() << w.getMostCommonCount()<<" \n"
0
 
van_dyCommented:
>>No. That should be

are you implying that this code has error?

#include <string>
#incldude <iostream>

int main()
{
      std::string name = "xyz"
            std::cout << name;
           return 0;
}
0
 
van_dyCommented:
>>       std::string name = "xyz";           // :)
0
 
komlaaaAuthor Commented:
sorry i delay to accept that answer.... doing other stuff.  --- yeah i forgot to call w.mostCommonWord();
int main.
0
 
jkrCommented:
>>are you implying that this code has error?

This code - yes *veg* :o)

Apart from that - sorry, to some extent to me it seems to be carved in stone that there's no 'ostream<T>& ostream<T>::operator<<(basic_string<T>)'
0
 
van_dyCommented:
Well, i have seen examples using cout << somestring, even in
Stroustrup's book. though, it does reccommend using .c_str() with
printf().
0
All Courses

From novice to tech pro — start learning today.