Tuesday, December 30, 2025

Borders for CLI apps

Drawing dbl-line border around some text, in the terminal output.

void DrawBorder(NULL)
    {
        int size = 3;
        string name = "Program Name, version 1.0", cpr = "(c) 2025", author = "R. Malik";
        string titles [size] = {"" + name, "Copyright: " + cpr, "Author: " + author};
        int rows = 80;

        cout << "\342\225\224";//upper left corner (dbl-line)

        for (int i = 0; i < rows; i++)
            {
            cout<<"\342\225\220";//horizontal (dbl-line)
            }

        cout << "\342\225\227";//upper right corner (dbl-line)
        cout << endl;

        for (int i = 0; i < size; i++)
            {
            cout << "\342\225\221" << titles[i];//vertical (dbl-line)
            double length = titles[i].length() ;

            for(int j = 0; j < (rows-length); j++)
                {
                cout << " ";
                }

            cout << "\342\225\221 "<< endl;//vertical (dbl-line)
            }

        cout << "\342\225\232";//lower left corner (dbl-line)

        for (int i = 0; i < rows; i++)
            {
            cout << "\342\225\220";//horizontal (dbl-line)
            }

        cout << "\342\225\235";//lower right corner (dbl-line)
        cout <<  "\n";
    }

Sunday, December 28, 2025

C++ Cards

A Deck of Cards 

C++ example of a deck of cards.

Output to stdout. From here you can do a shuffle, deal, whatever with the deck. Poker, BlackJack, Solitare.







//------------------------cards.cpp
#include <stdio.h>
#include <strings.h>
#include <iostream>
#include <stdlib.h>
#include <vector>
#include <algorithm>

using namespace std;
void unwrap(vector<int> &);
void shuffle(vector<int> &);
void printCards(const vector<int> &);

vector<string> suit = {" \033[1;31m\342\231\241\033[0m", " \342\231\247", " \033[1;31m\342\231\242\033[0m", " \342\231\244"};
vector<string> value = {"2 ", "3 ", "4 ", "5 ", "6 ", "7 ", "8 ", "9 ", "10 ", "J ", "Q ", "K ", "A "};

int main()
{
    int index = 0;
    //print to screen
    for (string s : suit)
        {
        for (string v : value)
            {
            cout << s << v;
            index++; 
            }
        cout  << index <<"\n";
        }

cout << "\n";                   //space
index = 0;
                      //reset

  return 0;
}//EOF

Tuesday, December 9, 2025

 Outputting file contents to the terminal

Simple fstream output

#include <iostream>
#include <string> // For std::string
#include <vector> // For std::vector (optional, but good for argument storage)
#include <errno.h>
#include <fstream>


using namespace std;

void printHelpMessage() {
    cout << "Usage: myprogram [OPTIONS]" << endl;
    cout << "Options:" << std::endl;
    cout << "  --help, -h    Display this help message" << endl;
    cout << "  --version, -v Display program version" << endl;
    cout << "  --input, -f FILENAME  Specify an input file to open." << endl;
    // Add more options and descriptions as needed
}

int main(int argc, char* argv[]) {
    // Loop through the command-line arguments, starting from argv[1]
    // argv[0] is always the program name itself
string inputFileName;
    for (int i = 1; i < argc; ++i) {
        string arg = argv[i];

        if (arg == "--help" || arg == "-h") {
            printHelpMessage();
            return 0; // Exit after displaying help
        }
        // Add more argument handling here
        else if (arg == "--version" || arg == "-v") {
            cout << "ReadFile Version 1.0" << endl;
            return 0;
        }
        else if (arg == "--input" || arg == "-f") {
            if (i + 1 < argc) { // Check if there's a next argument for the filename
                inputFileName = argv[++i];
                cout << "Reading FILE: " << inputFileName << endl <<endl;
            } else {
                cerr << "Error: --input requires a filename." << endl;
                return 1; // Indicate error
            }
        }
        else {
            cerr << "Unknown argument: " << arg << endl;
            cerr << "Use --help for usage information." << endl;
            return 1; // Indicate error
        }
    }

    // If no specific arguments are handled, or after handling them
    //cout << "Running main program logic..." << endl;
    // Your main program logic goes here
if(inputFileName == "")
    {cout << "Error: File name required.\n"; return 0;}
    else
    {
    fstream file(inputFileName, ios::in | ios::out);// | ios::app);
    if(file.is_open())
        {
        string data;
        while (getline(file,data))
            {
            cout << data << endl;
            }
        file.close();
        }
    else{cerr <<"error opening file" <<endl; return 0;}

    }
cout << "File name required.\n";
    return 0;
}

