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

Enumerated types are useful, but safety is not always guaranteed. To illustrate,

ID: 3586002 • Letter: E

Question

Enumerated types are useful, but safety is not always guaranteed. To illustrate, the following code would
compile: Color c = 10; even though there is no color corresponding to the integer 10. What happens
if you call your getCost function using such a color? To prevent this, we will add additional code to
detect and handle this type of error situation.
1. Add another enumerated type called Error that will recognize the following error types: No Error,
Unavailable Selection Error, Invalid Argument Error, and Null Pointer Error (note: a good convention
is to use all-caps with words separated by an underscore).
2. Add additional code and checks to your getCost function to check for valid input (on all the
arguments).
3. Modify the getCost method to return one of your enumerated types based on the type of error
encountered.
4. Modify your main program to check for the type of error returned and print an appropriate error
message.

------------------------------------------------------------------Partial Code-------------------------------------------------------------------

#include<stdio.h>

typedef enum { RED = 1, WHITE = 2 } Color;

typedef enum {Roses = 1, Lilies = 2, Daisies = 3} Flower;

typedef enum {Bouquet = 1, Vase = 2} Arrangement;

double getCost(Flower flower, Color color, Arrangement arr);

int main (void) {

Flower flower;

Color color;

Arrangement arr;

int cost;

  

printf(" Types of flowers ");

printf("1. Roses ");

printf("2. Lilies ");

printf("3. Daises ");

printf("Please enter the item number for your choice: ");

scanf("%d", &flower);

  

printf(" Color choices ");

printf("1. Red ");

printf("2. White ");

printf("Please enter the item number for your choice: ");

scanf("%d", &color);

  

printf(" Arrangements ");

printf("1. Bouquet ");

printf("2. Vase ");

printf("Please enter the item number for your choice: ");

scanf("%d", &arr);

cost = getCost(flower, color, arr);

printf("The total cost for the arrangement of flowers is %d", cost);

  

return 0;

}

double getCost(Flower flower, Color color, Arrangement arr){

int base_price =0;

int additional_cost = 0;  

int arrangement_cost = 0;  

int total_cost;

if(flower == Roses){

base_price = 30;

}

else if(flower == Lilies){

base_price = 20;

}

else{

base_price = 45;

}

  

if(flower == Roses && color == WHITE){

additional_cost = 10;

}

if(flower == Lilies && color == RED){

additional_cost = 5;

}

if(flower == Daisies && color == RED){

additional_cost = 5;

}

if(arr == Vase){

arrangement_cost = 10;

}

total_cost = base_price + additional_cost + arrangement_cost;

return total_cost;

}

Explanation / Answer

//changed getCost function to return error if enumerated data is wrong

//written error function to check enumerated data

//changed main function to test if error is there

//you can find my changes with keyword Added by cheggEA

#include<stdio.h>

#include<string.h>

#include<stdlib.h>

#include<ctype.h>

typedef enum { RED = 1, WHITE = 2 } Color;

typedef enum { Roses = 1, Lilies = 2, Daisies = 3 } Flower;

typedef enum { Bouquet = 1, Vase = 2 } Arrangement;

double getCost(Flower flower, Color color, Arrangement arr);

//added by CheggEA

int ERROR_ENUMERATED_VARIABLE(char type[], int value);

int main(void) {

Flower flower;

Color color;

Arrangement arr;

//added by CheggEA

int error = 1; //to indicate error

int cost;

printf(" Types of flowers ");

printf("1. Roses ");

printf("2. Lilies ");

printf("3. Daises ");

printf("Please enter the item number for your choice: ");

scanf("%d", &flower);

printf(" Color choices ");

printf("1. Red ");

printf("2. White ");

printf("Please enter the item number for your choice: ");

scanf("%d", &color);

printf(" Arrangements ");

printf("1. Bouquet ");

printf("2. Vase ");

printf("Please enter the item number for your choice: ");

scanf("%d", &arr);

cost = getCost(flower, color, arr);

//added by CheggEA to check error return from getCost function

switch (cost)

{

case -1:

printf("Error in enumerated datatype value in main: Flower ");

error = 1;

break;

case -2:

printf("Error in enumerated datatype value in main: Color ");

error = 1;

break;

case -3:

printf("Error in enumerated datatype value in main: Arrangement ");

error = 1;

break;

default:

error = 0;

}

if (!error)

printf("The total cost for the arrangement of flowers is %d", cost);

return 0;

}

