CIS 150 C++ Programming Project: Functions, input validation

Objectives

  1. Create function prototypes
  2. Create and use functions
  3. Validate user input
  4. Implement pseudocode

Overview

This assignment gets a start value and an end value from the user. Both values must be validated as integers within specified ranges. The program then displays a list of all the prime numbers between those two values.

Program requirements

  1. Add documentation comments to the start of your program, including file name, course, your name, the date, and a brief description of the purpose of the program.
  2. Include the needed libraries: cmath, iostream, sstream, and string
  3. Create prototypes for the two functions that you will implement after main (getInt and isPrime).
  4. Implement main
    1. Input: Either nothing, or "int argc, char* argv[]" (which represent the command line arguments)
    2. Output: Returns an integer representing the exit status (0 for success).
    3. Side effects: Gets two integers from the user representing a range and displays the prime numbers in that range.
    4. Use getInt to display a prompt ("Enter starting integer (1 - 100): ") and get an integer from the user within the range 1 through 100. Store the result.
    5. Use a stringstream to create a string prompt for the second user input. Example:
      stringstream s;
      s << "Enter ending integer (" << (min+1) << " - 1000): ";
      Then use s.str() wherever you need to turn it into a regular C++ string object
    6. Use getInt to display a prompt for the ending value in the range. Use the custom prompt you created in the last step. The range for this value should be from 1 greater than the previous number the user typed in through 1000. Store the result.
    7. Display a line stating what will be displayed next (including the first and last values in the range).
    8. Create a loop which will iterate through all the values in the range. For each value, call isPrime with that value to determine if the value is prime. If it is prime, then display it followed by a newline.
    9. Return a success (0) exit status.
  5. Create the getInt function following these requirements:
    1. Input: a string prompt, an integer minimum value, and an integer maximum value.
    2. Output: Returns an integer the user entered.
    3. Side effects: Displays the prompt, gets input from the user, and may display error messages and reprompt the user as needed.
    4. Create a Boolean value to tell you if you have a valid value to return. Initialize it to false.
    5. Create an integer variable to store the user input.
    6. While you do not have a valid value to return do the following:
      1. Prompt the user to enter a value.
      2. Read an integer value in from the user.
      3. If that read did not cause an error then:
        1. If the value is less than the minimum, display an appropriate error message.
        2. If the value is greater than the maximum, display an appropriate error message.
        3. If the value is within range, set the Boolean value to indicate we have valid input.
      4. Otherwise, display an error message that an invalid integer has been entered, clear cin's error flags, and clear the input buffer.
    7. Return the user input.
  6. Create the isPrime function following these requirements:
    1. Input: an unsigned integer value.
    2. Output: true if the value sent in is prime, and false if it is not prime.
    3. Side effects: None.
    4. Let limit be the integer (truncated) square root of the value sent in.
    5. If the number sent in is 0 or 1, the result should be false.
    6. If the number sent in is 2, the result should be true.
    7. If the number sent in is divisible by 2, the result should be false.
    8. If none of the above conditions has been met, assume the number sent in is prime (result is true) and set up a loop to prove otherwise. The loop should run from 3 through limit, incrementing by 2. On each iteration, check to see if the number sent in is divisible by the loop counter. If it is, then the result is false and you should end the loop and return.
    9. Return the result.

Sample Run

Enter starting integer (1 - 100): SpongeBob SquarePants
Error: Invalid integer
Enter starting integer (1 - 100): -4
Error: Value below minimum of 1
Enter starting integer (1 - 100): 700
Error: Value above maximum of 100
Enter starting integer (1 - 100): 1
Enter ending integer (2 - 1000): 1
Error: Value below minimum of 2
Enter ending integer (2 - 1000): Patrick Star
Error: Invalid integer
Enter ending integer (2 - 1000): 123
Primes between 1 and 123
2
3
5
7
11
13
17
19
23
29
31
37
41
43
47
53
59
61
67
71
73
79
83
89
97
101
103
107
109
113

Rubric