Topical Information

This lab will help you practice with function design, making libraries, and string manipulation.

Program Information

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.

Thought Provoking Questions

  1. What argument(s) does your function need? Will you change the argument(s)? What type of value(s) (if any) does it return?

  2. 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.)

  3. Is the translation of "-123" actually significantly different than that for "+123" or "123"? What one little thing must change?

  4. When translating "1" how can you access just that character '1'? What other character is in the string?

  5. 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.)

  6. 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...)

  7. 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.)

  8. 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?)

  9. 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..?)

  10. 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?)

  11. 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).

Options


Note: This lab is adapted from ones given in textbooks by Walter Savitch. Mr. Savitch also inspired the sample support function referenced above.