Old fart here, still a horrible newbie.

Tonight, I've actually got a working solution for my problem, but the book I'm reading is unclear about something, and I'm having trouble getting Google to give me the the exact answer I want. Given,

//Header junk
//CandyBar struct
int main()
{
    CandyBar * pt = new CandyBar[3];
    //This init works
    pt[0].brand = "Three Musketeers";
    pt[0].weight = .33;
    pt[0].calories = 200;
    //This init does not work
    pt[1] = {"Hershey's", .45, 225};
    //input, output already work, hallelujah
    delete pt;

    return 0;
}

...how do I push bracketed input into a new struct array like I'm failing to do with pt[1]? Banging out multiple-row initializations is really clumsy. If, for some reason, the answer is, "don't do that," I'll accept that, too.

Recommended Answers

All 2 Replies

kind like this:

CandyBar cb = {"Hershey's", .45, 225};

Only works at initialization.

Line 11. is already too late

this works:

CandyBar cb[3] = { {"Three Musketeers", .33, 200}, 
                                        {"Hershey's", .45, 225}, 
                                        {"Mars", .45, 2250} };
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.