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.
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>
/*********** 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
*/
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.
/*********** 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 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).
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