I'm working on an assignment for my class to write a program which will:
1. Prompt the user for the number of characters in a string (actually a cstring, we haven't learned strings yet)
2. Allocate a string of sufficient length (one more than # of characters)
3. Prompt the user for the string using getline
4. Display the string back to the user
5. Release the memory and for allocation failures
I *think* I have allocated the memory correctly based off what I've seen in videos and tutorials.
#include <iostream>
usingnamespace std;
int getSize()
{
int size;
cout << "Enter number of characters: ";
cin >> size;
return size;
}
void getText(char text[], int pSize)
{
cout << "Enter text: ";
cin.ignore();
cin.getline (text, pSize);
}
int main()
{
int *pSize = new (nothrow) int;
pSize = newint;
*pSize = getSize();
if (*pSize <= 0)
{
cout << "Allocation failure!";
return 0;
}
cout << "The size is: " << *pSize;
char text[*pSize];
text = getText(text, *pSize);
cout << "Text: " << text << endl;
return 0;
}
The compiler returns this error:
[Error] incompatible types in assignment of 'void' to 'char[(((sizetype)(((ssizetype)(*pSize))+ -1)) + 1)]'
I have no idea what that error is supposed to be telling me or how to fix it. Or if I'm even on the right track for that matter for figuring out how to do this. (By the way, my knowledge of pointers is bad at best)
The syntax for dynamic memory allocation is: type* variableName = new type(constructor arguments here); if you're allocating a single type element, or
type* variablename = new type[how many]; for an array.
To free (deallocate) the memory afterwards, you call delete variableName; if you just allocated one, or
delete[] variableName; if you allocated an array.
So, to allocate memory for a C string (which is just a char array), you would do char* myStringName = newchar[howManyCharsPlusOneForTheNullTerminator];
#include <iostream>
usingnamespace std;
int getSize()
{
int size;
cout << "Enter number of characters: ";
cin >> size;
return size;
}
void getText(char text[], int pSize)
{
cout << "Enter text: ";
cin.ignore();
cin.getline (*text, *pSize);
}
int main()
{
int* pSize = new (nothrow) int;
pSize = newint;
*pSize = getSize();
if (*pSize <= 0)
{
cout << "Allocation failure!";
return 0;
}
cout << "The size is: " << *pSize;
char* text = new (nothrow) char[*pSize + 1];
text = getText(text, pSize);
// cout << "Text: " << text << endl;
return 0;
}
PS: Im not quite sure exactly why I'm using nothrow except my book is using it. From what I understand is that if an invalid entry is returned, the it sets the value to NULL?
My errors are:
1 2 3
[Error] invalid conversion from 'int*' to 'int' [-fpermissive]
[Error] invalid conversion from 'int*' to 'int' [-fpermissive]
[Error] void value not ignored as it ought to be
I was unable to adjust the size of the memory (I think that's what I'm doing at least) by having size + 1 in the "char* text = new (nothrow) char[size];". I had to do size + 1 within the function call "getText(text, (size + 1));". But either way, I got my code to compile and print out the results I needed to get to get credit for the program :)