Array List

I was making a array list in an assignment, so at the 'typedef int ITEMTYPE' part, I am supposed to change the 'int' with 'string, but it produces the following errors:
Compiling...
listArray.cpp
c:\users\eric\desktop\assignment 1\listarray.h(5) : error C2146: syntax error : missing ';' before identifier 'ITEMTYPE'
c:\users\eric\desktop\assignment 1\listarray.h(5) : fatal error C1004: unexpected end of file found
TestlistArray.cpp
c:\users\eric\desktop\assignment 1\listarray.h(5) : error C2146: syntax error : missing ';' before identifier 'ITEMTYPE'
c:\users\eric\desktop\assignment 1\listarray.h(5) : fatal error C1004: unexpected end of file found

so, I dont know what is the problem when I replace the 'int' with 'string'

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
#ifndef LISTARRAYH
#define LISTARRAYH
#include <string>
const int MAXLEN = 100;
typedef int ITEMTYPE; /*the typedef thing
class List{
	public:
		List();
		~List();

		ITEMTYPE retrieve (int pos) const;
		void insert (const ITEMTYPE input, int pos);
		void append (ITEMTYPE input);
		bool remove (int pos);
		int length () const;
		bool isEmpty ();
		void makeEmpty();
		void printlist();
		bool search(ITEMTYPE input);
		int itempos(ITEMTYPE input);
	private:
		ITEMTYPE item[MAXLEN];
		int len;
};
#endif */


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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
#include <iostream>
#include "listArray.h"
#include <string>
using namespace std;
List::List(){
	cout <<"List constructor called - Creating empty list" << endl;
	len=0;
}
List::~List(){
	cout<<"List destructor called"<<endl;
}

ITEMTYPE List::retrieve (int pos) const{
	int index=pos-1;
	int max=length();

	ITEMTYPE temp;

	if ((index>=0)&&(pos<=max)){
		temp = item[index];
	}
	return temp;
}	

void List::insert (const ITEMTYPE input, int pos){
	int len=length();
	int index=pos-1;
	int indexOfLast=len-1;

	if ((index>=0)&&(index<len)){

		for(indexOfLast;indexOfLast>=index;indexOfLast--){
			//cout<<"fg\n";
			item[indexOfLast+1]=item[indexOfLast];
		}
		item[index]=input;
	}
	else
		cout<<"\nPosition does not exist\n";
	
}
void List::append (ITEMTYPE input){
	int last=length();
	int appendpos=last;

	item[appendpos]=input;
}

bool List::remove (int pos){
	
	bool removed=false;
	int len=length();
	int index = pos-1;
	int indexOfLast=len-1;
	
	int counter=index;


	if ((index>= 0)&&(index < indexOfLast)){
		cout<<"last: "<<indexOfLast<<endl;

		do{
			//item[index]=item[index+1];
			item[counter]=item[counter+1];
			cout<<item[index];
			//cout<<counter;
			counter++;

		}while(counter<=indexOfLast);

		item[indexOfLast]=0;
		removed=true;
		
	}
	
return removed;
}

int List::length () const{
	int sum=0;
	for(int j=0; j<=MAXLEN; j++){
		if(item[j]!=0)
			sum++;
	}
	return sum;
}

bool List::isEmpty (){
	bool empty=false;
	int len=length();

	for(int j=0; j<=MAXLEN; j++){
		if(len==0)
			empty = true;
	}
	return empty;
}

void List::makeEmpty(){
	for(int j=0; j<=MAXLEN; j++){
		item[j]=0;
	}
}

void List::printlist(){
	bool empty = false;
	cout<<"The Available Items in the List are: \n";

	for (int j=0; j<=MAXLEN;j++){
		if (item[j]!=0){
			cout<<"Position: "<<j+1<<". "<<item[j]<<endl;
		}
	}

	if (empty==true){
		system("cls");
		cout<<"\n\n\t\tThe List is empty\n\n\n";
	}

}

bool List::search(ITEMTYPE input){
	bool found=false;
	int max=length();

	for(int j=0; j<=max; j++){
		ITEMTYPE element=retrieve(j);
		if(element== input)
			found = true;
	}
	return found;
}

