reference problem
Feb 8, 2014 at 11:49am UTC
Hi I have to write a code, in which will be a function insert. The function should give us ability to write array in it which has to have 12 numbers in it. However, the main thing is that I have to use reference. I do not know if it is really called reference in english I am international but I mean - & - under it. Would you please be so kind to help me with the error.
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
#include <iostream>
using namespace std;
void insert(int &);
int main(int argc, const char * argv[])
{
int array[12];
int &t=array[12];
insert(t);
}
void insert(int &t){
for (int i=0;i<12;i++){
cin>>t[i];// it is error
}
}
Feb 8, 2014 at 12:07pm UTC
If anybody else needs it:
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
#include <iostream>
using namespace std;
void insert(int (&t)[12]);
int main(int argc, const char * argv[])
{
int array[12];
int (&t)[12]=array;
insert(t);
}
void insert(int (&t)[12]){
for (int i=0;i<12;i++){
cin>>t[i];
}
for (int i=0; i<12;i++){
cout<<t[i]<<endl;
}
}
Feb 8, 2014 at 12:22pm UTC
The
t array reference in
main() isn't needed.
Here is your code, cleaned up:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
#include <iostream>
void insert(int (&t)[12]);
int main()
{
int array[12];
insert(array);
}
void insert(int (&t)[12])
{
for (int i=0; i < 12; i++)
{
std::cout << "t[" << i << "]: " ;
std::cin >> t[i];
}
for (int i=0; i < 12; i++)
std::cout << t[i] << ' ' ;
}
Topic archived. No new replies allowed.