Help with Program getSize, getSpace, inputData, printData, Destroy

I'm getting error messages "No matching function for call to 'inputData', 'printData', and 'Destroy'.

This is the problem:
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.



Here is my code:

#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 << "Type your string: ";
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
The function inputData takes two parameters. An int, and a pointer-to-string.

You are trying to feed it an int, and a pointer-to-pointer-to-string. The second parameter is supposed to be a pointer-to-string.

You're doing the same thing with the other two.
So what should i change it to?

Sorry I'm new to programming.
string* ptrSpace;
Here, you created a pointer-to-string, named ptrSpace.

So pass ptrSpace.
inputData(sizeOfString, ptrSpace);
I changed it but its still not working.
Its giving me these two errors:
Undefined symbols for architecture x86_64:
"getSpace(int, std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> >*)", referenced from:
_main in main.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)

Heres my code after i changed it:
#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 << "Enter the 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;
}
The prototype:
void getSpace(int, string*);
and the function header:
void getSpace(int sizeOfString, string*& str)
do not match.

In this case, the & (pass by reference) should be used in both cases, since the pointer will be modified inside the function.
I changed it and when I ran the code, I got this:
how many strings do you want? 3
Enter the string you want: hi hello bye
hi
hihello
hellobye
byehi
hello
bye
Program ended with exit code: 0


I'm not sure if this is correct. This is what was asked:
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.

Here is my modified code:

#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 << "Enter the 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;
}
In this function:
1
2
3
4
5
6
7
8
9
10
11
12
void inputData(int sizeOfString, string *space)
{
    cout << "Enter the string you want: ";
    string data;
    for (int i = 0; i < sizeOfString; i++)
    {
        cin >> data;
        cout << data << endl;
        space[i] = data;
        cout << space[i];
    }
}

the two cout statements seem ok for debugging, but once the code is working, in my opinion they add confusion, by mingling extra text with the text entered by the user.

You might also put the prompt message "enter the string" inside the loop. maybe something like this:
1
2
3
4
5
6
7
8
void inputData(int sizeOfString, string *space)
{
    for (int i = 0; i < sizeOfString; i++)
    {
        cout << "Enter string number " << i+1 << " :";
        cin >> space[i];
    }
}


I've not tested that, I think it's ok.

You might also consider using getline() if the string may contain spaces. (But that may open another can of worms).

Last edited on
Thank you so much!
So with the directions of the problem, the program should print this correct?
How many strings do you want? 3
Enter string number 1 : hello
Enter string number 2 : hi
Enter string number 3 : bye
hello
hi
bye
Program ended with exit code: 0
Last edited on
Yes, that seems reasonable.
Topic archived. No new replies allowed.