forked from noahc66260/C-PrimerPlus
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpe4-9.cpp
51 lines (38 loc) · 1.42 KB
/
pe4-9.cpp
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
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
// pe4-9.cpp -- we create a dynamic array of a structure whose parameters are the brand, weight, and calories of a candy bar; initialize it; and display its contents
// This is exercise 9 of chapter 4 in C++ Primer Plus by Stephen Prata
#include<iostream>
#include<cstring>
struct CandyBar
{
char brand[20];
float weight;
int calories;
};
int main(void)
{
using namespace std;
CandyBar * snacks = new CandyBar [3];
strcpy(snacks->brand, "Twix");
snacks->weight = 1.1;
snacks->calories = 200;
strcpy((snacks+1)->brand, "Snickers");
(snacks+1)->weight = 2.5;
(snacks+1)->calories = 300;
strcpy((snacks+2)->brand, "Three Musketeers");
(snacks+2)->weight = 2.1;
(snacks+2)->calories = 150;
cout << "For our first snack: " << endl;
cout << "Our snack brand is " << snacks[0].brand << endl;
cout << "The weight is " << snacks[0].weight << endl;
cout << "And it contains " << snacks[0].calories << " calories" << endl << endl;
cout << "For our second snack: " << endl;
cout << "Our snack brand is " << snacks[1].brand << endl;
cout << "The weight is " << snacks[1].weight << endl;
cout << "And it contains " << snacks[1].calories << " calories" << endl << endl;
cout << "For our third snack: " << endl;
cout << "Our snack brand is " << snacks[2].brand << endl;
cout << "The weight is " << snacks[2].weight << endl;
cout << "And it contains " << snacks[2].calories << " calories" << endl;
delete [] snacks;
return 0;
}