Thread: Homework Question

  1. #1
    Registered User
    Join Date
    Feb 2008
    Posts
    21

    Homework Question (New one)

    First problem fixed, segfault problem below.





    <Ignore>Ok, so for my assignment, I need a list of list of list of ints. We're working with lists this week, so more elegant data structures are not allowed . In any case, I'm wondering if theres a way to 'name' each of these lists. More simply:

    Lets say I need a list of streets, each with a list of house numbers. This would be a list of list of ints, but I would also need a separate list of strings for the street names. I'm wondering if I could combine these somehow so that the list of list of ints somehow still contains the information on the street names.

    I can give any more information to help better explain the problem.</Ignore>
    Last edited by Jozrael; 02-16-2008 at 05:51 PM.

  2. #2
    Advanced Novice linucksrox's Avatar
    Join Date
    Apr 2004
    Location
    Michigan
    Posts
    198
    Well without using a more elegant data structure, no you can't combine strings with ints into one structure. I'm really not sure exactly what you're asking, but it sounds like you need a 2 dimensional array of ints and also an array of strings that correlates with the ints. Or instead of arrays, if you're actually using lists (from the STL) it's the same way. So you already said it:
    This would be a list of list of ints, but I would also need a separate list of strings for the street names.
    Do you still need help with the code?
    Last edited by linucksrox; 02-16-2008 at 11:54 AM.
    "What are all you parallelograms doing here?" - Peter Griffin (to Joe and his wheelchair buddies)

  3. #3
    The larch
    Join Date
    May 2006
    Posts
    3,573
    You could combine them by making a list of struct (or std::pair) that contain a string (the street name) and a list of ints (house numbers).
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  4. #4
    Registered User
    Join Date
    Feb 2008
    Posts
    21
    Alright, well, I 'solved' the first problem by throwing a whole series of classes into the equation. It works fine, except for the fact that I'm getting a segfault while running my program (its still not fully written, I just like to debug it continually as I write :x). I was wondering if I could get advice as to how to fix this particular segfault. I've narrowed it down to a set of for loops with some odd behavior. Here it is:

    Code:
    for(list<Route>::iterator itr = routes.begin(); itr != routes.end(); itr++) {
        for(list<Street>::iterator itr2 = itr->getStreets().begin(); itr2 != itr->getStreets().end(); itr2++) {
             blah blah;
        }
    }
    Oddly enough, when I comment out the for statements and just put each of the parts of them in their own command (like, taking each of the 3 parts of the for loop and putting it on its own line of code, not looped, its fine. Its just when looped together it segfaults.

    Each of my classes (Route, Street, etc) has a list of the next Class. I.e. route has a list of Street. The lists are not initialized to zero (since doing that caused funky problems of its own). Any ideas on where I might start looking? Thanks for your help, and thank you to the two people that already posted.

    EDIT: The problem lies in the second for loop: the program doesn't like to increment itr2. I'm guessing it goes out of bounds, but I'm not sure how to prevent this.
    Last edited by Jozrael; 02-16-2008 at 05:49 PM.

  5. #5
    Weak. dra's Avatar
    Join Date
    Apr 2005
    Posts
    166
    are you allowed to use structs?

    Code:
    struct street_n_numbers{
    
    std::string street;
    std::list<int> house_nums;
    };

  6. #6
    Registered User
    Join Date
    Feb 2008
    Posts
    21
    No, we're only allowed to use classes in this course. And this is how I setup my program.

    As to the segfault, I found some interesting information via some debugging cout statements...my memory management must be terrible, because the street names arent being written correctly to the variables =\.

  7. #7
    Registered User
    Join Date
    Feb 2008
    Posts
    21
    Well, I found an odd workaround to the problem, but I'm still not sure why it didn't work in the first place =\. I'll post it later (once I've met the deadline) so I can find out what I was doing wrong.

  8. #8
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    Structs are classes. But if your teacher is opposed to the struct keyword, you can do this:
    Code:
    class street_addresses
    {
    public:
      std::string street;
      std::list<int> house_nums;
    };
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Question About My Homework Pls Help Me İmmediately
    By jennyyyy in forum C Programming
    Replies: 27
    Last Post: 03-13-2008, 11:40 AM
  2. quick question
    By surfingbum18 in forum C Programming
    Replies: 5
    Last Post: 12-04-2007, 06:16 AM
  3. Design layer question
    By mdoland in forum C# Programming
    Replies: 0
    Last Post: 10-19-2007, 04:22 AM
  4. opengl DC question
    By SAMSAM in forum Game Programming
    Replies: 6
    Last Post: 02-26-2003, 09:22 PM
  5. Very simple question, problem in my Code.
    By Vber in forum C Programming
    Replies: 7
    Last Post: 11-16-2002, 03:57 PM