Reading specific length

I've been trying to do this:
1
2
3
4
5
6
7
char* funct(){
char* cont;
cont=new char[somenum];
cin>>cont;
return cont;
delete[] cont;
}


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 "*"?
Last edited on
Did I put it right?


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.

3) http://www.cplusplus.com/forum/general/3570/

1)So I don't need to use delete?

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

3)Thanks, I can also use that for n.2!
You should use delete. You should use it when you no longer want to keep whatever data you've put into the memory allocated using new.
but since the function has to return a value, do I have to make another variable and return that after deleting the dynamic one?
No. Return cont, as you are. Then, use cont. Then, when you are all done with that data, delete cont.

Here is an example.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
char* funct(){
  char* cont;
  cont=new char[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;
}


ok thanks a lot!
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()

cin >> setw(somenum) >> cont;
thank you too Cubbi,

I've decided to go with the aproach Moshchops linked(http://www.cplusplus.com/forum/general/3570/)

so now I've hit a wall, I've modified it a bit, I have trouble with deleting the last character:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
string read;
		char ch;
		ch = _getch();
		while(ch !=tENTER&&i<twidth)
		{
			if(ch=tBACKSPACE)
			{
				read.erase(read.length()-1);
			}
			else read.push_back(ch);
			if(pwMode)
			{
				cout<<'*';
			}				
			else
			{
				cout<<ch;
			}
			do
			{
				ch=_getch();
			}while(!(isAlphanum(ch)||ch==tENTER||ch==tBACKSPACE));
			i++;
		}


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
Last edited on
Topic archived. No new replies allowed.