[2 days left] What’s wrong with your cloud strategy? Learn why multicloud solutions matter with Nimble Storage.Register Now

x
?
Solved

Incorrect output.

Posted on 2004-10-28
13
Medium Priority
?
333 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
Comment
Question by:komlaaa
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 6
  • 4
  • 2
  • +1
13 Comments
 
LVL 86

Expert Comment

by:jkr
ID: 12436350
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
ID: 12436556
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
ID: 12437055
ok post the part of the code where u are printing out the most common word
0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 

Author Comment

by:komlaaa
ID: 12438401

===============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
ID: 12438432
>>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
ID: 12438441
does it produce the right output?
i think you arent calling in your main
0
 
LVL 5

Accepted Solution

by:
van_dy earned 1200 total points
ID: 12438465
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
ID: 12438494
>> 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
ID: 12438979
>>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
ID: 12438987
>>       std::string name = "xyz";           // :)
0
 

Author Comment

by:komlaaa
ID: 12439911
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
ID: 12440394
>>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
ID: 12441698
Well, i have seen examples using cout << somestring, even in
Stroustrup's book. though, it does reccommend using .c_str() with
printf().
0

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This tutorial is posted by Aaron Wojnowski, administrator at SDKExpert.net.  To view more iPhone tutorials, visit www.sdkexpert.net. This is a very simple tutorial on finding the user's current location easily. In this tutorial, you will learn ho…
Summary: This tutorial covers some basics of pointer, pointer arithmetic and function pointer. What is a pointer: A pointer is a variable which holds an address. This address might be address of another variable/address of devices/address of fu…
The goal of this video is to provide viewers with basic examples to understand and use structures in the C programming language.
The goal of this video is to provide viewers with basic examples to understand opening and writing to files in the C programming language.
Suggested Courses

656 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