Coffee Enums

Coffee Enums

Create a program that lets a user select a coffee size. The program should prompt the user to select a coffee size (small, medium, or large) and then display the selected size.

Select coffee size: (0 for SMALL, 1 for MEDIUM, 2 for LARGE): 1
You selected MEDIUM coffee.

Use an enum to represent the coffee sizes. Include a CoffeeSize getCoffeeSize() function to get user input and a void displayCoffeeSize(CoffeeSize size) function to display the selected size.

Answer

#include <iostream>

// Define enum for coffee sizes
enum CoffeeSize {
    SMALL,
    MEDIUM,
    LARGE
};

// Function to get coffee size from user input
CoffeeSize getCoffeeSize() {
    int choice;
    std::cout << "Select coffee size: (0 for SMALL, 1 for MEDIUM, 2 for LARGE): ";
    std::cin >> choice;
    
    switch (choice) {
        case 0: return SMALL;
        case 1: return MEDIUM;
        case 2: return LARGE;
        default: 
            std::cout << "Invalid choice, defaulting to SMALL." << std::endl;
            return SMALL;
    }
}

// Function to display selected coffee size
void displayCoffeeSize(CoffeeSize size) {
    switch (size) {
        case SMALL: 
            std::cout << "You selected SMALL coffee." << std::endl;
            break;
        case MEDIUM: 
            std::cout << "You selected MEDIUM coffee." << std::endl;
            break;
        case LARGE: 
            std::cout << "You selected LARGE coffee." << std::endl;
            break;
        default:
            std::cout << "Invalid size." << std::endl;
            break;
    }
}

int main() {
    // Get coffee size from user input
    CoffeeSize size = getCoffeeSize();

    // Display the selected coffee size
    displayCoffeeSize(size);

    return 0;
}
Questions