Thread: Using new while creating an array of a struct

  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をこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    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をこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    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をこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    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
    28,413
    In this case, an appropriate solution is to #include <vector> and use a std::vector<CandyBar>. A smart pointer is unnecessary here.
    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

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, 03:06 AM
  3. 2 problems: Struct array and pointer + struct
    By v1n1c1u5 in forum C Programming
    Replies: 0
    Last Post: 12-13-2009, 05: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