UART Baud Rate Validation

#include <iostream>

class UARTDriver {
private:
    int baud_rate;
public:
    // TODO: Only allow discrete hardware-supported baud rates.
    UARTDriver() : baud_rate(9600) {}

    // TODO: Add a validating setter.
    void set_baudRate(int val) {
        if(val == 9600 || val == 19200 || val == 115200) {
            baud_rate = val;
        }
    }
    // TODO: Add read-only access to the current baud rate.
    int get_baudRate() {
        return baud_rate;
    }
};

int main() {
    int N;
    if (!(std::cin >> N)) return 0;

    UARTDriver driver;

    for (int i = 0; i < N; ++i) {
        int req;
        std::cin >> req;

        // TODO: Replace direct access with a validating API.
        driver.set_baudRate(req);

        std::cout << "Active: " << driver.get_baudRate() << std::endl;
    }

    return 0;
}
Upvote
Downvote
Loading...

Input

4 115200 500 19200 -1

Expected Output

Active: 115200 Active: 115200 Active: 19200 Active: 19200