Akshit-Chilkoti / Trigonometry

Trigonometry based height and distance calculator.
MIT License
1 stars 0 forks source link

Suggestion for Nicer Text! #1

Open Brahvim opened 1 year ago

Brahvim commented 1 year ago

Hello there! I suggest you to have a look at the following program. Run it, read its code, and see how you could replicate the same, "nice, readable" stdout output!

Also, please do not use numbers to ask users for navigating menus! You might want to make use of actual words, and check if they are equal to a string without caring about the case. Put the entire query into a function so you can repeat it if the user enters something wrong. Also, do not perform checks on the input in the same function that also gives the user these options! Use another function for it. That way, you will be able to repeat certain tasks easily.

You might want to make functions to, say, print lists, simple lines, et cetera, then use those throughout the program along with separate functions to check the user's input. This will make things easier! Using function pointers here will give you extra convenience.

Feel free to ask me for example code doing this stuff!

#include <iostream>
#include <thread>
#include <chrono>

#pragma region // Header definitions!
#define ORIGINAL_WRITING_DELAY 50;

template <typename T>
void print(T);

void sleep(long);
void write(std::string);
#pragma endregion

namespace Speed {
    // This `static` limits these variables to this
    // "translation unit" (`.h` and `.cpp` combination.)

    /* static */ long LETTER, COMMA, FULLSTOP, EXCLAIM;

    void reset()
    {
        LETTER = ORIGINAL_WRITING_DELAY;
        COMMA = 4 * LETTER;
        FULLSTOP = 10 * LETTER;
        EXCLAIM = 6 * LETTER;
    }

    void setDelay(const long p_to)
    {
        LETTER = p_to;
        COMMA = p_to * 4.0f;
        FULLSTOP = p_to * 10.0f;
        EXCLAIM = p_to * 6.0f;
    }

}

int main(const int p_argCount, char const *p_arrArgValues[]) {
    Speed::reset();
    printf("Yo, I'm here simply waiting for you to press Enter.");
    std::cin.ignore();
    write("Hello, World!\a");
}

template <typename T>
void print(T p_toOutput) {
    std::cout << p_toOutput << '\n';
}

void sleep(const long p_time) {
    std::this_thread::sleep_for(std::chrono::milliseconds(p_time));
}

void write(const std::string p_str) {
    char c;
    const unsigned int length = p_str.length();
    for (unsigned int i = 0; i < length; i++) {
        using namespace std;

        c = p_str.at(i);
        cout << c;
        cout.flush();

        using namespace Speed;

        // Sleep for some time, based on the character encountered:
        switch (c) {
        case ',':
            sleep(COMMA);
            break;
        case '.':
            sleep(FULLSTOP);
            break;
        case '!':
            sleep(EXCLAIM);
            break;
        default:
            sleep(LETTER);
        }
    }

    std::cout << '\n';
}
Brahvim commented 1 year ago

In fact, check out this repository I built over the last ~20+ hours, just for you!

Have a look at Main.cpp and perhaps also Main.hpp. You'll get an idea on how code should be organized for such programs. Putting all kinds of printing and input checking into separate functions of their own is very helpful to organize the code into loops.

Also, the program uses an extremely simple algorithm I wrote to do some string matching magic. Check it out on your nearest GNU-Linux machine!

Akshit-Chilkoti commented 1 year ago

Thanks a lot for ur suggestion but ... uh I did not understood the code i mean i am not sooo much good at c++ :( . all this went above my head :( all that i know in c++ i have used it in this prj so i know only this much in c++