Monday, December 1, 2025

Terminal Calculator

Simple text based calculator. 

//-----------------------------------------------------------------
#include <vector>
#include <stack>
#include <iostream>
#include <errno.h>
#include <string> // For std::string
using namespace std;
//----------------------------------------------------------------
class CalculatorEngine
{
  public:
    enum binaryOperator { PLUS, MINUS, TIMES, DIVIDE, CLEAR };
    int  currentMemory ()                { return data.top(); }
    void pushOperand   (int value)       { data.push (value); }
    void doOperator    (binaryOperator);

  protected:
    stack< int, vector<int,allocator<int> > > data;
};//end class decl (CalculatorEngine)
//------------------------------------------------------------------
void CalculatorEngine::doOperator (binaryOperator theOp)
{
    float right = data.top();
    data.pop();
    float left = data.top();
    data.pop();
    switch (theOp)
    {
        case PLUS:   data.push(left + right); break;
        case MINUS:  data.push(left - right); break;
        case TIMES:  data.push(left * right); break;
        case DIVIDE: data.push(left / right); break;
    case CLEAR: data.push(left = 0);data.push(right = 0);break;
    }
}//end class inplemtation (CalculatorEngine)
//------------------------------------------------------------------
    //std::string inputFormula;
void printHelpMessage()
{
    std::cout << "Usage: calcutate [OPTIONS]\n";
    std::cout << "Options:\n";
    std::cout << "  --help, -h    Display this help message\n";
    std::cout << "  --version, -v Display version info\n";
    std::cout << "  --input FORMULA  Specify a properly formatted math formula\n";
    cout << "For more information, visit https://https://github.com/RickMalik-sys/smudges\n";
    // Add more options and descriptions as needed
}//end --help msgs
//-----------------------------------------------------------------
int main(int argc, char* argv[])
{
    // Loop through the command-line arguments, starting from argv[1]
    // argv[0] is always the program name itself
    for (int i = 1; i < argc; ++i)
    {
        std::string arg = argv[i];

        if (arg == "--help" || arg == "-h") {
            printHelpMessage();
            return 0; // Exit after displaying help
        }
        // Add more argument handling here
        else if (arg == "--version" || arg == "-v") {
            std::cout << "Version 1.0.0.0 (c)2025 RMSoftware" << std::endl;
            return 0;
        }
        else if (arg == "--input") {
            if (i + 1 < argc) { // Check if there's a next argument for the filename
                std::string inputFormula = argv[++i];
                std::cout << "Entered math formula, improperly formatted. " << inputFormula << std::endl;
            } else {
                std::cerr << "Error: --input requires a formula." << std::endl;
                return 1; // Indicate error
            }
        }
        else {
            std::cerr << "Unknown argument: " << arg << std::endl;
            std::cerr << "Use --help for usage information." << std::endl;
            return 1; // Indicate error
        }
    }//end for(loop) 
    // meatgrinder
    {
    cout << "Calculator program\n" << endl;
    cout << "Enter an expression ending with = for result. For example 2+2+= or 2+5*3/=c\nEnter c to clear. Enter q to quit.\n\n";
    float intval;
    CalculatorEngine calc;
    char c;
    char x = 'q';
    while (cin >> c)
    if(c == 'q')
        {
        fprintf(stderr, "Program complete.\n");
        exit(EXIT_FAILURE);
        }//end if    
    else{
        switch (c)
           {
            case '0': case '1': case '2': case '3': case '4':
            case '5': case '6': case '7': case '8': case '9':
                {
                cin.putback(c);
                cin >> intval;
                calc.pushOperand(intval);
                break;
                }//end cases
    case 'c': calc.doOperator(CalculatorEngine::CLEAR); break;
            case '+': calc.doOperator(CalculatorEngine::PLUS); break;
            case '-': calc.doOperator(CalculatorEngine::MINUS); break;
            case '*': calc.doOperator(CalculatorEngine::TIMES); break;
            case '/': calc.doOperator(CalculatorEngine::DIVIDE); break;
            case '=': cout << "result = " << calc.currentMemory() << endl;break;
        default:
                {
                fprintf(stderr, "Not a valid formula.\n" );
                exit(EXIT_FAILURE);
                }//end default
            }//end switch
        }//end else
    }//end meatgrinder
}//end main
//--------------------------------------------------------------