Using new while creating an array of a struct

This is a discussion on Using new while creating an array of a struct within the C++ Programming forums, part of the General Programming Boards category; Hey I am working on an example questions from a book and the question is asking to use an example ...

  1. #1
    Registered User
    Join Date
    Dec 2011
    Posts
    34

    Using new while creating an array of a struct

    Hey I am working on an example questions from a book and the question is asking to use an example but they want me to make it so you have an array of 3 structures using the new command. I have tried and do not believe I am able to figure out how to create a array of structures using the new command. Here is my code if you want to look at it.
    Code:
    #include <iostream>
    #include <string>
    
    
    struct CandyBar
    {
        std::string name;
        float weight;
        int calories;
    };
    
    
    int main()
    {
        using namespace std;
    
    
    
        CandyBar snack[3] = new CandyBar;
        cout<< "What is the snack's name?\n";
        getline(cin, snack[0].name);
        cout<< "What is the snack's weight?\n";
        cin>> snack[0].weight;
        cout<< "How many caolories does it haev?\n";
        cin>> snack[0].calories;
    
    
        cout<< "The name of the canyd bar is " << snack[0].name
            << " it weights " << snack[0].weight << " much\n and it has " << snack[0].calories << " calories.";
        
        delete [] snack;
        return 0;
    }
    I know I do not have anything accessing the other arrays yet but I just wanted to figure out how to create an array before I wrote out the data it needs to hold.

  2. #2
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,538
    new always return a pointer, so the type of your variable must be a pointer:
    CandyBar* snack

    Furthermore, if you want an array, you specify the number of items directly after the type in new:
    new CandyBar[3]

    So, putting it together, it becomes
    CandyBar* snack = new CandyBar[3];

    If you have a modern compiler, you can even simply that to:
    auto snack = new CandyBar[3];

    and let the compiler figure out the type for snack for you, so you don't have to type it.

    Btw, before delving into pointers with new and delete, it may be a good idea to learn about smart pointer.
    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.

  3. #3
    Registered User
    Join Date
    Dec 2011
    Posts
    34
    Oh forgot to put the * in before that and did not know you had to put the number of arrays you want on the side with the new. I have two more questions though about two things you brought up. Which compiler or IDE would you recommend to use that has all or some of the new features? I currently think I am using Code:Block 10.05 and its is not able to do the new C++11 stuff like auto or the new array type. Also is there some where were I can look for smart pointers? Saying I defiantly want to understand them better saying I have not understand really much about when to and not to use pointers.

  4. #4
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,538
    Code::Blocks supports C++11. Just make sure to grab latest gcc and enable support in compiler options (it is called C++0x there).

  5. #5
    Registered User
    Join Date
    Dec 2011
    Posts
    34
    Oh thanks for that I have been trying to look for it but I do not know my away around Code::Blocks or any compiler or IDE to know what any of the stuff means really. Also how do you use smart pointers? I have looked in the book I have though I wasnt able to find reference to them in the pointer section or the index. Do you have any suggestions were I should look to learn about how to use them?

  6. #6
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,538
    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.

  7. #7
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,598
    In this case, an appropriate solution is to #include <vector> and use a std::vector<CandyBar>. A smart pointer is unnecessary here.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Creating struct from existing data
    By Dest in forum C Programming
    Replies: 5
    Last Post: 04-13-2010, 06:58 PM
  2. Replies: 8
    Last Post: 01-22-2010, 02:06 AM
  3. 2 problems: Struct array and pointer + struct
    By v1n1c1u5 in forum C Programming
    Replies: 0
    Last Post: 12-13-2009, 04:38 PM
  4. creating a struct based on user input
    By rodrigorules in forum C Programming
    Replies: 1
    Last Post: 09-15-2005, 06:16 PM

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