Adding elements to array?

This is a discussion on Adding elements to array? within the C++ Programming forums, part of the General Programming Boards category; If my main has the following code Code: int main(void){ Resort R[100] = { {1267.45, 3, "Barcelo Marina Palace", "Varadero", ...

  1. #1
    Registered User
    Join Date
    Nov 2004

    Adding elements to array?

    If my main has the following code

    int main(void){
      Resort R[100] = {
                       {1267.45, 3, "Barcelo Marina Palace", "Varadero", "Cuba", "(416) 234-2345"},
                       {2159.99, 21, "Iberostar", "Varadero", "Cuba", "(416) 234-2346"},
                       {2400.26, 415, "Golden Crown Paradise Spa", "Cancun", "Mexico", "(416) 234-2444"},
                       {2899.54, 64, "Secrets Excellence All Suites Resort", "Punta Cana", "Domincan Republic", "(416) 234-2347"},
                       {1849.64, 51, "Melia Cayo Coco", "Cayo Coco", "Cuba", "(416) 234-2347"},
                       {1889.84, 15, "Aventura Spa Palace", "Mayan Riviera", "Mexico", "(416) 254-5547"},
                       {3849.04, 45, "El Dorado Seaside Suites", "Mayan Riviera", "Mexico", "(416) 254-2527"}
        rb(R, 7);;
    Later on in my program how can I do the following

    R[7] = {0,0,"","","",""};


    How might I be able to remove records in that array too?

  2. #2
    semi-colon generator ChaosEngine's Avatar
    Join Date
    Sep 2005
    Chch, NZ
    if you want to add and remove elements you should use a std::list
    "I saw a sign that said 'Drink Canada Dry', so I started"
    -- Brendan Behan

    Free Compiler: Visual C++ 2005 Express
    If you program in C++, you need Boost. You should also know how to use the Standard Library (STL). Want to make games? After reading this, I don't like WxWidgets anymore. Want to add some scripting to your App?

  3. #3
    ZuK is offline
    Registered User
    Join Date
    Aug 2005
    Quote Originally Posted by INFERNO2K
    Later on in my program how can I do the following

    R[7] = {0,0,"","","",""};
    You can't. This syntax is only valid for initialisation. This is an assignement.
    What you could do is to initialize a temporary and copy.
    Resort tmp = { 0,0,"","","","" };
    R[7] = tmp;

  4. #4
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    int main(void) {
        /* ... */
    You don't need that semicolon, and you should have a return 0.

    You can dynamically allocate an array, or use a list, as suggested.

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell

    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ:

    My website:
    Projects: codeform, xuni, atlantis, nort, etc.

  5. #5
    Rabble Rouser Slacker's Avatar
    Join Date
    Dec 2005
    >how can I do the following
    >R[7] = {0,0,"","","",""};
    Set up a constructor for your Resort class that takes those arguments and then you can do this:
    R[7] = Resort ( 0, 0, "", "", "", "" );
    And if you're lazy like me, you'll use those values as the default arguments so that you can then do this:
    R[7] = Resort();
    >and you should have a return 0
    You can, but it's not a requirement. C++ returns 0 from main implicitly if you fall off the end. Of course, that's only on modern compilers. If you're using a compiler that was shipped before the standard, it's hit or miss whether not returning 0 is undefined behavior or not. That's why it's a good idea to keep up to date. At least then you have some solid rules to fall back on and complain about to the vendor when it still doesn't work.


Popular pages Recent additions subscribe to a feed

Similar Threads

  1. adding elements in array
    By reb221 in forum C Programming
    Replies: 6
    Last Post: 12-30-2008, 02:41 PM
  2. Setting all elements of an array to zero
    By kolistivra in forum C Programming
    Replies: 9
    Last Post: 08-29-2006, 03:42 PM
  3. Type and nontype parameters w/overloading
    By Mr_LJ in forum C++ Programming
    Replies: 3
    Last Post: 01-02-2004, 01:01 AM
  4. Merge sort please
    By vasanth in forum C Programming
    Replies: 2
    Last Post: 11-09-2003, 12:09 PM
  5. Help with an Array
    By omalleys in forum C Programming
    Replies: 1
    Last Post: 07-01-2002, 09:31 AM

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