I'm trying to return a string array from a function. Within this function the user will define the legnth of the array and then populate the array. I need the array return to the call so that I can send it to a print function when the time comes. I have include the code that I have, thanks for the help
I'm trying to return a string array from a function. Within this function the user will define the legnth of the array and then populate the array.
Sorry, that's plain impossible in C++ since the size of a C++ array needs to be known at compile-time. Looks like what you actually want is an std::vector that not only can be sized at runtime, but also can be returned from a function much easier than an array.
Note that I didn't actually have a (significant) look at your code because it's practically unreadable due to you not using code tags. Please use them next time you post code, otherwise you can't really expect anyone here to read your code. However, in this case, the answer to your question actually was apparent from the question itself.
I was thrown out of college for cheating on the metaphysics exam; I looked into the soul of the boy sitting next to me.
This is a snakeskin jacket! And for me it's a symbol of my individuality, and my belief... in personal freedom.
I'm trying to return a string array from a function. Within this function the user will define the legnth of the array and then populate the array. I need the array return to the call so that I can send it to a print function when the time comes. I have include the code that I have, thanks for the help
Code:
string *Seri_Num();
You didn't include the <string> header, so this is not guaranteed to compile. You must include all headers that you are using in the module, even if one of those other headers includes <string> just by chance.
Code:
string *Seri_Num()
{
string serNum;
int unit_num;
string *unit= new string[unit_num];
unit_num is uninitialized. Therefore it has a garbage value, but you're using it in the new[] operation.
In general, your code uses error-prone pointer manipulations and has memory leaks. C++ has a std::vector class that has been standard C++ since 1998. Why are you not using it, and instead, you're using error prone methods?
As Eri523 stated, use vector, then the issue becomes much more manageable than your current code.
Code:
#include <vector>
#include <string>
#include <iostream>
using namespace std;
typedef std::vector<std::string> StringArray;
StringArray Seri_Num()
{
int unit_num;
cout << "Enter number of units:" << endl;
cin >> unit_num;
StringArray unit(unit_num);
//...
return unit; // return the array
}
int main()
{
StringArray arr = Seri_Num();
cout << "There are " << arr.size() << " entries in the array\n";
}
That code just created an array of strings, and returned the array. Note that there are no pointers, no dynamic allocation, no memory leaks, etc. In other words, it works.
And next time, please use code tags if you're posting code.
* The Best Reasons to Target Windows 8
Learn some of the best reasons why you should seriously consider bringing your Android mobile development expertise to bear on the Windows 8 platform.