UART Write Overloading

#include <iostream>
#include <string>
#include <cstdint>
#include <cstddef>

using namespace std;

void write(uint8_t _byte)
{
 cout << static_cast<int>(_byte) << endl;
}

void write(const string& _string)
{
 cout << _string << endl;
}

void write(const uint8_t buffer[], size_t n)
{
    constexpr std::size_t MAX = 100;
    if ( 0 >= n && n > MAX) return;
    {
        for (size_t i = 0; i < n; i++)
        {
            cout << static_cast<int>(buffer[i]) << " ";
        }
    }

}

int main() {
    int mode;
    cin >> mode;

    if (mode == 1) {
        unsigned int byteValue;
        cin >> byteValue;
        write(static_cast<uint8_t>(byteValue));
    }
    else if (mode == 2) {
        string s;
        cin >> s;
        write(s);
    }
    else if (mode == 3) {
        int n;
        cin >> n;

        uint8_t buffer[100];
        for (int i = 0; i < n; i++) {
            unsigned int temp;
            cin >> temp;
            buffer[i] = static_cast<uint8_t>(temp);
        }
        write(buffer, static_cast<size_t>(n));
    }

    return 0;
}

Solving Approach

 

 

 

 

Upvote
Downvote
Loading...

Input

1 0

Expected Output

0