In: Computer Science
Not sure where to begin on this c++ assignment. The instructions are to enclose the current contents of the print_output function into a try block that throws two different exceptions based on which line catches the bad input (shape or color). You will then print an appropriate message based on the type of exception that was thrown.
this is what I have so far:
#include <stdlib.h>
#include <time.h>
#include <stdio.h>
#include <iostream>
namespace color {
enum color{
red =
0xFF0000,
green = 0x00FF00,
blue = 0x0000FF,
white = 0x000000
};
}
namespace shape {
enum shape{
circle = 0,
triangle = 1,
square = 2
};
}
void print_object(int color, int shape) {
int r = color >> 16 & 0xFF;
int g = (color >> 8) & 0xFF;
int b = color & 0xFF;
color::color estimated_color = color::white;
if ( (r > g) and (r > b) ) {
estimated_color = color::red;
}
else if ( (g > r) and (g > b) ) {
estimated_color =
color::green;
}
else if ( (b > r) and (b> g) ) {
estimated_color =
color::blue;
}
std::cout<<"The object is a ";
switch ( estimated_color ) {
case color::red: std::cout<<"red "; break;
case color::green: std::cout<<"green ";
break;
case color::blue: std::cout<<"blue ";
break;
default: exit(1);
}
switch ( shape ) {
case shape::triangle:
std::cout<<"triangle";break;
case shape::square: std::cout<<"square ";
break;
case shape::circle: std::cout<<"circle ";
break;
default: exit(1);
}
std::cout<<std::endl;
}
int main() {
int random_color = 0;
int random_shape = 0;
srand(time(NULL));
print_object(color::red, shape::circle);
print_object(color::green, shape::square);
print_object(color::blue, shape::square);
for (int i=0; i < 10; i++) {
random_color = rand() %
0xFFFFFF;
random_shape = rand() % 3;
print_object(random_color,
random_shape);
}
return (0);
}
Exception Handling:
Exceptions are run-time anomalies or abnormal conditions that a program encounters during its execution.
There are two types of exceptions: a)Synchronous, b)Asynchronous
C++ provides following specialized keywords and blocks to handle these exceptions.
try: represents a block of code that can throw an exception.
catch: represents a block of code that is executed when a particular exception is thrown.
throw: Used to throw an exception. Also used to list the exceptions that a function throws, but doesn’t handle itself.
// Modified source code to handle exceptions raised with bad input of color and shape
#include <stdlib.h>
#include <time.h>
#include <stdio.h>
#include <iostream>
namespace color {
enum color{
red = 0xFF0000,
green = 0x00FF00,
blue = 0x0000FF,
white = 0x000000
};
}
namespace shape {
enum shape{
circle = 0,
triangle = 1,
square = 2
};
}
void print_object(int color, int shape) {
// try block
try
{
int r = color >> 16 & 0xFF;
int g = (color >> 8) & 0xFF;
int b = color & 0xFF;
color::color estimated_color = color::white;
if ( (r > g) and (r >
b) ) {
estimated_color = color::red;
}
else if ( (g > r) and (g > b) ) {
estimated_color = color::green;
}
else if ( (b > r) and (b> g) ) {
estimated_color = color::blue;
}
std::cout<<"The object is a ";
switch ( estimated_color ) {
case color::red: std::cout<<"red "; break;
case color::green: std::cout<<"green "; break;
case color::blue: std::cout<<"blue "; break;
default: throw 'c'; // throws Exception if color is not blue, red
or green
//default: exit(1);
}
switch ( shape ) {
case shape::triangle: std::cout<<"triangle";break;
case shape::square: std::cout<<"square "; break;
case shape::circle: std::cout<<"circle "; break;
default: throw 's'; // throws Exception if shape is not triangle,
square or circle.
//default: exit(1);
}
std::cout<<std::endl;
}
//catch block
catch(char ch) // Catches
exception
{
if (ch=='c') // displays color exception
std::cout<<"Bad Input: Color
exception"<<std::endl;
else
if(ch=='s') // displays shape exception
std::cout<<"Bad Input: Shape
exception"<<std::endl;
}
}
int main() {
int random_color = 0;
int random_shape = 0;
srand(time(NULL));
print_object(color::red, shape::circle);
print_object(color::green, shape::square);
print_object(color::blue, shape::square);
for (int i=0; i < 10; i++) {
random_color = rand() % 0xFFFFFF;
random_shape = rand() % 3;
print_object(random_color, random_shape);
}
return (0);
}
You can check this program by
changing the statement in main() function
random_shape = rand() % 3; to
random_shape = rand() % 5; i.e change number 3 to more than 3 .
It raises shape exception.
Similarly, you can also change estimated_color value and can raise color exception.