Skip to main content

5.4) Exercise 7


Copy your solution to Exercise 6 to a new file, then modify this program so that it ensures that the values read into variables x and y are in the range 0 to 100. Use separate if statements to do the checking, one for x and the other for y. If a value is out of range, your program should display a suitable error message and then terminate.

  • Use cerr rather than cout to display the error message. This is an ostream object like cout, but it is intended for error messages rather than regular output. (Operating system shells allow you to distinguish between the two - redirecting regular output to a file while allowing error message to still be displayed in the terminal window, for example).
  • Terminate the program by using a return statement with a value of 1 instead of 0. (Note: this approach to program termination can be used inside main but not within other functions - as will be discussed later).