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