Question 1: Write a program [7.5]

Write a complete and correct C++ program that fulfills all the requirements below:

  1. The program must prompt the user to enter a value in the range 0 to 1.
  2. If the user enters a non-numeric value, the program should display an appropriate error message.
  3. Otherwise, if the user enters an out-of-range value (smaller than 0 or larger than 1), the program should display an appropriate error message.
  4. Otherwise, the program should compute and display the square root of the the value they entered (use the sqrt function from the cmath library).
Some sample runs of the program, user input shown in bold italics:
Please enter a number in the range 0 to 1:
-1
That value is too small.
Please enter a number in the range 0 to 1:
foo
That is not a number. 
Please enter a number in the range 0 to 1:
1.01
That value is too large.
Please enter a number in the range 0 to 1:
0.25
The square root of 0.25 is 0.5.

SAMPLE SOLUTION
#include <cstdio>
#include <cmath>

int main()
{
   float userVal, root;
   printf("Please enter a number in the range 0-1:\n");
   int numRead = scanf("%g", &userVal);
   if (numRead == 0) {
      printf("That is not a number\n");
   }
   else if (userVal < 0) {
      printf("That value is too small\n");
   } 
   else if (userVal > 1) {
      printf("That value is too large\n");
   } 
   else {
      root = sqrt(userVal);
      printf("The square root of %g is %g\n", userVal, root);
   }
   return 0;
}