This lab will help you practice with function design, making libraries, and string manipulation.
The compiler your employer has given you to work with comes with a slightly substandard library. It is missing the function stol (as described at cppreference.com). (Note that we don't have the concept of pointers yet — the star on the second argument to the function, but we do have references which can allow an extra value to be given back as 'output' from a function. This will become key as you read on.)
Write such a function yourself (I called mine str_to_long). Write a driver program for testing it.
Make sure the following cases all work:
"1234" --> 1234 "-123" --> -123 " 1" --> 1 " -1" --> -1 " +1" --> 1 "+1.4" --> 1 "apes" --> undefined or 0 "2 boxes" --> 2 "12false" --> 12 " -12.3 " --> -12
Note the optional leading sign and the ignoring of leading whitespace and trailing extra information.
(Of course, not all testing possibilities are covered here, but these are ones I see people miss or have trouble with most often.)
You should "return" to the caller not only the translated long integer value but also the position within the string where translation stopped. For the above cases, the translation stop position (a string::size_type, you'll recall) would be:
"1234" --> 4 "-123" --> 4 " 1" --> 4 " -1" --> 4 " +1" --> 4 "+1.4" --> 2 "apes" --> 0 "2 boxes" --> 1 "12false" --> 2 " -12.3 " --> 4
Note: You are not allowed to use stol, strtol, istringstream, or the like.
What argument(s) does your function need? Will you change the argument(s)? What type of value(s) (if any) does it return?
How can you easily skip past the beginning whitespace? Is there a library function that might help detect that the characters within the string are whitespace characters? (Hint: It's not a string library function.)
Is the translation of "-123" actually significantly different than that for "+123" or "123"? What one little thing must change?
When translating "1" how can you access just that character '1'? What other character is in the string?
When translating "1" how can you convert the character representation of the digit 1 to a numeric version (i.e. how can you make '1' become 1)? (Would type-casting help? Maybe a quick look at the ASCII chart in Appendix 3? Or even skimming through this function from another resource might help.)
When translating "12", how can you ensure that you end up with 12 instead of 3? (i.e. You don't just add the digits together, right? Maybe you should look back at the notes on how cin's >> operator does it...)
How can you stop and ignore the extra information in "+1.4"? (i.e. the ".4" is irrelevant to the translation of the integer value "+1" --> 1.) What tells you you are out of useful information? Is this different from the string "1 box"? How about "12false"? Is there a library function that might help detect what kind of character you are [about to be] dealing with? (Again, not a string function.)
How can you tell the caller the position at which you stopped translating? (Hint: Is there a reason that return wasn't highlighted as a keyword above but rather put in quotes like that?)
When the string contains no integer, how can you have the user's value be/remain undefined? (That is, in the same state that it entered your function..?)
How can the user detect that their value hasn't been modified? (i.e. when their string didn't contain a valid integer, how can they tell?) (Hint: If you couldn't translate anything, what position did you tell them you stopped?)
When translating a string to an integer, we can start from either the front or the end of the string. Discuss (briefly) the merits of each and conclude which is best.
This assignment is (Level 4).
Add (Level 1) to place your string to long integer translating function in a library (I called mine strextra).
Add (Level 1) to allow the caller of your function to tell you the starting position from which to translate. (Note: this shouldn't take very much effort at all! ...and ideally would add no extra arguments...)
Examples:
string, trans_from value, trans_to "1234", 0 --> 1234, 4 "-123", 0 --> -123, 4 " 1", 0 --> 1, 4 "+1.4", 0 --> 1, 2 "+1.4", 2 --> undef, 2 "+1.4", 3 --> 4, 4 "app4", 3 --> 4, 4 "1+12", 0 --> 1, 1 "1+12", 1 --> 12, 4 "1+12", 2 --> 12, 4
Add (Level 3) to write a 'str_to_dbl' function to translate a string to a double (if possible). Just like the 'str_to_long', this one should allow an optional leading sign, ignore any leading whitespace, and ignore any other trailing information.
Make your str_to_dbl translate 'e' notation as well as normal numbers (like >> and the compiler do) and I'll throw in another (Level 1.5).
I'll even give you another (Level 1) to write your str_to_dbl so that it reuses your str_to_long for both the whole and fractional parts' translations (and the 'e' part's translation if you did that option).
Note: this will require two extra arguments to the str_to_long to configure whether leading sign is allowed or not and whether leading whitespace is allowed or not. These two arguments should not interfere with anyone else using the function — i.e. they shouldn't have to pass extra arguments they aren't interested in passing. ...perhaps an overload? ...perhaps defaulted arguments?
Heck! I'll even let you repeat the 'translate start position' option with the str_to_dbl function for another (Level 0.5).
Now for the big dogs!
Add (Level 4) to write a function that can translate a long integer into a string.
What? No shivering? No running screaming from the room? I'll give you a second to think it through, then...
Ah! That's better! Have fun!
And the horses that hauled the dogs in:
Add (Level 5) to write a function that can translate a double value into a string.
(Note: This will NOT be using the long --> string function above.)
*holds ears* WOW! THAT'S REALLY DEAFENING, ISN'T IT?! *slams door after stampede* OKAY THEN...*ahem* Sorry, okay then, let's work this out... *looks around for volunteers* Crap! *snaps fingers* Lost another class full!
Note: This lab is adapted from ones given in textbooks by Walter Savitch. Mr. Savitch also inspired the sample support function referenced above.