Need help

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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
#include <iostream>
#include <string>

using namespace 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);

}



void getSize(int &sizeOfString)
{
    cout << "how many strings do you want? ";
    cin >> sizeOfString;
}


void getSpace(int sizeOfString, string*& str)
{
    str = new string[sizeOfString];
}


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];
    }
}


void printData( string *space, int sizeOfString)
{
    for (int i = 0; i < sizeOfString; i ++)
    {
        cout << space[i] << endl;
    }
}


void Destroy(string *space)
{
    delete [] space;
}


Any idea why this doesn't work?
Error I'm getting is cannot convert to std::string** to 'std::string*
Please, any help would be amazing.
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.
Line 81?
That should be line 18.
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
Last edited on
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.
Okay, thank you for your help. I've a new problem now.
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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
#include <iostream>
#include <string>

using namespace 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?
What do you mean by "the program seems to malfunction"? What do you hope to achieve by withholding details from us?
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.
Topic archived. No new replies allowed.