C++ Step 1. Write the code that prompts the user to enter an integer between 1 a
ID: 3557698 • Letter: C
Question
C++
Step 1. Write the code that prompts the user to enter an integer between 1 and 20 (including 1 and 20), reads the value using cin, and then prints the value that they entered in a statement that begins with "You entered a ". Save this version in a separate location so that you can submit it.
Step 2. Add more code or modify your code so that if the user enters a value that is less than 1 or greater than 20, it prints out "The value you entered is not between 1 and 20." Otherwise, and only if the value is between 1 and 20, it prints the statement as before. Save this version in a separate location so that you can submit it.
Step 3. Add more code or modify your code so that if the user enters the values 8, 11, or 18, the statement that is printed is "You entered an" instead of "You entered a". Save this version in a separate location so that you can submit it.
Step 4. Add more code or modify your code so that if the users something that is not a number, i.e. includes letters, your program does not print any of the above lines, but instead prints out "Idiot! Your input could not be read as a number. Get a life!". As mentioned in the previous assignment, you can test to see if the previous cin failed to read a value by calling cin.fail(). Save this version in a separate location so that you can submit it.
Include comments in your code to explain what each of the
Explanation / Answer
#include <iostream>
using namespace std;
int main()
{
int n
cout << "Enter an integer between 1 and 20";
cin >> n
// this tests if the number is in the given limits
if(n < 1 || n > 20)
cout << "The value you entered is not between 1 and 20."<<endl;
// this tests for specific set of mentioned numbers
else if(n == 8 || n == 18 || n == 11)
cout << "You entered an: " << n << endl;
// tis tests if entered an number with letters
else if(cin.fail())
cout << "Idiot! Your input could not be read as a number. Get a life!"
// if all cases are satisfied
else
cout << "You entered a: " << n << endl;
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.