The purpose of this quiz is to give you a chance to focus your knowledge of formatting data in files.
If a data file consists of a simple list of values (one right after the other), we say it has a sequential data format. If, on the other hand, a single data value actually consisted of several simple values (like a student consists of a name, gpa, address, etc.), we say the data has a block data format. When each simple value is given a name, we say the data is in a labeled format.
| TRUE ✗ | FALSE ✓ | Labeled and block formats cannot be mixed together. | ||
|---|---|---|---|---|
| TRUE ✗ | FALSE ✓ | Labeled and sequential formats cannot be mixed together. | ||
| TRUE ✗ | FALSE ✓ | Sequential and block formats cannot be mixed together. |
Explain (briefly) how one would translate a string containing a labeled data line (label = value). Start from separating the label and value and continue until you have the value stored in the proper variable location. (Don't forget to check for poorly formed data lines!)
Search for the separator (= in the sample) in the labeled data line. If not
found, maybe print a message.
If found, copy the text before this to a label string and the text after it
to a value string. Now strip the spacing off both sides of the label and
value strings.
In a case-insensitive way, search the array of known labels for this label.
Match the array location to code to process the value appropriately (a switch
would be helpful here). If not found, maybe print a message.
In the branches that handle each value, translate the string content to the
proper data type for that class member/parameter.
How do you translate a string to a character? (Think of special cases: " apple", "\t\n\bsmile", etc.)
Use the first non-space character (!isspace) in the string.
Once you can translate a string to a character, is translating to bool much harder? Explain...
Just compare -- case-insensitively if necessary -- the char to a stock of
true representations depending on our our agreement with the user. I.E.
'y', '1', 't', etc.
When translating a string to a double, how do we re-use our ability to translate a string to an integer?
Just translate the whole part with our integer translator and repeat for
the decimal part. Make sure to get a count of characters translated for
the decimal part! Now convert the integer decimal part to a decimal with
a pow call having 10 to the negated power equal to the count of characters
mentioned above. Add the whole part and the decimal to get the number that
was in the string.
I.E. If we had "12.3456", we'd first get 12 and 3456 with 4 as our count.
Now we'd take 3456 and multiply it by pow(10,-4) to make it .3456. Now 12
plus .3456 gives us back the 12.3456 that was in the string.
The reason the count is so important is that we might have "12.0456" which
would have only 456 and a count of 4. If we tried to just count the digits
in the decimal integer, it would lead us to thinking it was 3 digits long
instead of the proper figure.
| TRUE ✗ | FALSE ✓ | Real data files never use labeled data formatting. |
|---|
| TRUE ✓ | FALSE ✗ | The data format of a file is a contract between the end-user and the programmer. |
|---|