Thread: Inserting words into an array

  1. #1
    Registered User
    Join Date
    Aug 2002
    Posts
    16

    Inserting words into an array

    I am trying to scan a list of words from a file. The list is as follows:

    hello
    this
    is
    a
    list

    Each subsequent word is on a new line. I'm trying to insert each word into an array, however, the code that I have only inserts the very first word - that's it.

    Code:
    void Array :: put()
    {
    	Array a;
    	ifstream in("small.txt");
    	
    	in >> a.buffer;
    	cout << a.buffer;
    	
    
    	in.close();
    }
    So the ouput would only be: Hello

    How can I modify this portion of my code to have it insert the entire list of words?

  2. #2
    Confused Magos's Avatar
    Join Date
    Sep 2001
    Location
    Sweden
    Posts
    3,145
    in >> a.buffer;

    This will only read one word at a time. If you want to read in every word in a file, you have to use a loop, which ends when the end of the file (eof) is encountered.
    Code:
    while(!in.eof())
    {
       in >> a.buffer;
       cout << a.buffer << endl;
    }
    Note that it will overwrite the earlier data when reading each word, so if you want to store all input at once you have to make another buffer and use strcat().
    Last edited by Magos; 10-17-2002 at 12:45 PM.
    MagosX.com

    Give a man a fish and you feed him for a day.
    Teach a man to fish and you feed him for a lifetime.

  3. #3
    Skunkmeister Stoned_Coder's Avatar
    Join Date
    Aug 2001
    Posts
    2,572
    The easy way is to use one of the getline() functions to read up to a '\n' delim char.
    Free the weed!! Class B to class C is not good enough!!
    And the FAQ is here :- http://faq.cprogramming.com/cgi-bin/smartfaq.cgi

  4. #4
    Registered User Gnoober's Avatar
    Join Date
    Oct 2002
    Posts
    40
    This could be a stupid question... but how do you know which line getline() is getting?
    - Grady (:

  5. #5
    Registered User
    Join Date
    Aug 2002
    Posts
    16
    I modified the code to look like this:

    Code:
    char buf[CAPACITY]; //global array
    
    void Array :: put()
    {
    	Array a;
    	ifstream in("small.txt");
    	
    	while (!in.eof())
                    {
                    in >> a.buffer >> buf[i];
                    i++;
    	cout << a.buffer;
    	}
    
    	in.close();
    }
    It was an attempt to store everything into another array (buf in this case). However, when I went to print out one of the elements of buf, it only printed out one letter, not a word. I'm wondering if a string array will work, but I'm not sure how to implement that. How does strcat() work?
    Last edited by Rizage; 10-17-2002 at 08:19 PM.

  6. #6
    Registered User Gnoober's Avatar
    Join Date
    Oct 2002
    Posts
    40
    To implement a string array, use pointers to char arrays.
    Example:
    Code:
    char a[], b[];
    strcpy (a, "STRING1");
    strcpy (b, "STRING2");
    
    char * c[];
    
    c[0] = a;
    c[1] = b;
    I hope you get the idea, I'm kind of in a hurry


    - Grady (:

  7. #7
    Registered User
    Join Date
    Aug 2002
    Posts
    16
    I don't follow...

  8. #8
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    I gather that you are not inserting words into an arbitrary position in the array. So something like what Gnoober's code would be fine. But realize that Gnoober's code is also wrong. Try something like this:

    Code:
    #define MAX_ARRAY 100
    char *a, *b;
    char *c[MAx_ARRAY];
    
    a = new char[16];
    b = new char[16];
    strcpy (a, "STRING1");
    strcpy (b, "STRING2");
    
    c[0] = a;
    c[1] = b;
    
    //don't forget to delete this stuff later
    You could also use vectors. The above code works but is limited to 100 words (or whatever MAX_ARRAY is defined as).

  9. #9
    Banned Troll_King's Avatar
    Join Date
    Oct 2001
    Posts
    1,784
    You can allocate a large (or smaller) 2d array:

    Code:
    #include <iostream>
    
    int main()
    {
      int **ppArray = 0;
    
      //create ppArray[10][10]
      ppArray = new int* [10];
      for ( int ix = 0; ix < 10; ++ix )
        {
          ppArray[ix] = new int[10];
        }
    
     
      for (int ix =0; ix < 10; ++ix )
        delete [] ppArray [ix];
      delete [] ppArray;
    
      return 0;
    Last edited by Troll_King; 10-18-2002 at 03:35 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 16
    Last Post: 05-29-2009, 07:25 PM
  2. [question]Analyzing data in a two-dimensional array
    By burbose in forum C Programming
    Replies: 2
    Last Post: 06-13-2005, 07:31 AM
  3. Class Template Trouble
    By pliang in forum C++ Programming
    Replies: 4
    Last Post: 04-21-2005, 04:15 AM
  4. Array Program
    By emmx in forum C Programming
    Replies: 3
    Last Post: 08-31-2003, 12:44 AM
  5. extracting words from an array of words
    By axon in forum C++ Programming
    Replies: 2
    Last Post: 04-02-2003, 11:21 PM