CIS 150 C++ Programming Project: Functions, input validation
Objectives
- Create function prototypes
- Create and use functions
- Validate user input
- 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
- 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.
- Include the needed libraries: cmath, iostream, sstream, and string
- Create prototypes for the two functions that you will implement after main (getInt and isPrime).
- Implement main
- Input: Either nothing, or "int argc, char* argv[]" (which represent the command line arguments)
- Output: Returns an integer representing the exit status (0 for success).
- Side effects: Gets two integers from the user representing a range and displays the prime numbers in that range.
- 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.
- 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
- 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.
- Display a line stating what will be displayed next (including the first and last values in the range).
- 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.
- Return a success (0) exit status.
- Create the getInt function following these requirements:
- Input: a string prompt, an integer minimum value, and an integer maximum value.
- Output: Returns an integer the user entered.
- Side effects: Displays the prompt, gets input from the user, and may display error messages and reprompt the user as needed.
- Create a Boolean value to tell you if you have a valid value to return. Initialize it to false.
- Create an integer variable to store the user input.
- While you do not have a valid value to return do the following:
- Prompt the user to enter a value.
- Read an integer value in from the user.
- If that read did not cause an error then:
- If the value is less than the minimum, display an appropriate error message.
- If the value is greater than the maximum, display an appropriate error message.
- If the value is within range, set the Boolean value to indicate we have valid input.
- Otherwise, display an error message that an invalid integer has been entered, clear cin's error flags, and clear the input buffer.
- Return the user input.
- Create the isPrime function following these requirements:
- Input: an unsigned integer value.
- Output: true if the value sent in is prime, and false if it is not prime.
- Side effects: None.
- Let limit be the integer (truncated) square root of the value sent in.
- If the number sent in is 0 or 1, the result should be false.
- If the number sent in is 2, the result should be true.
- If the number sent in is divisible by 2, the result should be false.
- 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.
- 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
- 3 points for proper documentation comments and for correctly following coding conventions (indentation, naming rules, NO tabs, NO global variables, etc.)
- 2 points for having the correct function prototypes
- 5 points for having the main function working properly
- 10 points for having the getInt function working properly
- 10 points for having the isPrime function working properly