now I got 3 questions:
1) Where does delete[] go? Did I put it right?
2) using cin>>cont allows to enter longer string than intended, how could I limit it that it would automaticaly stop reading after it's full?
3)Is there any way not to display entered text, like replacing it with "*"?
delete[] count will never ever happen, as your function returns before that code is reached. As such, no, you've not done it right.
You must use delete when you no longer want to use that memory. Given that you are returning a pointer to that exact memory, I'm guessing you actually want to use that memory somewhere else? As such, you should delete it only when you no longer need it.
2) using cin>>cont allows to enter longer string than intended, how could I limit it that it would automaticaly stop reading after it's full?
Simple answer; use a proper C++ string that doesn't care about size. Less simple answer; use the get function that comes with a iostream object - http://www.cplusplus.com/reference/iostream/istream/get/ and feed it the max number of characters you want to read.
2)well I'm making some sort of class that should be reading a predefined length, different for every class instance, so a string is not what I need(unless there is a way to limit its size).
char* funct(){
char* cont;
cont=newchar[somenum];
cin>>cont;
return cont;
}
int main()
{
// get an array of char from a function
char* returnedPointer = funct();
// do things with it
cout << returnedPointer;
// do more things with it
someOtherFunction(returnedPointer);
// Now I am all done with it. I have no more use for the data
delete[] returnedPointer;
}
2) using cin>>cont allows to enter longer string than intended, how could I limit it that it would automaticaly stop reading after it's full?
As moschops already correctly pointed out, you should be using more appropriate C++ concepts, such as strings, but if you do actually need to input an array, and you want to use operator>>, it's your responsibility to impose the size limit with setw()
it compiles fine, but when it comes to the loop this is what the compiler returns: Unhandled exception at 0x7646b9bc in WoG.exe: Microsoft C++ exception: std::out_of_range at memory location 0x0032f74