Thread: adding to a set

  1. #1
    Registered User
    Join Date
    Sep 2006
    Location
    Kansas City
    Posts
    76

    adding to a set

    I am having trouble with adding ints form a file to a set container. Could someone provide a short example or point me to a tutorial somewhere. I searched all over google and could not find anything helpful.

    Thanks

  2. #2
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    How about posting the code you are having issues with?
    That helps avoid playing 20 questions.
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  3. #3
    Registered User
    Join Date
    Sep 2006
    Location
    Kansas City
    Posts
    76
    Here is what I have so far
    Code:
    #include <iostream>
    #include <stdlib.h>
    #include <vector>
    #include <fstream>
    #include <string>
    #include <set>
    
    using namespace std;
    
    class Numbers 
    {
          public:
                void add ( int x_p ) ;  // add item to set
                Set () ;     // creates empty set
            private:		
                vector <int>  intigers ;
    };
    
    
    int main()
    {
    
         Numbers num;
    
          ifstream in_data;
          in_data.open ("D:\\numbers.txt");
          if ( ! in_data)
          {
            cout << "File not opened" << endl;
            exit (1);
          }
    
          cout << "How many ints do you want to add ?" << endl;
          int z;
          cin >> z;
          for (int i = 0 ; i < z ; i++)
          {
            cout << "Enter an int" << endl;
            int x ;
            cin >> x;
            num.add (x) ;  
    
          }
    
    
          system("PAUSE");
          return 0;
    }
    
    
    void Numbers::add (int x_p)
    {
    
    }

  4. #4
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    My first issue is a typo:
    Code:
                Set () ;     // creates empty set
    The language is case-sensitive; got something that compiles for me?
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  5. #5
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    Your class has a vector, but you said you want a set. Just change vector to set. Then, use the insert function from the set class.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 8
    Last Post: 01-18-2008, 04:06 AM
  2. Replies: 4
    Last Post: 01-13-2008, 02:14 AM
  3. Replies: 7
    Last Post: 08-19-2007, 08:10 AM
  4. Linking OpenGL in Dev-C++
    By linkofazeroth in forum Game Programming
    Replies: 4
    Last Post: 09-13-2005, 10:17 AM
  5. Program help
    By Trebor in forum C++ Programming
    Replies: 4
    Last Post: 06-04-2002, 03:19 PM