//changed by Chegg EA to return -1 error if enumerated data flower has error in data, -2 if enumerated data color has error in data, if enumerated data arrangement has error in data

double getCost(Flower flower, Color color, Arrangement arr){

int base_price = 0;

int additional_cost = 0;

int arrangement_cost = 0;

int total_cost;

//added by checgEA, check all the enumerated data value for error by calling new function defined ERROR_ENUMERATED_VARIABLE

if (ERROR_ENUMERATED_VARIABLE("flower", Roses) < 0)

{

printf("Error occured in enumerated data type flower ");

return -1;

}

if (ERROR_ENUMERATED_VARIABLE("color", color) < 0)

{

printf("Error occured in enumerated data type color ");

return -2;

}

if (ERROR_ENUMERATED_VARIABLE("arrangement", arr) < 0)

{

printf("Error occured in enumerated data type arrangement ");

return -3;

}

//else everything is fine

if (flower == Roses){

base_price = 30;

}

else if (flower == Lilies){

base_price = 20;

}

else{

base_price = 45;

}

if (flower == Roses && color == WHITE){

additional_cost = 10;

}

if (flower == Lilies && color == RED){

additional_cost = 5;

}

if (flower == Daisies && color == RED){

additional_cost = 5;

}

if (arr == Vase){

arrangement_cost = 10;

}

total_cost = base_price + additional_cost + arrangement_cost;

return total_cost;

}

//Added by cheggEA ,this function returns 0 for no error, -1 for Unavailable Selection Error -2 for Invalid Argument

int ERROR_ENUMERATED_VARIABLE(char type[], int value)

{

int i = 0;

//check valid values for color

if (strcmp(type, "color") == 0)

{

switch (value)

{

//no error in enumerated data

case 1:

case 2:

return 0;

default:

if (value < 0)

return -2;

if (value > 2)

return -1;

}

}

//check valid values for flower

if (strcmp(type, "flower") == 0)

{

switch (value)

{

//no error in enumerated data

case 1:

case 2:

case 3:

return 0;

default:

if (value < 0)

return -2;

if (value > 2)

return -1;

}

}

//check valid values for Arrangement

if (strcmp(type, "arrangement") == 0)

{

switch (value)

{

//no error in enumerated data

case 1:

case 2:

return 0;

default:

if (value < 0)

return -2;

if (value > 2)

return -1;

}

}

}

----------------------------------------------

//output


Types of flowers
1. Roses
2. Lilies
3. Daises
Please enter the item number for your choice: 1

Color choices
1. Red
2. White
Please enter the item number for your choice: 10

Arrangements
1. Bouquet
2. Vase
Please enter the item number for your choice: 1
Error occured in enumerated data type color
Error in enumerated datatype value in main: Color

------------------------------------

//output


Types of flowers
1. Roses
2. Lilies
3. Daises
Please enter the item number for your choice: 1

Color choices
1. Red
2. White
Please enter the item number for your choice: 2

Arrangements
1. Bouquet
2. Vase
Please enter the item number for your choice: 2
The total cost for the arrangement of flowers is 50

----------------------------------------------------------------------

//output

Types of flowers
1. Roses
2. Lilies
3. Daises
Please enter the item number for your choice: 1

Color choices
1. Red
2. White
Please enter the item number for your choice: 10

Arrangements
1. Bouquet
2. Vase
Please enter the item number for your choice: 1
Error occured in enumerated data type color
Error in enumerated datatype value in main: Color