Saturday, 2 April 2016

The CandyBar structure contains three members.The first member holds the brand name of a candy bar.The second member holds the weight (which may have a fractional part) of the candy bar, and the third member holds the number of calories (an integer value) in the candy bar.Write a program that declares such a structure and creates a CandyBar variable called snack, initializing its members to "Mocha Munch", 2.3, and 350, respectively. The initialization should be part of the declaration for snack. Finally, the program should display the contents of the snack variable.

Leave a Comment


Questions:


5. The CandyBar structure contains three members.The first member holds the brand name of a candy bar.The second member holds the weight (which may have a fractional part) of the candy bar,
and the third member holds the number of calories (an integer value) in the candy bar.Write a program that declares such a structure and creates a CandyBar variable called snack, initializing its members to "Mocha Munch", 2.3, and 350, respectively. The initialization should be part of the declaration for snack. Finally, the program should display the contents of the snack variable.

Explanation:

Below mention code is compiled in Visual Studio 2015 and Code Blocks 13.12,output snap is attached.. If any problem you feel and you want some explanation feel free to contact us.

Code:

/**************************************************|
/*************C++ Programs And Projects************|
***************************************************/


#include <iostream>
#include <string>
using namespace std;

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

int main() {
       CandyBar snack = { "Mocha Munch",
              2.3,
              350 };

       cout << "Brand: " << snack.brand << endl
              << "Weight: " << snack.weight << endl
              << "Number: " << snack.number << endl;
       return 0;
}



Output:



Related Articles:

C++ Primer Plus Sixth Edition Complete Solution Manual 

C++ Books Solution

If You Enjoyed This, Take 5 Seconds To Share It