#include <stdio.h>
#include <string.h>
#include <stdlib.h>

const int SUCCESS = 0;
const int QUIT = 1;
const int ERR = 2;
const int NEGS = 3;

long twoPower(int expo)
{
    unsigned long result = 1;
    for (int i = 1; i <= expo; i++)
    {
        result *= 2;
    }
    return result;
}

int checkIfDigitsOnly(char *str)
{
    if (*str == '-')
    {
        return NEGS;
    }
    while (*str != '\0')
    {
        if ((*str >= '0' && *str <= '9'))
        {
            str++;
            continue;
        }
        else
        {
            if (*str == 'q' && *(str + 1) == '\0')
            {
                return QUIT;
            }
            else
            {
                return ERR;
            }
        }
    }
    return SUCCESS;
}

int getInput(char *inputBuffer, int bufferLength)
{
    printf("enter your exponent (max %d character): ", bufferLength - 1);
    fgets(inputBuffer, bufferLength, stdin);
    if (inputBuffer[strlen(inputBuffer) - 1] != '\n')
    {
        int dropped = 0;
        while (fgetc(stdin) != '\n')
        {
            dropped++;
        }

        if (dropped > 0)
        {
            printf("the input is over the limit by %d characters. try again!\n", dropped);
            return ERR;
        }
    }
    else
    {
        inputBuffer[strlen(inputBuffer) - 1] = '\0';
    }
    int check = checkIfDigitsOnly(inputBuffer);
    if (check == ERR)
    {
        printf("the input contains non-digits. try again!\n");
        return ERR;
    }
    else if (check == QUIT)
    {
        printf("\n \t Bye :) \n");
        return QUIT;
    }
    else if (check == NEGS)
    {
        printf("only non-negative numbers are allowed!\n");
        return NEGS;
    }
    else
    {
        int out = atoi(inputBuffer);
        if (out > 63)
        {
            printf("Not allowed value over 63 \n");
            return ERR;
        }
        printf("2^%d = %lu \n", out, twoPower(out));
        return SUCCESS;
    }
}

int main()
{
    char input[3] = "";
    int status = SUCCESS;
    while (status != QUIT)
    {
        status = getInput(input, 3);
        printf("\n");
    }
}