int List::itempos(ITEMTYPE input){
	
	int max=length();
	int pos;

	for(int j=0; j<=max; j++){
		ITEMTYPE element=retrieve(j);
		if(element== input){
			pos=j;
		}
	}
	return pos;
}

int main()
{
	List ListArray;
	int Input;
	int pos;
	ITEMTYPE output;
	bool removed=false,found=false,loop;
	char choice;

	ListArray.makeEmpty();//clean the Array for usage


	int menu;
	do{
		loop=false;

		do
		{
			//system("cls"); // or system("cls") in windows
			cout << "\t Lab1 : Array Implementation of LIST ADT" << endl;
			cout << "\t\t\t Menu Choices" << endl << endl;
			cout << "\t\t1 - Insert Item " << endl;
			cout << "\t\t2 - Append Item" << endl;
			cout << "\t\t3 - Retrive Item" << endl;
			cout << "\t\t4 - Delete an Item" << endl;
			cout << "\t\t5 - Search" << endl;
			cout << "\t\t6 - Print List Items" << endl;
			cout << "\t\t7 - Clear List" << endl;
			cout << "\t\t8 - Quit" << endl << endl;

			cout << "\t\t8 - length" << endl << endl;
			cout << "\tEnter your choice: ";
			cin >>menu;

		} while ((menu <=1) && (menu >= 10));
		switch(menu)
		{
		case 1:
			system("cls");
			cout<<"Enter the value to be inserted"<<endl;
			cin>>Input;
			cout<<"Enter the position to be inserted"<<endl;
			cin>>pos;
			
			ListArray.insert(Input,pos);
			
			loop=true;
			break;

		case 2:
			system("cls");
			cout<<"Enter the value to be Appended"<<endl;
			cin>>Input;
			ListArray.append(Input);
			loop=true;
			break;
		case 3:
			system("cls");
			cout<<"Enter the position of the value to be Retrieved"<<endl;
			cin>>pos;
			output = ListArray.retrieve(pos);
			cout<<"\nThe value at position: "<<pos<<" is "<< output;
			loop=true;
			break;
		case 4:
			system("cls");
			cout<<"Enter the position of the item to be deleted"<<endl;
			cin>>pos;
			removed =ListArray.remove(pos);
			if (removed=true)
				cout<<"\nThe Item has been Successfully Deleted\n";
			else
				cerr<<"\nError deleting the Item\n";
			loop=true;
			break;

		case 5:
			system("cls");
			cout<<"Enter the value to be searched"<<endl;
			cin>>Input;
			found=ListArray.search(Input);
			pos=ListArray.itempos(Input);
			cout<<"\nThe Item: "<<Input<<" That you Queried, exists, at Position: "<<pos <<endl<<endl;
			loop=true;
			break;
		case 6:
			system("cls");
			ListArray.printlist();
			loop=true;
			break;
		case 7:
			system("cls");
			cout<<"\nAre you Sure you want to empty the List?(y/n)\n";
			cin>>choice;
			if (choice=='y')
				ListArray.makeEmpty();
			loop=true;
			break;
		case 8:
			loop=false;
			break;
		case 9:
			loop=true;
			cout<<ListArray.length ();
			break;
		default:
			cerr<<"Invalid Entry";
			loop=true;
			break;
		}
	}while (loop==true);
	
	

return 0;
}
try typedef std::string ITEMTYPE ?

Alternatively, you can make your class to be template:

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
#ifndef LISTARRAYH
#define LISTARRAYH
#include <string>
const int MAXLEN = 100;

template<class T>
class List{
	public:
		List();
		~List();

		T retrieve (int pos) const;
		void insert (const T input, int pos);
		void append (T input);
		bool remove (int pos);
		int length () const;
		bool isEmpty ();
		void makeEmpty();
		void printlist();
		bool search(ITEMTYPE input);
		int itempos(ITEMTYPE input);
	private:
		T item[MAXLEN];
		int len;
};
#endif */
Remember that "string" is in the std namespace

(Edit: Too slow in typing)
Last edited on
Topic archived. No new replies allowed.