inputData expects as it's second argument to be of type pointer-to-string. On line 81 you give it a value of type pointer-to-pointer-to-string. Get rid of the &. There is no reason to take the address of the pointer.
I got rid of it, and now I'm getting the error 'ptrSpace' is used uninitialized in this function, I also got rid of the & in printData and Destroy as well
In the OP, the declaration for getSpace and the definition for the same do not match. This would lead to the code not linking properly, so I presume you've made some changes to that code without modifying the OP. If you made the definition match the declaration, that would cause your latest problem. getSpace needs to modify the pointer in main, so it should take a reference-to-pointer-to-string.
#include <iostream>
#include <string>
usingnamespace std;
void getSize(int&);
void getSpace(int, string*);
void inputData(int, string*);
void printData(string* , int);
void Destroy(string*);
int main()
{
int sizeOfString;
string* ptrSpace;
getSize(sizeOfString);
getSpace(sizeOfString, ptrSpace);
inputData(sizeOfString, ptrSpace);
printData(ptrSpace, sizeOfString);
Destroy(ptrSpace);
}
/*********** getSize ************
Ask user to input how many strings
they want.
*/
void getSize(int &sizeOfString)
{
cout << "how many strings do you want? ";
cin >> sizeOfString;
}
/*********** getSpace ************
gets an array in heap of the size
requested by the user
*/
void getSpace(int sizeOfString, string* str)
{
str = new string[sizeOfString];
}
/*********** inputData ************
Allow users to input string values
and store them in the array
*/
void inputData(int sizeOfString, string *space)
{
cout << "now put string you want : ";
string data;
for (int i = 0; i < sizeOfString; i++)
{
cin >> data;
cout << data << endl;
space[i] = data;
cout << space[i];
}
}
/*********** printData ************
Print out all the strings
One string per line
*/
void printData(string *space, int sizeOfString)
{
for (int i = 0; i < sizeOfString; i ++)
{
cout << space[i] << endl;
}
}
/*********** Destroy ************
Retrun all the space to the heap
*/
void Destroy(string *space)
{
delete [] space;
}
When I run this program, it prompts me to enter the amount of strings, fine. Then it prompts which string. After I enter anything, the program seems to malfunction and I'm not sure why. Any ideas anyone?
I think you didn't read my last post. getSpace should take a reference-to-pointer-to-string. Currently you are only modifying a local-to-getSpace variable that shares the same name as a different variable in main. You are not modifying the variable in main, so when you dereference the pointer in main you get undefined behavior as it just points to some random place in memory.