Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Language: C++, I need help with Task 1, I have completed Task 0, and attached my code. If you could please explain how to do

Language: C++,

I need help with Task 1, I have completed Task 0, and attached my code. If you could please explain how to do Task 1, and provide an answer that would be amazing.

OBJECTIVE:

Secure programming requires validation of inputs, both for types and values. For example, when inputting a variable that should be an integer between 0 and 23, the program needs to validate that the input is indeed an integer (e.g., not a string with non-numeric characters) and that it is in [0,23]. We have done only the latter in the past, and this lab will concentrate on the former.

To do this, we will augment the shapes lab with input validation, and add features along the way.

TASK 0

In the shapes lab, you drew a number of shapes as 5 separate programs (since we didn't have functions at that time). Rewrite the code into at least 4 separate functions (including main()) that take appropriate arguments. These functions will only have side effects, returning void. Then, write a function that does the following:

Present a menu: "Enter (R)ectangle, (T)Triangle, C(ircle), or (E)nd"

Input a character corresponding to the menu choice

Depending on whether the character is R, L, T, or C, input appropriate parameters

Call the appropriate drawing function

The function will do these steps repetitively until "E" is input.

For this task you only need to validate values (not types)

TASK 1

Run your above program, typing in a non-numeric value at a parameter prompt. State what happens in comments. Also state what happens if you enter a number causing overflow. You may need to terminate the program. In Linux this is done by typing ^C. Alternatively, you can open another window, list all processes by typing "ps -e" (grepping the output), and killing the appropriate process by typing "kill -9 ". Now we wish to validate the input for types as well as values to avoid this problem. There are 3 major concepts to be aware of:

The >> function returns false when an input failure occurs. One possible cause of input failure is a type error.

Since the input stream library maintains an internal state, it needs to be reset after an error. This is done by calling cin.clear().

Since there may have been additional data typed on the error line (e.g., "foo 17" on a line expecting 2 integers), the rest of the line needs to be cleared before retrying. The cin.ignore() function indicates that the rest of the input line already read should be ignored.

Although we won't use it in this program, recall that the cin.fail() and cin.eof() functions can also be a major component of input validation.

Identify places in your Task 0 code that need additional input validation, and rewrite the code to handle this. For example, you may write something like the following pseudocode:

output prompt while input n is false reset/clear system output an error message output prompt // assert n contains valid input

My code for Task 0.

#include

#include

using namespace std;

int pth (int x,int y) {

return sqrt (pow(x,2)+pow(y,2));

}

void printSquare (); //prototype for the square creating funtion

void printTriangle (); //prototype for the triangle creating funtion

void printCircle (); //prototype for the circle creating funtion

void printEnd(); //prototype for the end funtion

int main(){

char ch;

cout << "Hello! Welcome to the shape maker. "; //Will refer to as "Mwnu Screen'

while (ch != 'E' || ch != 'e') // Will allow the function to continue until "end" is selected by the user

{

cout << "Please choose a shape to make. "; //Will refer to as "Menu Screen'

cout << "NOTE: Entering (E) will end this program ";

cout << "Enter (R)ectangle, (T)riangle, C(ircle), or (E)nd: ";

cin >> ch;

cout << endl;

switch (ch) //will compare the user's input against the menu optins

{

case 'R':

case 'r':

printSquare();

break; //will stop the loop here and will return to menu screen

case 'T':

case 't':

printTriangle();

break; //will stop the loop here and will return to menu screen

case 'C':

case 'c':

printCircle();

break; //will stop the loop here and will return to menu screen

case 'E':

case 'e':

printEnd();

return 0; //will end the loop after the prototype is called

default :

cout << "Incorrect value, please enter a valid responce. ";

cout << endl;

continue; //will continue the loop at the menu screen. When "break" was inserted,

//or when "cin >> ch" was placed the program would take an extra step

//before returning to the menu screen.

}

}

}

void printSquare (){

int squareHeight, squareWidth;

cout << "Welcome to the rectangle maker! ";

cout << "To begin, enter the parameters. ";

cout << "Enter Height: ";

cin >> squareHeight;

cout << "Enter Width: ";

cin >> squareWidth;

while ( squareHeight <= 0 || squareWidth <= 0 ) //Tests to make sure the user gives appropriate values.

{

cout << "Error, both values must be greater than zero. Try Again. ";

cout << "Enter Height: ";

cin >> squareHeight;

cout << "Enter Width: ";

cin >> squareWidth;

}

for(int width=1; width <= squareHeight; width++) //This will create the top row.

{

if(width <= 1)

for(int width=1; width<=squareWidth; width++)

{

cout << "*";

}

else if(width

{

cout<< endl;

for(int width2=1; width2<=squareWidth; width2++)//This will create the middle of the rectangle.

{

if(width2==1 || width2==squareWidth) //this will make the sides of the rectangle.

cout << "*";

else

cout << " "; //This will create the empty portion.

}

}

else

{

cout<< endl;

for(int width3=1; width3<=squareWidth; width3++) //this will create the bottom row.

{

cout <<"*";

}

}

}

cout << endl;

}

void printTriangle(){

int number;

cout << "Hello! "; //Greeting

cout << "Welcome to the triangle creator. ";

cout << "To begin, enter the height of the triangle: ";

cin >> number;

int y = number;

int x = number;

while ( number <= 0) //Tests to make sure the user gives appropriate values.

{

cout << "Error, enter a number greater than zero. Try Again. ";

cout << "Enter the height of the triangle: ";

cin >> number;

}

for (int r = 1; r <= number; r++) //This will be used to create lines after the first row

{

for (int c = 1; c <= y ; c++) //this will eb out top row.

{

if (c <= x) //at start, will print the number of * until it matches the number entered by the user.

{

cout << "*";

}

}

cout << endl;

x = x-1; //allows the following row to decrease by one "*." Giving us the triangle appearance.

}

}

void printCircle()

{

int radius;

cout << "Hello! "; //Greeting

cout << "Welcome to the circle creator. ";

cout << "To begin, enter the radius of the circle: ";

cin >> radius;

while ( radius <= 0) //Tests to make sure the user gives appropriate values.

{

cout << "Error, enter a number greater than zero. Try Again. ";

cout << "Enter the radius of the circle: ";

cin >> radius;

}

int y = radius;

int x = radius;

int c=0;

const int width= radius;

const int length= radius *1.5;

for (int y=width;y >= -width;y-=2) {

for (int x=-length;x <= length;x++) {

if ((int) pth(x,y)== radius) cout << "*";

else cout << " ";

}

cout << " ";

}

cin.get();

}

void printEnd()

{

cout << "End of program... ";

}

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Database Driven Web Sites

Authors: Joline Morrison, Mike Morrison

2nd Edition

? 061906448X, 978-0619064488

More Books

Students also viewed these Databases questions

Question

1.what is the significance of Taxonomy ?

Answered: 1 week ago

Question

What are the advantages and disadvantages of leasing ?

Answered: 1 week ago

Question

Name is needed for identifying organisms ?

Answered: 1 week ago