Solved

Incorrect output.

Posted on 2004-10-28
284 Views
Last Modified: 2010-04-15
==============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;
}
0
Question by:komlaaa
    13 Comments
     
    LVL 86

    Expert Comment

    by:jkr
    And where are you calling 'getMostCommonWord()'? Be sure to use string::c_str() when writing a string to cout.
    0
     
    LVL 55

    Expert Comment

    by:Jaime Olivares
    Try to check if your map object is correctly filled. Create a printing function to print entire map contents.
    0
     
    LVL 5

    Expert Comment

    by:van_dy
    ok post the part of the code where u are printing out the most common word
    0
     

    Author Comment

    by:komlaaa

    ===============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
     
    LVL 86

    Expert Comment

    by:jkr
    >>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
     
    LVL 5

    Expert Comment

    by:van_dy
    does it produce the right output?
    i think you arent calling in your main
    0
     
    LVL 5

    Accepted Solution

    by:
    try this in your main it should work

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

    Expert Comment

    by:jkr
    >> 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
     
    LVL 5

    Expert Comment

    by:van_dy
    >>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
     
    LVL 5

    Expert Comment

    by:van_dy
    >>       std::string name = "xyz";           // :)
    0
     

    Author Comment

    by:komlaaa
    sorry i delay to accept that answer.... doing other stuff.  --- yeah i forgot to call w.mostCommonWord();
    int main.
    0
     
    LVL 86

    Expert Comment

    by:jkr
    >>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
     
    LVL 5

    Expert Comment

    by:van_dy
    Well, i have seen examples using cout << somestring, even in
    Stroustrup's book. though, it does reccommend using .c_str() with
    printf().
    0

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    How your wiki can always stay up-to-date

    Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
    - Increase transparency
    - Onboard new hires faster
    - Access from mobile/offline

    Have you thought about creating an iPhone application (app), but didn't even know where to get started? Here's how: ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ Important pre-programming comments: I’ve never tri…
    This is a short and sweet, but (hopefully) to the point article. There seems to be some fundamental misunderstanding about the function prototype for the "main" function in C and C++, more specifically what type this function should return. I see so…
    Video by: Grant
    The goal of this video is to provide viewers with basic examples to understand and use while-loops in the C programming language.
    The goal of this video is to provide viewers with basic examples to understand and use conditional statements in the C programming language.

    846 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    3 Experts available now in Live!

    Get 1:1 Help Now