Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

PLEASE READ THE QUESTION CAREFULLY. I NEED THE FULL TASK. IF YOU ARE NOT SURE WH

ID: 3590545 • Letter: P

Question

PLEASE READ THE QUESTION CAREFULLY. I NEED THE FULL TASK. IF YOU ARE NOT SURE WHAT TO DO, PLEASE SKIP MY QUESTION. I WILL GIVE NEGATIVE RATING FOR WRONG OR INCOMPLETE ANSWER.

THANK YOU.

QUESTION:

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 <pid>".

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

// CODE STARTS HERE

#include<iostream>

#include<cmath>

#include<math.h>

using namespace std;

//These 4 functions are from the given code

void Task1()

{

int width, height;

cout<< "Task 1: Rectangular Frame"<<endl;

cout<< "Enter width: ";

cin>>width;

cout<< "Enter height: ";

cin>>height;

cout<<endl;

for(int i = 0; i < height; ++i)

{

for(int j = 0; j < width; ++j)

{

if(i == 0 || j == 0 || i == height - 1 || j == width - 1)

{

cout << "*";

}

else{

cout << " ";

}

}

cout << endl;

}

cout<<endl;

}

void Task2()

{

int length;

cout<< "Task 2 North to West"<<endl;

cout<< "Enter length: ";

cin>>length;

cout<<endl;

for(int i=length; i >= 1; i--)

{

for(int j=1; j<=i; j++)

{

cout<< "*";

}

cout<< " ";

}

cout<<endl;

}

void Task3()

{

int length;

cout<< "Task 3: South to East"<<endl;

cout<< "Enter length: ";

cin>>length;

cout<<endl;

for(int i=length; i >= 1; i--)

{

for(int j=1; j <=i; j++)

{

cout<< " ";

}

for(int k=length;k>=i;k--)

{

cout << "*";

}

cout<<" ";

}

cout<<endl;

}

void Task4()

{

cout << "Task 4: Circle"<<endl;

float r;

cout<<"Enter radius: " ;

cin>>r;

float pr = 2; // pixel ratio

for (int i = -r; i <= r; i++)

{

for (int j = -r; j <= r; j++)

{

float d = ((i*pr)/r)*((i*pr)/r) + (j/r)*(j/r);

if (d >0.95 && d<1.08)

{

cout << "*";

}

else

{

cout << " ";

}

}

cout << endl;

}

}

//this is menu function

//it will take choice from user

//based on choice user entered, corresponding function will called

void Menu()

{

char choice;

//iterate till user enter E to end

do{

//getting choice from user

cout<<"Enter Your choice: ";

cout<<"(R)ectange (T)riangle (C)ircle (E)end ";

cin>>choice;

//if choice is R

if(choice=='R')

{

Task1();

}

//if choice is T

else if(choice=='T')

{

Task2();

Task3();

}

//if choice is C

else if(choice=='C')

{

Task4();

}

  

}while(choice!='E');//terminate when E entered

}

//Main method

int main()

{

//calling Menu function

Menu();

}

Explanation / Answer

On typing a non-numeric value at input prompt which is not part of {R,T,C.E} , the menu is displayed again with the request to choose another input.

On typing a numeric input , menu is displayed again even for large input values (e.g., 999999999999999999)

The question talks about input reset in Task 0 code but there is no Task 0 so I will proceed with the input validation for Task 1.

void Task1()
{
int width, height;
cout<< "Task 1: Rectangular Frame"<<endl;
cout<< "Enter width: ";
cin>>width;

while(1)
{
if(cin.fail())
{
cin.clear();

  cin.ignore(10000, ' ');
cout<< "Enter width: " <<endl;
cin>>width;
}
if(!cin.fail())
break;
}
cout<< "Enter height: ";
cin>>height;
cout<<endl;

while(1)
{
if(cin.fail())
{
cin.clear();

  cin.ignore(10000, ' ');
cout<< "Enter height: " <<endl;
cin>>height;
}
if(!cin.fail())
break;
}

for(int i = 0; i < height; ++i)
{
for(int j = 0; j < width; ++j)
{
if(i == 0 || j == 0 || i == height - 1 || j == width - 1)
{
cout << "*";
}
else{
cout << " ";
}
}
cout << endl;
}
cout<<endl;
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote