Showing posts with label CppStruct. Show all posts
Showing posts with label CppStruct. Show all posts

Saturday, April 2, 2016

Compound Types (C++ Primer Plus Sixth Edition Solution Manual Question # 9)

Leave a Comment
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************|
***************************************************/


/*
9. Do Programming Exercise 6, but, instead of declaring an array of three CandyBar structures,
use new to allocate the array dynamically.
*/

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

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


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

       pc[0].brand = "A";
       pc[0].weight = 1.2;
       pc[0].number = 100;

       pc[1].brand = "B";
       pc[1].weight = 2.3;
       pc[1].number = 200;

       pc[2].brand = "C";
       pc[2].weight = 3.4;
       pc[2].number = 300;

       cout << pc[0].brand << "\t" << pc[0].weight << "\t" << pc[0].number << endl
              << pc[1].brand << "\t" << pc[1].weight << "\t" << pc[1].number << endl
              << pc[2].brand << "\t" << pc[2].weight << "\t" << pc[2].number << endl;

       return 0;
}




Output:


Related Articles:

C++ Primer Plus Sixth Edition Complete Solution Manual 

C++ Books Solution

Read More

Compound Types (C++ Primer Plus Sixth Edition Solution Manual Question # 8)

Leave a Comment
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************|
***************************************************/


/*
Do Programming Exercise 7, but use new to allocate a structure instead of declaring a
structure variable. Also, have the program request the pizza diameter before it requests
the pizza company name.
*/

#include <iostream>
#include <string>

using namespace std;

struct sPizza {
       string name;
       double diameter;
       double weight;
};

int main() {
       sPizza * pp = new sPizza;
       cout << "Enter the pizza company name: ";
       getline(cin, pp->name);
       cout << "Enter the diameter of pizza: ";
       cin >> pp->diameter;
       cout << "Enter the weight of pizza: ";
       cin >> pp->weight;

       cout << "Pizza Company: " << pp->name << endl
              << "Pizza Diameter: " << pp->diameter << endl
              << "Pizza Weight: " << pp->weight << endl;
       delete pp;
       return 0;
}



Output:


Related Articles:

C++ Primer Plus Sixth Edition Complete Solution Manual 

C++ Books Solution

Read More