runtime error in the program, but the code is ok

can not understand what is the error in the code. when i compile it and run it ti runs well until it reaches the second iteration of the loop then the program just stopped working error comes out. any help please.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
  #include<iostream>
#include<stdlib.h>
using namespace std;
template <class T> void input(int a, T *array[]){
	for(int i=0;i<a;i++){
	cout<<"Enter : ";
	cin>>*array[i];
}
	for(int i=0;i<a;i++){
	cout<< *array[i];
}
}
main(){
	int a=0;
	cout<<"Enter the number of elements :";
	cin>>a;
	int *array = new int[a];
	input(a, &array);
	delete[] array;
	system("pause");

}

Because you're using the array wrong.

input(a, &array); // remove the & template <class T> void input(int a, T *array[]) // sending the array via * or [] is the same thing, but dont have both. Remove the *

cin>>*array[i]; // remove the *

cout<< *array[i]; // remove the *

Also - main(){ // should int main()
Last edited on
This:
 
cin>>*array[i];

shuld be this:
 
cin>>array[i];
The reason is that line 4 says that array is an array of pointers to T objects.
Topic archived. No new replies allowed.