UART Baud Rate Validation

#include <iostream>

class UARTDriver {
private:
    int baud_rate;
public:
    // TODO: This member must not be publicly writable.
    // TODO: Only allow discrete hardware-supported baud rates.

    UARTDriver():baud_rate(9600) {}

    // TODO: Add a validating setter.
    void setter(int br) {
        if (br==9600 || br==19200 || br==115200) baud_rate = br;
    }
    // TODO: Add read-only access to the current baud rate.
    int getter() {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.setter(req);

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

    return 0;
}

Solving Approach

 

 

 

 

 

Upvote
Downvote
Loading...

Input

4 115200 500 19200 -1

Expected Output

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