• C

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?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

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
van_dyCommented:
ok post the part of the code where u are printing out the most common word
0
Managing Security & Risk at the Speed of Business

Gartner Research VP, Neil McDonald & AlgoSec CTO, Prof. Avishai Wool, discuss the business-driven approach to automated security policy management, its benefits and how to align security policy management with business processes to address today's security challenges.

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
van_dyCommented:
try this in your main it should work

       w.mostCommonWord();
 std::cout<<"MOST COMMON WORD: "<< w.getMostCommonWord() << w.getMostCommonCount()<<" \n";
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
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
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.