Need more simple way to do this

closed account (10oTURfi)
Excercise from CH4 Primer Plus is killing me.
I need easier way to init all those variables in candybar struct
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
#include <iostream>
#include <string>
using namespace std;

struct CandyBar
{
	string brand;
	double weight;
	int calories;
};

int main(){
	CandyBar *snack = new CandyBar[3];

	snack->brand = "Mocha Munch"; //sample candy is lame! I haz sum better candy down below
	snack->weight = 2.3;
	snack->calories = 350;

	snack[1].brand = "Demons Delight";
	snack[1].weight = 3.3;
	snack[1].calories = 666;

	snack[2].brand = "Troll Jelly";
	snack[2].weight = 6.9;
	snack[2].calories = 9001;

	delete [] snack;

}
Last edited on
In C++11, it is done as follows:
1
2
3
vector<CandyBar> snacks({{"Mocha Munch",2.3,350},
                         {"Demons Delight",3.3,666},
                         {"Troll Jelly",6.9,9001}});

It might require enabling a compiler switch (with GCC it is -std=c++0x).

You shouldn't use manual memory allocation (new[] and delete[]), by the way.
I hope your book told you this.
closed account (zb0S216C)
Since the values are different for each snack, there's no easier way to do it. If you want to initialize each member to the same value, you would use a constructor. For example:

1
2
3
4
5
struct CandyBar
{
    CandyBar( ) : brand( "Brand name" ), weight( 0.1 ), calories( 0 )
    { }
};


Wazzak
closed account (10oTURfi)
In C++11, it is done as follows:
1
2
3
vector<CandyBar> snacks({{"Mocha Munch",2.3,350},
{"Demons Delight",3.3,666},
{"Troll Jelly",6.9,9001}});

It might require enabling a compiler switch (with GCC it is -std=c++0x).

You shouldn't use manual memory allocation (new[] and delete[]), by the way.
I hope your book told you this.


Ok now I am getting confused.
First I was learning from Herbert Schild books, then I found out that his books are ranked #1 in "books you dont want to read". Then I was learning from thenewboston.com and it somehow felt wrong, beacuse he didnt explain alot of stuff etc.
Now my third source for learning seem to be bad too.

Which book should I read??
I tend to recommend the C++ Primer (without the Plus).
I haven't heard anything bad about the C++ Primer Plus, though (but not anything particularly good either).
You shouldn't use manual memory allocation (new[] and delete[]) if there is a reasonable alternative available, by the way

fixed.

There shouldn't be any books out there that include C++11 yet, as there is no complete implementation of C++11 yet (at least none that I am aware of).


And I think that syntax is already allowed for POD types in C++03 (and earlier too, it's an import from C). C++11 doesn't require the ()'s around the expression btw. (and neither does C++03)

Another option would be just writing a constructor. Doesn't save you the work of providing the values, but does save you the work of assinging each of them manually.
if there is a reasonable alternative available

So basically, always (just as there are always exceptions - however, when you are in such an exceptional situation, you'll be aware of it anyway - it doesn't taint the rule).

And I think that syntax is already allowed for POD types in C++03 (and earlier too, it's an import from C).

Yes, but only for C arrays.
Last edited on
closed account (10oTURfi)
Well.. Back on topic; MS VC++ wont compile that piece of code. I get a syntax error.
This compiles fine for me without specifying std=c++0x with gcc 4.5.2:

1
2
3
4
5
6
7
8
9
10
11
12
13
#include <string>
struct Something
{
	float a,b;
	std::string c;
};

int main()
{
Something stuff[2] {{1.0,2.0, "abc"},{2.0,1.0, "defg"}};
return 0;
}
Yeah, since it's an array.
One can also initialize a vector from a static array, but whether that's a good idea depends on the situation.
He used an array in his program. Of course C++03 doesn't have initializer lists yet, but the OP didn't seem to want to use a vector to begin with.

@Krofna: That won't compile cause only a small subset of C++11 is implemented in Visual C++, and this is not part of it.
Last edited on
Considering he used dynamic allocation, I'm not so sure about that.
Topic archived. No new replies allowed.