May 21, 2013 at 7:01pm UTC
Hello,
I'm trying create a function that return a array with all ids.
I tried the below code but not worked
#include <iostream>
#include <string>
using namespace std;
string ide()
{
int nanim;
string id[nanim];
cout<<"Enter the number of animals: ";
cin>> nanim;
for (int i=0; i<nanim;i++)
{
cout<< "\nEnter id anim "<<i+1 << "\n";
cin>> id[i];
}
return id;
}
int main()
{
string id = ide();
cout << id <<"\n";
return 0;
}
May 21, 2013 at 7:30pm UTC
This code of course is invalid by several reasons. First of all the return type of function ide is std::string while in the return statement there is array name id that is converted to the pointer to the first element of the array.
Also you should specify a constant expression as the size of an array. In the program variable nanim was not initialized so the next statement has undefined behavior.
1 2
int nanim;
string id[nanim];
In such cases it is better to use std::vector instead of arrays.
Last edited on May 21, 2013 at 7:32pm UTC
May 21, 2013 at 7:41pm UTC
How can do I rewrite this code for return array? I cant specify a constant expression as the size of an array because I do Know the size...
Thank's
May 21, 2013 at 9:44pm UTC
When I do the last code; I cant print all ids, i can print just one id...How can I do to print the array?
I tried:
#include <iostream>
#include <string>
using namespace std;
string * ide(size_t nanim)
{
cout << "Enter the number of animals: ";
cin >> nanim;
string *id = new string[nanim];
for ( size_t i=0; i < nanim; i++ )
{
cout<< "\nEnter id anim "<< i+1 << ": ";
cin >> id[i];
}
return id;
}
int main()
{
size_t n;
string *id =ide(n);
int i=0;
cout << id[i] <<i + 1<< endl;
}
May 21, 2013 at 9:51pm UTC
#include <iostream>
#include <string>
using namespace std;
string * ide(size_t &nanim )
{
cout << "Enter the number of animals: ";
cin >> nanim;
string *id = new string[nanim];
for ( size_t i=0; i < nanim; i++ )
{
cout<< "\nEnter id anim "<< i+1 << ": ";
cin >> id[i];
}
return id;
}
int main()
{
size_t n;
string *id =ide(n);
for ( size_t i = 0; i < n; i++ ) cout << id[i] <<i + 1<< endl;
}
May 21, 2013 at 10:02pm UTC
ok Thank you!
But I did not want for in the int main..
I tried
#include <iostream>
#include <string>
using namespace std;
string * ide()
{
size_t nanim;
cout << "Enter the number of animals: ";
cin >> nanim;
string *id = new string[nanim];
string *id2 = new string[nanim];
for ( size_t i=0; i < nanim; i++ )
{
cout<< "\nEnter id anim "<< i+1 << ": ";
cin >> id[i];
cout << id[i] <<i + 1<< endl;
}
return id;
}
int main()
{
string *id =ide();
cout << id << "\n";
return 0;
}
But my output was:
Enter the number of animals: 2
Enter id anim 1: 22
221
Enter id anim 2: 333
3332
0xb25ed8
--------------------------------
Process exited with return value 0
Press any key to continue . . .
What is the 0xb25ed8?...
I woud like my id array :22
333
Thank you
Last edited on May 21, 2013 at 10:15pm UTC
May 21, 2013 at 10:19pm UTC
0xb25ed8 is the address of the first element of the array that you are returning from the function.
Also it is not clear why you allocated two arrays.
string *id = new string[nanim];
string *id2 = new string[nanim];
May 21, 2013 at 10:22pm UTC
I'm sorry
I put the wrong code
string * ide()
{
size_t nanim;
cout << "Enter the number of animals: ";
cin >> nanim;
string *id = new string[nanim];
for ( size_t i=0; i < nanim; i++ )
{
cout<< "\nEnter id anim "<< i+1 << ": ";
cin >> id[i];
cout <<i + 1<< ":"<< id[i] << endl;
}
return id;
}
int main()
{
string *id =ide();
cout << id << "\n";
return 0;
}
May 21, 2013 at 10:41pm UTC
It is not very important for the question that you put a wrong code.
In any case id is a pointer. So line
cout << id << "\n";
displays its value that is the address of the first element of the allocated array.