C++ n00b error: vector<Class>.push_back error? Does the size of variable matter?


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 3 of 3

Thread: C++ n00b error: vector<Class>.push_back error? Does the size of variable matter?

  1. #1
    Join Date
    Apr 2009
    Posts
    2

    C++ n00b error: vector<Class>.push_back error? Does the size of variable matter?

    I'm quite new to C++ and have been solving this uni exercise. (it's actually an assignment, but I haven't been able to finish it on time, but I thought I might try to make it work and learn about C++)..

    So, here is part of the description of the program:

    It has to read a text file word by word and then separate single letter words from other size words. Other various-sized letters are to be stored and calculate an average word length. But I have already worked out how the other part should work.

    PROBLEM IS: single character words can be read in and store into singleWordList by calling the storeWord method. But it wouldn't work for bigWordList, which is using the exact same class as singleWordList.

    I'm using two classes:
    1. Occurrence for storing word length and occurrence count
    2. WordList for storing vector of Occurrence and a vector of string (for storing the word itself)

    Here is how I uses the classes in main():

    int main()
    {
    ifstream file("text.txt");
    string text;
    WordVector bigWordList, singleWordList;

    file >> text;

    while (!file.eof())
    {
    if ( text.length() == 1 )
    {
    singleWordList.storeWord(text);
    }
    if ( text.length() > 1 )
    {
    bigWordList.storeWord(text);
    cout << "FOUND SOME BIG WORDS" << endl;
    }
    file >> text;
    }


    Here is the storeWord method from WordVector class:

    /**
    * Store the word read in from file into WordList vector and WordCount vector
    */
    void WordVector::storeWord(string str)
    {
    wordList.push_back(str);

    /**
    * If wordCount vector has an Occourrence object with the same word length,
    * increase the occurrence count.
    * If not, make a new Occurrence object, set length and occurrenceCount fields,
    * then push it onto the wordCount vector.
    */

    if ( wordCountList.size() == 0 )
    {
    newOccr.setLength(str.length());
    newOccr.setOccurrenceCount(true);
    wordCountList.push_back(newOccr);
    }
    else
    {
    for (int i=0; i< wordCountList.size() ; i++)
    {
    if (wordCountList[i].getLength() == str.length())
    {
    wordCountList[i].setOccurrenceCount(true);
    }
    else
    {
    newOccr.setLength(str.length());
    newOccr.setOccurrenceCount(true);
    wordCountList.push_back(newOccr);
    }
    }
    }
    }


    My text file contains: A is the first word of English alphabet. Z is the last.

    And the output is:

    FOUND SOME BIG WORDS
    FOUND SOME BIG WORDS
    FOUND SOME BIG WORDS
    FOUND SOME BIG WORDS
    FOUND SOME BIG WORDS
    FOUND SOME BIG WORDS
    FOUND SOME BIG WORDS
    FOUND SOME BIG WORDS
    FOUND SOME BIG WORDS
    SINGLE WORDS
    -------------
    A
    Z
    BIG WORDS
    -----------
    bigWordList size is: 0Press any key to continue

    Is the error something to do with push_back method called on the vector?

    I appreciate all your help and advice. Thanks in advance.
    Kenn

  2. #2
    Join Date
    Dec 2007
    Posts
    401
    > It has to read a text file word by word and then separate single letter words from other size words.
    > Other various-sized letters are to be stored and calculate an average word length.

    Aren't you going about it in a really round about manner?

    Code:
    #include <iostream>
    #include <fstream>
    #include <vector>
    #include <string>
    
    int main()
    {
       std::vector< std::string > single_letter_words, longer_words ;
       std::ifstream file( "text.txt" ) ;
    
       std::string word ;
       // read word by word till eof and append to the appropriate vector
       while( file >> word )
          if( word.size() > 1U ) longer_words.push_back(word) ;
          else single_letter_words.push_back(word) ;
    
       // sum up the lengths of all the longer words
       std::size_t total = 0 ;
       for( std::size_t i = 0 ; i<longer_words.size() ; ++i )
           total += longer_words[i].size() ;
    
       // and divide by their count to get the average size
       std::cout << "average length: " << double(total) / longer_words.size() << '\n' ;
    }
    > Is the error something to do with push_back method called on the vector?

    No. But it is impossible to say what it is without seeing the whole code.

  3. #3
    Join Date
    Apr 2009
    Posts
    2
    Quote Originally Posted by vijayan View Post
    > It has to read a text file word by word and then separate single letter words from other size words.
    > Other various-sized letters are to be stored and calculate an average word length.

    Aren't you going about it in a really round about manner?
    You mean reading the text file? The way I do is, the text file is read only once, while all words are stored in the two classes with 'string' vectors.

    Quote Originally Posted by vijayan View Post
    > Is the error something to do with push_back method called on the vector?

    No. But it is impossible to say what it is without seeing the whole code.
    Here's the whole code of main(). The other method, storeWord, is the only other I use for storing the words.

    Code:
    #include <iostream>
    #include <fstream>
    #include <string>
    #include <vector>
    
    #include "WordVector.h"
    
    using namespace std;
    
    int main()
    {
    	ifstream file("text.txt");
    	string text;
    	int i = 1;
    
    	WordVector bigWordList, singleWordList;
    
    	file >> text;
    
    	while (!file.eof())
    	{
    		if ( text.length() == 1 )
    		{
    			singleWordList.storeWord(text);
    		}
    		if ( text.length() > 1 )
    		{
    			bigWordList.storeWord(text);
    			cout << "FOUND SOME BIG WORDS" << endl;
    		}
    		
    		file >> text;
    	}
    	
            cout << "SINGLE WORDS" << endl << "-------------" << endl;
    	singleWordList.print();
    	cout << "BIG WORDS" << endl << "-----------" << endl;
    	bigWordList.print();
    	cout << "bigWordList size is: " << bigWordList.getWordListSize() << endl;
    	return 0;
    }
    By the way, I found the cause of it not printing.. I forgot to call the print method on the bigWordList. :) The big words are also able to stored too.

Similar Threads

  1. queues how to print it
    By xsouldeath in forum Java
    Replies: 0
    Last Post: 12-10-2005, 10:56 PM
  2. Replies: 3
    Last Post: 11-30-2005, 04:37 AM
  3. Size Does Matter - amongst other things
    By spudmasher in forum ASP.NET
    Replies: 2
    Last Post: 06-20-2005, 09:41 PM
  4. DOES SIZE MATTER?
    By SB in forum VB Classic
    Replies: 2
    Last Post: 02-04-2001, 09:01 AM
  5. Variable based array size
    By Nikhil in forum ASP.NET
    Replies: 2
    Last Post: 05-11-2000, 03:18 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center