Skip to main content

How To Think Like a Computer Scientist C++ Edition The Pretext Interactive Version

Section 7.6 A run-time error

Way back in Subsection 1.4.2 I talked about run-time errors, which are errors that don’t appear until a program has started running.
So far, you probably haven’t seen many run-time errors, because we haven’t been doing many things that can cause one. Well, now we are. If you use the [] operator and you provide an index that is negative or greater than length-1, you will get a run-time error and a message something like this:
index out of range: 6, string: banana
Try it in your development environment and see how it looks.
Listing 7.6.1. Running this active code will result in a runtime error. Can you fix it so that we print out the first letter and last letter of string greeting instead of indexing out of range?

Checkpoint 7.6.1.

Checkpoint 7.6.2.

Construct a block of code that correctly changes the string to say “cat in the hat” instead of “cat on the mat”, then print it. Change the necessary characters in order from index 0 to the end of the string.
You have attempted 1 of 4 activities on this page.