I searched the internet front and back looking for the answer to this problem. "Do programming exercise 4, 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."
I have a minor in computer science but since it has been a while since I have done any programing I wanted to put myself to the test and reinforce what I know and see if I can learn more on my own. Anyway I ran into this problem and could not find a good example to emulate so after many attempts I figured it out and I want to share my code with anyone who may run into this problem in the future or so that I can find it if I need to in the future. I used MS visual studios so adjust to your compiler.
for the first question always answer 3 or modify the code
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 50 51 52 53 54 55 56 57
|
#include "stdafx.h" //you may have to remove this depending on you compiler
#include <iostream>
using namespace std;
struct Pizza_Analysis //defining the struct
{
char company_name[35];
double dia_of_pizza;
double weight_of_pizza;
};
int main()
{
int num_of_companies;
//using new to allocate the structure
Pizza_Analysis *ps = new Pizza_Analysis[3];
cout << "How many companies do you want to look at? ";
cin >> num_of_companies;
cin.get();
//the struct array at work
for(int i=0; i<num_of_companies; i++)
{
cout << "\n\nPlease enter company name. ";
cin.getline(ps<i>.company_name,35);
cout <<"\n\nPlease enter the diameter of the pizza. ";
cin >> ps<i>.dia_of_pizza;
cout <<"\n\nPlease enter the weight of the pizza. ";
cin >> ps<i>.weight_of_pizza;
cin.get();
}
cout << endl << endl;
//the struct array at work
// prints the input to screen
for(int i=0; i<3; i++)
{
cout << "Company Name : " << ps<i>.company_name << endl
<< "diameter of pizza : " << ps<i>.dia_of_pizza << endl
<< "Weight of pizza : " << ps<i>.weight_of_pizza << endl <<endl;
};
delete [] ps;
system("pause");//you may have to remove this depending on you compiler
return 0;
}
| |