Templates user inputs dynamic array

This code is from my text book it shows how to implement code that is embedded I have modified it somewhat but I was wondering how I could get the user to implement the size of the array and enter the integers with the size of array that was implemented. Thanks

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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
#include<iostream>
#include<cstdlib>
#include<iomanip>
using namespace std;

template<class T>
class SimpleVector
{
private:
	T *aptr;//a pointer to a specific template data type
	int arraySize;//array size
	void subError();//subscript out of range
public:
	SimpleVector(int);
	SimpleVector(const  SimpleVector &);//copy constructor
	~SimpleVector();
	int size()
	{
		return arraySize;
	}
	T &operator[](int);//overloaded [] operator
	void print();//outputs the array elements
	void getElementAt();
};
//*********************************************
//constructor for simple vector class. set the size
//of the array and allocates memory for it
//*********************************************
template<class T>
SimpleVector<T>::SimpleVector(int s)
{
	arraySize = s;
	aptr = new T [s];
	for(int count = 0; count < arraySize; count++)
		aptr[count] = T();
}
//***********************************************
//copy constructor for SimpleVector class
//***********************************************
template<class T>
SimpleVector<T>::SimpleVector(const SimpleVector &obj)
{
	arraySize = obj.arraySize;
	aptr = new T[arraySize];
	for(int count = 0; count < arraySize; count++)
	aptr[count] = obj[count];
}
//***********************************************
//Destructor for SimpleVector class
//***********************************************
template<class T>
SimpleVector<T>::~SimpleVector()
{
	if(arraySize > 0)
		delete []aptr;
}
//***********************************************
//sub error function. Display an error messages and 
//terminates the program when subscript is out of
//range
//***********************************************
template<class T>
void SimpleVector<T>::subError()
{
	cout<< " Error: Subscript out of range.\n";
	exit(0);
}
//************************************************
//overloaded [] operator. The argumentis the subscript
//This function returns a reference to the element
//in the array indexed by the subscript
//************************************************
template<class T>
T &SimpleVector<T>::operator[](int sub)
{
	if (sub < 0 || sub >= arraySize)
	subError();
	return aptr[sub];
}
//**************************************************
//prints all the entries in the array
//**************************************************
template<class T>
void SimpleVector<T>::print()
{
	for(int k = 0; k < arraySize; k++)
	cout<<aptr[k] << " ";
	cout<<endl;
}

int main()
{

const int SIZE = 0;

char keepGoing;//yes to continue no to exit
int choice;//menu choice
do{



cout<< " 				Simple Vector \n"<<endl;
cout<< " Enter number 1 for integer, 2 for double, or 3 for strings"<<endl;
cout<< "1.  integer\n";
cout<< "2.  double\n";
cout<< "3.  string\n";
cout<< " Enter your choice: ";
cin>> choice;

//set the numbic output formatting
cout<< fixed << showpoint << setprecision(2);
//set of actions
if(choice == 1)
{
	//size of dynamic array
   cout<< "\n How many data inputs do you have: ";
   //data for size of array

   
/*******************************************
this is the data you enter in dynamic array
********************************************/
cout<< " Enter the data:\n"<<endl;

}
else if(choice == 2)
{
	//size of dynamic array
   cout<< "\n How many data inputs do you have: "<<endl;
   //data for size of array
   //
/*******************************************
this is the data you enter in dynamic array
********************************************/

}
else if(choice == 3)
{
	//size of dynamic array
   cout<< "\n How many data inputs do you have: "<<endl;
   
cout<< " Enter an index to retrieve the data at that index\n"<<endl;
}

	


cout<<" Play again?";
cin>>keepGoing;
}while(toupper(keepGoing) =='Y');
return 0;
}
Last edited on
I think you need to create a method called something like [set_size].

This method will have to cater for condition when [aptr] is pointing to some valid array and consequently needs to be transferred to the new array that [aptr] will point to.

This method will also have to cater for the case when you make the size smaller than its previous value - this will require some elements in array to be truncated.

Topic archived. No new replies allowed.