use new to allocate a structure array

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;
}


Why is this in the article section? It doesn't look that good...

You could make better use of std::strings instead of char[] for the string data; also you ask how many companies you want despite having allocated space for only 3. This means your first loop will write beyond your area for numbers greater than 3. You cin>> statements also completely die if you input any non-numerical data for the diameter or weight.

stdafx.h is not necessary even for VS compilers; you only need that if you enable precompiled headers, and even then I believe that stdafx is just a default name that VS happens to use for the precompiled header.

system("pause") has nothing to do with your compiler either; it's all about the OS, and that command works only for Windows. There are other more portable options available that would allow a different user to get the same functionality instead of having to delete the line.

tldr: Could be improved quite a bit, and TBH I don't think we need an article for this topic. Perhaps it would have been better elsewhere?
Topic archived. No new replies allowed.