Thread: Add to map using ifstream (homework)

  1. #1
    Registered User
    Join Date
    Apr 2012
    Posts
    15

    Add to map using ifstream (homework)

    We're doing the "How Close to Kevin Bacon" problem in class. To do it, we are building a graph using maps and sets. I'm still new to C++, and am having problems with the very beginning (reading the file). Here is a sample line from the file:

    43: The Richard Petty Story (1974)/McGavin, Darren/Beery Jr., Noah/Petty, Richard (I)/Jalbert, Pierre/Jones, L.Q./Whittington, Jerry (I)/Browne, Kathie

    Where the first part is the title of the movie, and everything else is a list of the actors (all delimited by "/"). Apparently, the movie should just be a string, while all the actors should be added to a set. Then, they will all be added to the map together. Here is the header file provided:

    Code:
    #ifndef GRAPH_H
    #define GRAPH_H
    #include <string>
    #include <map>
    #include <set>
    
    using namespace std;
    
    class Graph {
        public:
            Graph();
            Graph(string);
            Graph(const Graph& orig);
            virtual ~Graph();
            void addEdge(string v, string w);
            void adjacentTo(string,
                            set<string>::const_iterator &, 
                            set<string>::const_iterator &);
            void vertices(map<string,set<string> >::const_iterator &,
                          map<string,set<string> >::const_iterator &);
        private:
            map <string, set<string> > ST;
    };
    #endif
    /* GRAPH_H */
    Apparently, it should only be around two lines total, which makes me feel fairly stupid, to be honest. I've used ifstream a little, and am pretty sure it will start something like:
    Code:
    ifstream myFile(str) // str is the name of the file passed in
    getLine(myFile, movieName, "/"
    But I don't know how to add everything to the set. I'm sure it's probably simple, but I think I've reached max capacity for this semester. Any help would be appreciated. Thanks.

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Hint: use a loop. A while loop could work.

    EDIT:
    Actually, that's not quite so simple, since there will be other lines in the file as well. An approach that can be used is to first use getline to read line by line into a string. Then you create a stringstream using this string and extract the movie name with a getline call using the '/' delimiter. Then the while loop using getline with '/' comes into play for the list of actors.
    Last edited by laserlight; 04-23-2012 at 09:04 PM.
    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. #3
    Registered User
    Join Date
    Apr 2012
    Posts
    15
    I'm using a while loop like this: (while myFile.good()). I figured I needed another for the rest of the line. Here's what I tried:
    Code:
    while(myFile.good())
        {
            getline(myFile,line, '/');
            myFile >> movie;
            while(myFile.good())
            {
                
                    myFile >> temp;
                    mySet.insert(temp);
            }
        }
    NetBeans says build failed but gives no error...

  4. #4
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,817
    What laserlight suggested is more like:
    Code:
    while(getline(myFile,line))
    {
        // Initialize stringstream with line
        // Extract movie title using getline on the stringstream using '/' as delimiter
        while( use getline on the stringstream using '/' as delimiter to extract the individual actors )
        {
            // Insert actor name into map/set
        }
    }
    [edit]It's generally not a good idea to put "using namespace std" inside of a header. You should instead remove that line and explicitly qualify any necessary objects with std:: as needed. The more appropriate place for such lines is in your source file(s).[/edit]
    Last edited by hk_mp5kpdw; 04-24-2012 at 05:37 AM.
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  5. #5
    Registered User
    Join Date
    Apr 2012
    Posts
    15
    Ok, got it working. Thanks for the replies. Now I just have to figure out what to do with my vertices method. Out of curiosity, why is putting "using namespace std" in the header bad practice?

  6. #6
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by 9erNumber16
    why is putting "using namespace std" in the header bad practice?
    Because the header will be included in other source files that do not expect the using directive to be present. This could result in a name collision or unexpectedly change the meaning of the code. That said, if the using directive is in a restricted scope (e.g., in the body of an inline function in the header) it is okay.
    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. #7
    Registered User
    Join Date
    Apr 2012
    Posts
    15
    Ah, I think I understand. Come to think of it, I did have a couple name collisions I had to fix. Thanks again.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. ifstream
    By Know_Your_Role in forum C++ Programming
    Replies: 2
    Last Post: 12-10-2009, 07:05 AM
  2. ios::in with ifstream? Why?
    By siavoshkc in forum C++ Programming
    Replies: 4
    Last Post: 01-30-2006, 10:26 AM
  3. need help with ifstream
    By stillwell in forum C++ Programming
    Replies: 6
    Last Post: 06-20-2005, 09:23 AM
  4. ifstream
    By Klinerr1 in forum C++ Programming
    Replies: 2
    Last Post: 07-22-2002, 11:18 PM
  5. homework help about ofstream/ifstream.. help PLZ
    By kv2 in forum C++ Programming
    Replies: 1
    Last Post: 12-13-2001, 01:08 AM