Link to home
Start Free TrialLog in
Avatar of komlaaa
komlaaa

asked on

Incorrect output.

==============PLEASE NOTE THAT THIS IS A CONTINUATION OF THE PROBLEM at:
https://www.experts-exchange.com/questions/21180841/STDIN-STDOUT-STDERR.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;
}
Avatar of jkr
jkr
Flag of Germany image

And where are you calling 'getMostCommonWord()'? Be sure to use string::c_str() when writing a string to cout.
Try to check if your map object is correctly filled. Create a printing function to print entire map contents.
Avatar of van_dy
van_dy

ok post the part of the code where u are printing out the most common word
Avatar of komlaaa

ASKER


===============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;

}
>>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"
does it produce the right output?
i think you arent calling in your main
ASKER CERTIFIED SOLUTION
Avatar of van_dy
van_dy

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
>> try this in your main it should work

No. That should be

 std::cout<<"MOST COMMON WORD: "<< w.getMostCommonWord().c_str() << w.getMostCommonCount()<<" \n"
>>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;
}
>>       std::string name = "xyz";           // :)
Avatar of komlaaa

ASKER

sorry i delay to accept that answer.... doing other stuff.  --- yeah i forgot to call w.mostCommonWord();
int main.
>>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>)'
Well, i have seen examples using cout << somestring, even in
Stroustrup's book. though, it does reccommend using .c_str() with
printf().