Start by fixing any problems from project 7, then continue on to the new functions.
Operator>> is fairly hard, and it's unnecessary for testing any other function, so it should be the last thing you do. This should be obvious by now, but many people structured their Project 6 testing modules in such a way that it looks like they didn't follow our advice.
For this one, it is especially critical that you develop the function in several stages. Start with the simple case, where there is no leading whitespace, and get it to work for a single string, stopping at the first whitespace character. You will need to input to a char array locally, checking that the input was successful, then call the constructor to store the new string.
istream&
operator>>(istream& ist, String& item)
{
// build char array
char array[256];
...
//
store in object
if (...)
item = String(array);
return ist;
}
Obviously, this requires operator= to be working.
Depending on how you did this, you may need to do some extra work to skip
leading whitespace. The easiest way is to use peek() to look ahead in the
input stream and get() to throw out characters one at a time in a loop.
char ch;
ch = ist.peek(); // returns next character
(WS
or no
t)
, but leaves it in the input buffer
ch = ist.get(); // removes next character
from
input buffer and stores it
ist.get(); // removes next character
without storing it
Other methods involve some combination of the following istream functions and manipulators:
You can read about these on the cplusplus.com istream reference page.
No matter what you do, ideally, you should not consume any whitespace following the string you just read. However, if you cannot get the function to skip leading whitespace, then it will need to consume (though perhaps not store) at least one character per call in order for it to ever read more than one string (a very strange state of affairs). To reiterate:
You must use a variety of test cases to make sure that each function behaves as it should. In operator== from Project 7, for example, you needed to compare strings with the same length but different characters in order to see if the loop actually worked.
Do each function one at a time, testing the critical cases. Do not use an input loop that calls every function for every test case. Print your strings in quotes or brackets so you can easily tell whether or not there is leading/trailing whitespace.
Be careful with operator= and operator+= since they must do several things, like the value constructor. Again, you can run your program in "valgrind" to find out if it is deallocating memory properly.
With operator>>, you must test that it is ignoring whitespace properly. The most clear way to demonstrate this is to NOT use an input loop, but to label each case properly ("this is what I'm inputting") and put exactly the right contents in your "tests" file. Using an input loop is okay, but not ideal.
When you test constructors, note that the following lines all call the copy constructor:
In order to test operator=, you must declare the object separately.
This is true for value constructors as well, if there is only one parameter. See the example on constructors and assignment for the full picture.