#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 *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
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.
#include <iostream>
#include <string>
usingnamespace 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;
}
#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;
}
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: