Help with code

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
85

#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 *ptrSpace)
{
    ptrSpace = 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;
}


any idea why this doesnt work?
basically i get number of charater from user
i made a array of character with size that user wants
and store strings in there and print out
lastly i just delete the memory.
seems okay to me but doesnt work
1
2
3
4
void getSpace(int sizeOfString, string *ptrSpace)
{
    ptrSpace = new string[sizeOfString];
}
Parameter ptrSpace is passed by value. That means any changes to it will not be visible outside of function.

Pseudocode for clarity:
1
2
3
4
5
6
void getSpace(arg1, arg2)
  int sizeOfString = arg1;
  string* ptrSpace = arg2;
{
    ptrSpace = new string[sizeOfString];
}
thanks for your help,
so how should i write it to make it visible from outside of funciton?
This was the problem requirement

Write a program containing the following functions, in this order:

main - calls the other functions; otherwise does almost nothing
getSize - which asks the user how many strings they want
getSpace - which gets an array in the heap of the size requested by the user
inputData - which allows the user to input the strings and stores them in the array
printData - which prints all the strings, one string per line
destroy - which returns all the space to the heap

All of these functions, except main, shall have a return type of void.
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
85
86
#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*);
string* arg2;
int arg1,sizeOfString = arg1;
string* ptrSpace = arg2;

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(arg1, arg2)
{
    ptrSpace = 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;
}
Last edited on
It does not work.. :(
so how should i write it to make it visible from outside of funciton?


1
2
3
4
string* getSpace(int sizeOfString)
{
    return new string[sizeOfString];
}
it has to be void :(
1
2
3
4
void getSpace(int sizeOfString, string*& str)
{
    str = new string[sizeOfString];
}
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
85

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


giving error saying undefined reference to 'getSpace(int, std::string*)
Because you are trying to pass an r-value to it. Think, where will pointer returned by new will be saved? You do not have any variables which cn hold the result. Correct call:
1
2
string* ptrSpace;
getSpace(sizeOfString, ptrSpace);
Wow..
Thank you!
Topic archived. No new replies allowed.