Pass data to a Dynamic String Array That uses A Structure And Function

Objective of the program is for the user to enter 1 of 3 options at the beginning in processing and updating information in real time that can be later displayed.

For now my first goal is to get the program to process data when the user selects the first option. I.E. display
Size of 1st pizza order: small
Topping of 1st pizza order: no topping

Size of 2nd pizza order: small
Topping of 2nd pizza order: no topping

The instructions state 2 different arrays will be declared. 1 in main, the other in the order function. The one in the order function will be dynamically allocated likely enclosed in a for loop.

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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
#include <iostream>
#include <string> 
using namespace std;



struct Pizza
{
	string structSize;
	string structTopping;
};

void Order(Pizza * specPtr[], int size);

int main()
{
	int select, size;  //size will refer to the title group user wants to view/modify
	
	string pizzaSize, pizzaTopping;
	
	
	int pizzaOrder = -1; //generate default values of: Small (size), No topping (topping)

	Pizza * pizzaPtr[2]; //2 different pizza orders

	pizzaPtr[0]->structSize = "Small";
	pizzaPtr[0]->structTopping = "No topping";

	pizzaPtr[1]->structSize = "Small";
	pizzaPtr[1]->structTopping = "No topping";
	
	//******************************'Order' function call, to generate default values of: Small (size), None (topping)
	Order(pizzaPtr, 0);	
	
	do {
		cout << "Press 1, to see current selection. \n2, to change current selection. \n3, to end program: " << endl;
		cin >> select;

		switch (select)
		{
		case 1:  

			pizzaPtr[0];
			cout << "Size of 1st pizza order: " << (*pizzaPtr)->structSize;   //******************call initialization function to provide up to date information
			cout << "Topping of 1st pizza order: " << (*pizzaPtr)->structTopping << endl;
			
			pizzaPtr[1];
			cout << "Size of 2nd pizza order: " << (*pizzaPtr)->structSize << endl;
			cout << "Topping of 1st pizza order: " << (*pizzaPtr)->structTopping << endl;
			break;
		
		case 2:
			cout << "Enter Pizza order #, you want to change: ";
			cin >> pizzaOrder;

			pizzaOrder -= 1;  //passed on to Order( , int size)

			cout << "\nEnter pizza size #" << pizzaOrder ;
			getline(cin, pizzaPtr[pizzaOrder]->structSize);
			cout << "\nEnter Pizza topping#: " << pizzaOrder;
			getline(cin, pizzaPtr[pizzaOrder]->structTopping);
				
			//********Should I or/ how do i pass these user data entries to Order function, so that 
			//when user enters 1 at the main menu, these values will appear for the book they wanted to edit
			//Instead of the values; Size of 1st pizza order: small. Topping of 1st pizza order: no topping.

			break;

		case 3:
			cout << "Thank you for using this program" << endl;
			break;
		}
				
	} while (select != 3);

	system("pause");
	return 0;
}

void Order(Pizza * specPtr[], int size)
{
	//Dynamic array allocation required here
	


	for (int i = 0; i == size; i++)
	{
		Pizza * setPtr;  //call struct Pizza
		setPtr = new Pizza[size];

		cout << "\nEnter pizza size #" << size + 1 ;
		getline(cin, setPtr[size].structSize);
		cout << "\nEnter Pizza topping#: " << size + 1;
		getline(cin, setPtr[size].structTopping);
	}

	//delete [] setPtr;
	//setPtr = 0;
}
Come on now, any one?
Last edited on
Exactly what the program is supposed to do?
On first glance, there are several errors (wrong loop conditions, memory leaks, no-ops (l. 43+47)).

Edit: if the number of pizzas is fixed at 2, dynamic allocation is the wrong approach.
Last edited on

I intend to modify this program later, to handle 4 different pizza orders. The instructions require the utilization of a dynamic array.

This is what I got so far. The program executes, but refuses to display the default values of
...: Small, and ...: No topping.

The program is supposed to allow users to update, the information (Strings) of the 2 different pizzas. And will automatically update them, when they press in the main menu.

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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
#include <iostream>
#include <string> 
using namespace std;



struct Pizza
{
	string structSize;
	string structTopping;
	//string pizzaRAY[2]; //2 different pizza orders
};

void Order(Pizza * specPtr[], int size);

