Thread: Get stringstream with separator!!!

  1. #16
    Registered User
    Join Date
    May 2009
    Posts
    30
    This is also help us to understand the bintree and implement it into the assignment. which is i have to implement the bintree into my work. The app will read the file with multiple dilimiter in 1 file. As i have searching on the Internet, they said that we have to overload getline or checking each char to compare with dilimiter.

    Your code is very short and simple, but the thing is i have to do as the specification so it need times for searching and doing it.

    I m very appreciate you help.

  2. #17
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Right, bintree is a class template for an AVL tree, which is a balanced binary tree. The basic concepts of its usage to solve your problem are the same as with std::map, hence you can start by using std::map.

    Quote Originally Posted by cubimongoloid
    The thing here is the lecturer provide bintree.h and binnode.h (his own work so you have to implement it in to your assignment. Because when submit, i can online submit 1 file then it will be combined with two those file to make the app work.
    So, you are only supposed to submit the file that implements the main function that reads in words and prints the words and their corresponding counts, but you also have to implement, but not submit the code for, an AVL tree using the interface provided by your lecturer?
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #18
    Registered User
    Join Date
    May 2009
    Posts
    30
    yes. Just that. That is the template of bintree in the lecture which we may or maynot need to understand (hhaha...) but we can still write the code.

    This is the first subject about programming at uni but it turns out so hard for me. We had Java before but means nothing. last 2 assignment i did try best but still need some help.

    Anyway, i think the basic concept for solving the problem is the same. This assignment we don't know his input file to test. So we have to make it ourselves to get an output.

    The problems is the multi dilimiter in one file. i can be mixed up every where in the file.

  4. #19
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by cubimongoloid
    The app will read the file with multiple dilimiter in 1 file. As i have searching on the Internet, they said that we have to overload getline or checking each char to compare with dilimiter.
    As I have noted, I would prefer overloading operator>>. You could overload getline(), but using a Word class would make things easier.

    So, once you have implemented the Word class and tested it with std::map, you can then adapt it to work with bintree. Your lecturer's bintree class template provides the interface of an AVL tree rather than a map, so you would need to manually associate a key (the Word object) with a value (the count). Then can be done with yet another class, but this time one that is much smaller since all it does is model a key/value pair.

    Keep in mind: concentrate on parsing the input correctly first. You can implement the AVL tree later when you have confidence that your parsing is correct.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #20
    Registered User
    Join Date
    May 2009
    Posts
    30
    It seems to be stupid but how can i parsing delimiter inside operator>>
    Code:
    stream >> word.s;
     and
    words w(temp);
    the app will read the line then put temp into istream OR istream do the part put temp in to buffer then ostream will print.

    Could i get a hint of how can i compare separator? is it as normal as if else...???

  6. #21
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by cubimongoloid
    the app will read the line then put temp into istream OR istream do the part put temp in to buffer then ostream will print.
    I suggest that you store a std::string as a member variable for the Word object. You will then store the word read into that std::string, and print that std::string to print the Word object.

    Quote Originally Posted by cubimongoloid
    Could i get a hint of how can i compare separator? is it as normal as if else...???
    You could read character by character, ignoring separator characters (including periods) until you read a character that should be in a word, then keep reading until you reach a separator character (with special handling for periods). You may want to place that last separator character back into the input stream with the putback() member function of std::istream.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  7. #22
    Registered User
    Join Date
    May 2009
    Posts
    30
    Should we define in Words class the string and double for the decimal number. Because it also will be read (u can have a look back to the result). i read the book, it said cin on istream will be continued and separated by white space.

  8. #23
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by cubimongoloid
    Should we define in Words class the string and double for the decimal number.
    No, work with words. Trying to turn the class into a numeric type would just complicate the issue.

    Quote Originally Posted by cubimongoloid
    i read the book, it said cin on istream will be continued and separated by white space.
    Yes, but you have more than whitespace to think about.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  9. #24
    Registered User
    Join Date
    May 2009
    Posts
    30
    i have try but still not know that to getline into istream. Most of the example is use cin for istream.
    So should i call the cin instead of getline. Because they are at 2 separate function.

    Code:
    void loadFile(const string file)
    {
       ifstream fin;
       fin.open(file.c_str());
       string temp;
       
       if (!fin)
       {
          cout << "\nERROR - Unable to access " << file;
          exit(0);
       }
       else
       {
          cout << "\nfilename : " << file << endl << endl;
          while (fin.good())
          {
             getline(fin, temp);
             words w(temp);
             cout << w;
          }
       }
    }
    and
    istream &operator>> (istream &stream, words &word)
    {
       stream >> word.s; //is this put stream into words class
       return stream;
    }

  10. #25
    Registered User linuxdude's Avatar
    Join Date
    Mar 2003
    Location
    Louisiana
    Posts
    926
    After you have the line as a string, you should go through the string character by character
    Code:
    std::string new_word;
    std::string::const_iterator it = temp.begin();
    while(it != temp.end()) {
       if(*it == '>' || *it == '<' || *it == '.') {  // whatever else you are looking for
          // insert new_word into avl tree
          new_word.clear(); // Empty the string
       }
       else {
          new_word += *it;
       }
       it++;
    }
    // insert the last new_word into the avl tree, because got to the end.
    That way you have now parsed all the words that you got with getline.
    Last edited by linuxdude; 05-15-2009 at 09:59 AM.

  11. #26
    Registered User
    Join Date
    May 2009
    Posts
    30
    Thx alot. I will try it.

  12. #27
    Registered User
    Join Date
    May 2009
    Posts
    30
    I defined a vector for a word. so when the *itr meet some special symbol, it will finish the vector for a word. For example: Today!. a vector for T o d a y, when *itr see symbol "!" it will terminate and form a Today word which will be added to the tree. But i cannot figure out how to do that.
    Code:
    vector<char> ch;
    getline(fin, temp);
             string::const_iterator itr = temp.begin();
             while(itr != temp.end())
             {
                ch.push_back(*itr);
                if(*itr == '>' || *itr == '<' || *itr == '.' || *itr == ' ')
                {
                   if (!new_word.empty())
                   {
                      //word.insert(words(new_word));  //line not work for me. It was wrong with the template of avl tree
                      //word.insert(new_word(ch)); // instantiated from here 
                      new_word.clear(); // Empty the string
                      i++;
                   }
                }
                else
                {
                   new_word += *itr;
                }
                itr++;
             }
    The question is "as laserlight said, i can use istream and ostream. Is the way are different or ...???
    Last edited by cubimongoloid; 05-17-2009 at 11:03 PM.

  13. #28
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    What is word a vector of? std::strings? If so, then don't make ch a vector<char>, but a std::string instead. (You can do push_back on a string just like a vector.)

  14. #29
    Registered User
    Join Date
    May 2009
    Posts
    30
    I did go to this far. I wanna implement istream and ostream to bintree but hvn't known yet.
    This is the code by far i have recognized the duplicate word in the tree. I wanna the istream and ostream to put the count for duplicate word.
    1 more trouble is few last word of the file didn't display on the screen and tree.

    In some book and template. ostream related to bintree by defined
    ostream &operator>> (ostream &os, bintree<words> word);
    i did try but it cannot work.

    Code:
    words w(new_word); //for ostream i have to use this word class. It not related to bintree
                          //should i define bintree<words> s in class words instead of string s.
                
                if (isspace(*itr) || *itr == '>' || *itr == '<'  || *itr == '!' || *itr == ',') 
                {
                   if (word.find(new_word))
                   {
                         i++;
                         new_word.clear();
                   }
                   else
                   {
                      word.insert(words(new_word));
                      cout << w; //using ostream to display the output
                      new_word.clear(); // Empty the string
                   }
                }
                else
                {
                   new_word += *itr;
                }
                itr++;
    and text file
    Code:
    Today!is-not-a-good-day, the<problem<is<this "<"
    More.about.decimal 0.2abc 0.2 .2abc .2 abc.2abc abc.2 abc0.20   .2 . 2
    This is the quote "I don't know this will work".
    EOF
    the display shown up till "will" then finish.

    thx and regard
    Last edited by cubimongoloid; 05-18-2009 at 10:30 PM.

  15. #30
    Registered User linuxdude's Avatar
    Join Date
    Mar 2003
    Location
    Louisiana
    Posts
    926
    That's because after the loop there is something in new_word, but you didn't put it in, because the loop finished. After the while loop, you need to insert into the word vector and you'll have the last part.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. stringstream problem
    By black_spot1984 in forum C Programming
    Replies: 3
    Last Post: 10-28-2008, 04:09 PM
  2. stringstream clear
    By misterowakka in forum C++ Programming
    Replies: 3
    Last Post: 01-01-2008, 01:03 PM
  3. Socket: send stringstream
    By crisis in forum C++ Programming
    Replies: 5
    Last Post: 11-22-2007, 10:50 AM
  4. static separator
    By Bleech in forum Windows Programming
    Replies: 2
    Last Post: 10-21-2007, 03:47 PM
  5. Length of a stringstream
    By nickname_changed in forum C++ Programming
    Replies: 1
    Last Post: 10-24-2003, 07:30 PM