Thread: Struct && vector display empty .....

  1. #1
    Registered User
    Join Date
    Jan 2006
    Posts
    141

    Struct && vector display empty .....

    hi everyone, i dont know what is the problem of my code. here is the code...

    this is my .h
    Code:
    #include <string>
    #include <iostream>
    #include <vector>
    #include "stringset.h"
    
    using namespace std; 
    
    
    class InvertedIndex 
    {
    //insert data member here
      struct inputType
      {
          string word;
          string file;
      };
      
      vector<inputType> list;
    
    public:
     
      // Tally adds one word/file pair to the index 
      void tally(string word, string file);
    and this is my implementation file

    Code:
    #include "inverted_index.h"
    #include "osdir.h"
    
    // InvertedIndex::tally
    //   Pre: a word and file pair as strings
    //   Post: adds the file to the index under word
    void InvertedIndex::tally(string word, string file) 
    {
         inputType input;
             
         list.push_back( input );
    }
    
    and this is my main code

    Code:
    #include "inverted_index.h"
    
    int main() 
    {
    
      
      // test the InvertedIndex 
     InvertedIndex idx;
     idx.tally("cat", "cats.txt");
     idx.tally("cat", "animals.txt");
     idx.tally("cat", "everything.txt");
     idx.tally("dog", "dogs.txt");
     idx.print();
    the purpose of the program is to insert the string which i decalre it to string word and string file . but when i push_back to vector.. and display it... there are empty in vector....

    can someone let me know what is wrong with the code please...

    will highly appreciated...

  2. #2
    Registered User Tonto's Avatar
    Join Date
    Jun 2005
    Location
    New York
    Posts
    1,465
    Code:
      struct inputType
      {
          inputType(string s, string t) : word(s), file(t) { }
    
          string word;
          string file;
      };
    Code:
    void InvertedIndex::tally(string word, string file) 
    {
         inputType input(word, file);
             
         list.push_back( input );
    }
    You need to construct the inputType object with the stuff before putting it into the vector. I'm not very good with this whole expressing myself thing.

  3. #3
    Registered User
    Join Date
    Jan 2006
    Posts
    141
    thank you tony.. it works... appreciate tat help....

  4. #4
    Registered User Tonto's Avatar
    Join Date
    Jun 2005
    Location
    New York
    Posts
    1,465
    Who the hell is Tony?

  5. #5
    The superhaterodyne twomers's Avatar
    Join Date
    Dec 2005
    Location
    Ireland
    Posts
    2,273
    I don't know, but I'm going to call you tony for now on! Fare thee well, Tonto.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Converting from C to C++
    By Taka in forum C++ Programming
    Replies: 5
    Last Post: 04-08-2009, 02:16 AM
  2. Concatenating in linked list
    By drater in forum C Programming
    Replies: 12
    Last Post: 05-02-2008, 11:10 PM
  3. Replies: 16
    Last Post: 10-29-2006, 05:04 AM
  4. Binary Search Tree
    By penance in forum C Programming
    Replies: 4
    Last Post: 08-05-2005, 05:35 PM
  5. Contest Results - May 27, 2002
    By ygfperson in forum A Brief History of Cprogramming.com
    Replies: 18
    Last Post: 06-18-2002, 01:27 PM