int main()
{
	int select, size;  //size will refer to the title group user wants to view/modify
	
	string pizzaSize, pizzaTopping;
	
	int pizzaOrder; //generate default values of: Small (size), No topping (topping)

		
	//Pizza * pizzaPtr[2]; //2 different pizza orders
	Pizza * pizzaPtr[2];

	Pizza setup1, *setup2;


	Order(pizzaPtr, -2);

	do {
		cout << "\n1, to see current selection. \n2, to change current selection. \n3, to end program: " << endl;
		cin >> select;

		switch (select)
		{
		case 1:  
			
			pizzaPtr[0] = new Pizza;			
			cout << "Size of 1st pizza order: " << pizzaPtr[0]->structSize ;  
			cout << "\nTopping of 1st pizza order: " << pizzaPtr[0]-> structTopping << endl;
			
			pizzaPtr[1] = new Pizza;
			cout << "Size of 2nd pizza order: " << pizzaPtr[1]->structSize ;
			cout << "\nTopping of 2nd pizza order: " << pizzaPtr[1]->structTopping << endl;
			
			break;
		
		case 2:
			cout << "Enter Pizza order #, you want to change: ";
			cin >> pizzaOrder;

			//pizzaOrder -= 1;  //passed on to Order( , int size)
			
			Order(pizzaPtr, pizzaOrder);	

			break;

		case 3:
			cout << "Thank you for using this program" << endl;
			break;
		}
				
	} while (select != 3);

	system("pause");
	return 0;
}

void Order(Pizza * specPtr[], int size)
{
	//Dynamic array allocation required here


	for (int i = 0; i < size; i++)
	{
		
		Pizza * setPtr;  //call struct Pizza
		setPtr = new Pizza[size];
		
		if (size < 0)
		{
		specPtr[0]->structSize = "Small";
		specPtr[0]->structTopping = "No topping";

		specPtr[1]->structSize = "Small";
		specPtr[1]->structTopping = "No topping";
		}

		else
		{
		cout << "\nEnter size of of piza: " ;//<< specPtr[size]->structSize;
		cin.ignore();
		getline(cin, setPtr->structSize);
		cout << "\Enter topping of piza: " ;// << specPtr[size]->structTopping;
		cin.ignore();
		getline(cin, setPtr->structTopping);
		cout << endl;

		}
		
		//delete [] setPtr;
		//setPtr = 0;
	}


}

This is what i have so far.

Time's running out for me :x

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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
#include <iostream>
#include <string> 
using namespace std;



struct Pizza
{
	string structSize;
	string structTopping;
	//string pizzaRAY[2]; //2 different pizza orders
};

void Order(Pizza * specPtr[], int size);

int main()
{
	int select, size;  //size will refer to the title group user wants to view/modify
	
	string pizzaSize, pizzaTopping;
	
	int pizzaOrder; //generate default values of: Small (size), No topping (topping)

		
	//Pizza * pizzaPtr[2]; //2 different pizza orders
	Pizza * pizzaPtr[2];

	Pizza setup1, *setup2;


	Order(pizzaPtr, -2);

	do {
		cout << "\n1, to see current selection. \n2, to change current selection. \n3, to end program: " << endl;
		cin >> select;

		switch (select)
		{
		case 1:  
			
			pizzaPtr[0] = new Pizza;			
			cout << "Size of 1st pizza order: " << pizzaPtr[0]->structSize ;  
			cout << "\nTopping of 1st pizza order: " << pizzaPtr[0]-> structTopping << endl;
			
			pizzaPtr[1] = new Pizza;
			cout << "Size of 2nd pizza order: " << pizzaPtr[1]->structSize ;
			cout << "\nTopping of 2nd pizza order: " << pizzaPtr[1]->structTopping << endl;
			
			delete [] pizzaPtr;

			break;
		
		case 2:
			cout << "Enter Pizza order #, you want to change: ";
			cin >> pizzaOrder;

			//pizzaOrder -= 1;  //passed on to Order( , int size)
			
			Order(pizzaPtr, pizzaOrder);	

			break;

		case 3:
			cout << "Thank you for using this program" << endl;
			break;
		}
				
	} while (select != 3);


	system("pause");
	return 0;
}

void Order(Pizza * specPtr[], int size)
{
			
	for (int i = 0; i < size; i++)
	{
		
		Pizza * specPtr;  //call struct Pizza
		specPtr = new Pizza[size];
		//Pizza *ptr = &setPtr[0];
		/*if (size < 0)
		{
		
				
		}

		else
		{ */
		int min;
		min = size - 1;
		if (i == min)
		{
		cout << "\nEnter size of of piza: " ;//<< specPtr[size]->structSize;
		cin.ignore();
		getline(cin, (specPtr[i]).structSize);
		cout << "\Enter topping of piza: " ;// << specPtr[size]->structTopping;
		cin.ignore();
		getline(cin, (specPtr[i]).structTopping);
		cout << endl;
		}
		else 
		{
		specPtr[0].structSize = "Small";
		specPtr[0].structTopping = "No topping";

		specPtr[1].structSize = "Small";
		specPtr[1].structTopping = "No topping";	

		}

		
		delete [] specPtr;
	}
		
}

Topic archived. No new replies allowed.