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.