An array problem

This is a discussion on An array problem within the C++ Programming forums, part of the General Programming Boards category; Hey everyone, I am having some difficulties knowing how to assign city to the list. Can anyone help? Thanks! Code: ...

  1. #1
    Registered User XNOViiCE's Avatar
    Join Date
    Nov 2012
    Location
    'merica
    Posts
    38

    An array problem

    Hey everyone, I am having some difficulties knowing how to assign city to the list. Can anyone help? Thanks!

    Code:
    class DestinationList
    {
    private:   
    int num;                       // number of objects in the list
    Destination list[MAX_CITIES];  // array of destinations
    
    
    public:
    
    
    int Add(string city)
    {
       num = 0;
       list[num] = city;
       num++;
    }
    };

  2. #2
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,308
    You don't wan to set num to zero every time you call add. You want it to start off as zero.
    That's the job of a "constructor", hint hint.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  3. #3
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,689
    Yes, initialise num to 0 just once, in the constructor.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  4. #4
    Registered User XNOViiCE's Avatar
    Join Date
    Nov 2012
    Location
    'merica
    Posts
    38
    Well when I made the constructor and assigned num to zero it gives me this error "Cannot assign values of type string to a variable of type Destination"

  5. #5
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,689
    That seems about right.

    What's the relationship between a destination and a city?

    Perhaps your destinationlist add function should accept a destination, not a city.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  6. #6
    Registered User
    Join Date
    May 2010
    Posts
    2,837
    Post the code, and the exact error messages!

    Jim

  7. #7
    Registered User XNOViiCE's Avatar
    Join Date
    Nov 2012
    Location
    'merica
    Posts
    38
    That is the exact error message XD I have to use some limited compiler for some reason. I have the code posted at the top already too, the only difference is that a put in a constructor.
    Last edited by XNOViiCE; 04-21-2013 at 02:30 PM.

  8. #8
    Registered User XNOViiCE's Avatar
    Join Date
    Nov 2012
    Location
    'merica
    Posts
    38
    Well the city is the destination of where the shipment has to be dropped off. Would accepting a destination not a city still be a string anyways?

  9. #9
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,689
    Hey, it's your definition of Destination, not mine you have to worry about.

    What's the difference between
    Destination list[MAX_CITIES]; // array of destinations
    and
    String list[MAX_CITIES]; // array of destinations

    Presumably, you created a class called Destination, with an obvious member called say city, which is a string.
    But what else?
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  10. #10
    Registered User XNOViiCE's Avatar
    Join Date
    Nov 2012
    Location
    'merica
    Posts
    38
    string city
    int package_count
    float total_weight

    are the members that are present in Destination right now.

  11. #11
    ZuK
    ZuK is offline
    Registered User
    Join Date
    Aug 2005
    Location
    Austria
    Posts
    1,990
    you could try
    Code:
    list[num].city = city;
    Kurt

  12. #12
    Registered User XNOViiCE's Avatar
    Join Date
    Nov 2012
    Location
    'merica
    Posts
    38
    Hey that works! Thanks! Don't really know why I didn't try that yet! XD

  13. #13
    ZuK
    ZuK is offline
    Registered User
    Join Date
    Aug 2005
    Location
    Austria
    Posts
    1,990
    I'm suprised. Usually classes don't have too many public data members.
    Next time it would be a good idea to show the declaration of the class in question if you really want help.
    Kurt

  14. #14
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Posts
    22,829
    This spells trouble. First off, you should probably be using std::vector instead of an array here. Use std::vector:ush_back.
    And, as you have clearly demonstrated, a city is not a destination. You need to add some way to create a destination from a city. Probably using a constructor in the Destination class.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  15. #15
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,689
    Quote Originally Posted by XNOViiCE View Post
    string city
    int package_count
    float total_weight

    are the members that are present in Destination right now.
    That seems more like a 'Consignment' to me.

    One thing about OO design and programming is giving your objects accurate and meaningful names.

    No one here would have even guessed that it had anything to do with packages until you said so.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 2
    Last Post: 01-06-2013, 06:49 AM
  2. Array problem. Can you find the problem with this?
    By Aitra in forum C Programming
    Replies: 21
    Last Post: 01-03-2013, 09:33 AM
  3. problem initializing a double array for large array
    By gkkmath in forum C Programming
    Replies: 4
    Last Post: 08-25-2010, 08:26 PM
  4. Problem converting from char array to int array.
    By TheUmer in forum C Programming
    Replies: 11
    Last Post: 03-26-2010, 11:48 AM
  5. simple array of char array problem
    By cloudy in forum C++ Programming
    Replies: 5
    Last Post: 09-10-2006, 12:04 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21