The TWindow class created by AppExpert plays the role of our Application class in our other examples. As such it will house most of the commands that we had in our application. As in the case of application, the TWindow class Exam5Window will need to have a member groceries of type GroceryList.
As in the Easy Windows program, we will have a function for each of the menu commands. These functions are associated with the commands by OWL (Borland's Object Windows Library). The associations are created automatically if you use Class Expert. Click here to see the details of associating functions with commands.
Once you have created your functions to handle the commands, most of the code for these functions will look very similar to that used in the Easy Windows program. compare the EasyWindows function print with its Windows counterpart
void MyApplication::printList()
{
cout.precision(2); // two place accuracy on output
cout.setf(ios::fixed);// fixed point notation
groceries.print(cout);
}
void Exam5Window::cmPrintList ()
{
// INSERT>> Your code here.
char buff[500];
ostrstream out(buff,500);
out.precision(2); // two place accuracy on output
out.setf(ios::fixed);// fixed point notation
groceries.print(out);
MessageBox(buff,"The Groceries");
}
The main difference between the two functions is that the output stream is no longer cout. In
fact since we must provide a window to write the output. Most windows output is done
with primitive c language strings, and the easiest way to create these strings is to
create a string stream. String streams use all of the standard i/o type conversions so you
can write (or read) any of the standard types and conversions are done when needed.
To create a string stream, you first need a character buffer to write to (or read from). In this case we needed an output buffer, but the buffer is defined in the same way for an input stream.
char buff[500];The variable buff (this can be anything, but buff indicating buffer is fairly standard) is an array of char, with 500 character positions. In this case since we are not going to do much with this program 500 should be large enough.
The output stream is created by a call to its constructor. The constructor requires the buffer and its size:
ostrstream out(buff,500);
Once the buffer open output is almost identical to that in the original program. One note about the print function for GroceryList (below), it has an extra line in bold. When working with strstreams you must make sure that the last item in the array is the null character '\0'. This marks the end of a c language string. Other than this function is the same as that used in easy windows.
The function addItem requires user input. We will use the simplest form of input the TInputDialog. Consider the function addItem. Notice that TInputDialog reads a c language string buff, so that all of the items must be converted from strings to their appropriate type. In the case of strings name and id, there is a constructor that initializes strings from char*, the c language equivalent of an array of char. To create the double needed for cost call on the c function atof (a means string to (convert to) f means float). The conversion from an integer is atoi (a (string) to i (int)). Once we have the for data items needed to create the GroceryItem we create it and add it to the GroceryList.