Originally Posted by
[Re]cUrSivE
No he didn't say key press can only be recieved using >> operator, but since the functions I mentioned above are the only ones we are allowed to use, I assumed one of those has to be the one that could be used to get key press.
I will just post my entire code here, maybe you can give me an advice or 2 or 3 on how to better make use of them. The function names were all given to us by the prof, so I "have to" use the ones I used in my code.
.........
Btw, do I have to set the vector to NULL (in this function void DestructSingleVector(Vector* &vector)) even after deleting it? I thought that is what delete does?
The goal of the assignment is to practice TDD, which is why I used the ifdef units to test certain parts and make sure they work before moving on. So right now, I am testing for getting user input which is taking longer than necessary. But I think I will now go with fgets and sscanf and see where those will get me.
E: Btw in your isDigits() function, it will not catch the new-line character because (maybe this is just on macs) when I used fgetc() to get the value returned by the new_line character, it returned 10. And when I used it get return for end of file character, it returned -1. So your function might only work for EOF. Hmm, I wonder if it only returns the 10 because the fgetc() function returns type integer...
The names of the functions are given to you? OR is it the definition of the function that is given to you? Also don't worry about the function names I used. The logic is what you should see rather than function definition.
Well it's usually good practice to set global pointers to NULL when they are no longer going to be used. I passed a reference to a pointer and deleted it. It's only right I set it to NULL to guarantee it points to nothing (the standard guarantees this either way) and to make specific to the user that it should not be used. Almost everyone knows that you should not write to a NULL pointer and that reading from it is undefined/garbage.
Also I showed an example using fgets already :S It does indeed detect the enter key. The IsDigits is passed the length of the buffer - 1. That's so that it ignores the newline/null terminating character at the very end of the array. The thing is, fgets BREAKS on Enter-key press. Thus you know if the value of the vector at that index is 0, the Enter-key was either pressed OR the user entered 0.
You can test that it works by setting that index to -1 if the buffer-length is 0 as the buffer is within the scope of the for-loop and will be recreated every loop.
Also I don't think I'd extend the vector like that..
C++ Code:
template<typename T>
void Delete(T* &Type, size_t Size) //As we delete in the destructor and in resizing and in all C-Style arrays/pointers, might as well template this.
{
if (Size > 1)
delete[] Type;
else
delete Type;
Type = NULL;
}
template<typename T>
void ResizePointer(T* &Array, size_t OldSize, size_t NewSize) //Templated because it'd be so useful in the future for any array's/pointers. Does NOT work on Void*. For that, use ::Operator New.
{
if (OldSize < 1)
{
Array = new T[NewSize];
}
else if (NewSize > 0)
{
T* Temp = new T[NewSize];
for (int I = 0; I < OldSize; ++I) //Copy the elements of the old array to a temp array.
Temp[I] = Array[I];
Delete(Array, OldSize); //Delete the old array and allocate a new array using the original pointer.
Array = new T[NewSize];
for (int I = 0; I < NewSize; ++I) //Copy back all the old elements and set all the new ones to default/null
Array[I] = I < OldSize ? Temp[I] : T(0);
Delete(Temp, NewSize); //Clean up the temporary.
}
else
Delete(Array, OldSize);
}
//Using the above Templates:
void ResizeVector(Vector* &List, size_t NewSize)
{
ResizePointer(List->new_Elem, List->numElements, NewSize);
List->numElements = NewSize;
}
//Without using the ResizePointer Template:
void ResizeVector(Vector* &List, size_t NewSize)
{
if (NewSize > 0)
{
Elem* TempElements = new Elem[NewSize];
for (int I = 0; I < List->numElements; ++I)
TempElements[I] = List->new_Elem[I];
Delete(List->new_Elem, List->numElements);
List->new_Elem = new Elem[NewSize];
for (int I = 0; I < NewSize; ++I)
List->new_Elem[I] = I < List->numElements ? TempElements[I] : 0;
List->numElements = NewSize;
Delete(TempElements, NewSize);
}
else
Delete(List->new_Elem, List->numElements);
}