PLEASE READ THE QUESTION CAREFULLY. I NEED THE FULL TASK. IF YOU ARE NOT SURE WH
ID: 3590800 • 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.
EXPLANATION:
We wish to validate the input for types as well as values. 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.
QUESTION:
Identify places in your 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
#include
#include
using namespace std;
//These 4 functions are from the given code
void Task1()
{
int width, height;
cout<< "Task 1: Rectangular Frame"<
cout<< "Enter width: ";
cin>>width;
cout<< "Enter height: ";
cin>>height;
cout<
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<
}
void Task2()
{
int length;
cout<< "Task 2 North to West"<
cout<< "Enter length: ";
cin>>length;
cout<
for(int i=length; i >= 1; i--)
{
for(int j=1; j<=i; j++)
{
cout<< "*";
}
cout<< " ";
}
cout<
}
void Task3()
{
int length;
cout<< "Task 3: South to East"<
cout<< "Enter length: ";
cin>>length;
cout<
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<
}
void Task4()
{
cout << "Task 4: Circle"<
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
#include<iostream>
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;
while (width <= 0)
{
cout << " Please Enter valid width: ";
cin >> width;
}
cout << "Enter height: ";
cin >> height;
while (height <= 0)
{
cout << " Please Enter valid height: ";
cin >> height;
}
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;
}
}
void Task2()
{
int length;
cout << "Task 2 North to West" << endl;
cout << "Enter length: ";
cin >> length;
while (length <= 0)
{
cout << " Please Enter valid length: ";
cin >> length;
}
for (int i = length; i >= 1; i--)
{
for (int j = 1; j <= i; j++)
{
cout << "*";
}
cout << " ";
}
}
void Task3()
{
int length;
cout << "Task 3: South to East" << endl;
cout << "Enter length: ";
cin >> length;
while (length <= 0)
{
cout << " Please Enter valid length: ";
cin >> length;
}
for (int i = length; i >= 1; i--)
{
for (int j = 1; j <= i; j++)
{
cout << " ";
}
for (int k = length; k >= i; k--)
{
cout << "*";
}
cout << " ";
}
}
void Task4()
{
cout << "Task 4: Circle" << endl;
float r;
cout << "Enter radius: ";
cin >> r;
while (r <= 0)
{
cout << " Please Enter valid 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();
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.