hi;

i am doing a homework(due tomorrow) and i have a problem with it;

when I compile the program it shows no errors, and it takes input ,but does not show the output using function print();

note :the function of the program is to take elements from user,stores them in a dynamic array and then modify elements in the array using functions such remove ,replace and so on..


can someone tell me where is the problem and how to fix it.

i appreciate the help

here is the code:

#include<iostream>
using namespace std;


template <class type>
class arrayListType{

protected:

type *list;
int size;



public:


//arrayListType(type []);//constructor with one parameter
void remove(const type elem);
//void Setelem(type);
void removeall(const type);
void replaceall(const type &,const type &);
void print()const;
void setList(type arr[]);
};

template <class type>
void arrayListType<type>::setList(type  arr[]){

list=new type[size];


for(int i=0;i<size;i++)

list[i]=arr[i];



}



template <class type>
void arrayListType<type>::replaceall(const type & item,const type & replaceitem){
for (int r=0;r<size;r++)

{if (list [r]==item)

list[r]=replaceitem;

}

}


template <class type>
void arrayListType<type>::removeall(const type elem)
{
	
	for(int i=0;i<size;i++){

remove(elem);
	}
}



template <class type>
void arrayListType<type>::print() const //print the array
{
	for(int i=0;i<size;i++)
		cout<<list[i]<<"	";


}




template <class type>
void arrayListType<type>::remove(const type elem){//function beg

for(int i=0;i<size;i++)

{//for loop beg




	if (list[i]==elem){// if beg

		for(int y=i;y<size;y++)
		{
			
			list[y]=list[y+1];


		}
size--;
break;

}//end if 



}//end outer for loop

}//end of function

int main(){
int e,size;
int x,replace;

cout<<"Enter size of the array:";
cin>>size;
int *arr=new int [size];




for(int i=0;i<size;i++)
{
cout<<"Enter nummber:";
cin>>arr[i];
}


arrayListType<int> ob;
arrayListType<int> ob1;
arrayListType<int>ob2;
	cout<<"Enter element to delete:";
	cin>>e;
ob.setList(arr);

cout<<"\nobject deleting first occurance:\n";


ob.remove(e);
ob.print();
cout<<endl<<endl;

cout<<"object one deleting all occurance:\n\n";
ob1.removeall(e);
ob1.print();
cout<<"\n\n***************************************";
	cout<<"\n\n\nEnter number you want to replace:";
	cin>>x;
cout<<"Enter number you want to replace with:";
cin>>replace;
cout<<"\n";

cout<<"object two Replacing "<<x<<" with "<<replace<<"\n\n\n";
ob2.replaceall(x,replace);
ob2.print();

cout<<"\n";
	
	
	return 0;}

The size member of class arrayListType is not getting any value and because of that list is not getting allocated.

Well first off, ALWAYS write a constructor, a copy constructor, an assignment operator and a destructor unless you are 100% absolutely certain you don't have to. If you have to think about it for even a second, write them all.

Therefore, what happens, well size is not initialized. [nor is list]. But you then allocate memory completely randomly. Also when do you get rid of it?

You MUST write a default constructor and a destructor and most of your problems will go away.

In addition: you are using namespace std; and then using a variable called "list". This is 99.99% certain to trip you up soon. Since you have a list object in std. Please just don't use that "using" construct, sure use a smaller form, e.g. using std::cout; etc , or just use std::cout.

You have errors in remove(), you copy list[y+1] when y can get to the end of the array. Obvious, memory corruption, particularly if your template type has its own copy constructor.

Finally, make print put a std::endl; onto std::cout when you have finished writing.

^^^^^^^^^^^^^^^^^^^^^

both of you,thank you so much;

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.