0

Here is part of my code I want to make the title move up and down independently of other menu aspects, but it really doesn't work.

main.cpp

#include"Menu.h"

int main()
{
    Menu menu;

    while (menu.get_choice() != 1 && menu.get_choice() != 3)
    {
        menu.setup();
        menu.input();
        menu.check_input();
    }

    return 0;
}

Menu.cpp

#include "Menu.h"

void Menu::title_up_down()
{
    char previous_title[11][36]
    {
        {"                                   "},
        {"                                   "},
        {"                                   "},
        {"                                   "},
        {"                                   "},
        {"                                   "},
        {"                                   "},
        {"                                   "},
        {"                                   "},
        {"                                   "},
        {"                                   "}
    };
    for (int k = 0; ;k++)
    {
        for (int l = 0; l < 2; l++)
        {
            if (k % 2 == 0)
                go_to_XY(0, 0);
            else if (k % 2 == 1)
                go_to_XY(0, 1);
            for (int i = 0; i < 11; i++)
            {
                std::cout << '\n' << std::setw(w2);
                for (int j = 0; j < 36; j++)
                {
                    if (l == 0)
                        std::cout << title[i][j];
                    else if (l == 1)
                        std::cout << previous_title[i][j];
                }
            }
            if (l == 0)
                Sleep(1000);
        }
    }
}

void Menu::setup()
{
    system("cls");
    draw_title();
    std::thread t1(&title_up_down(), this);
    std::cout << "\n\n\n";
    std::cout << std::setw(w1 - 1) << " ---------------\n";
    std::cout << std::setw(w1) << "|  1. New Game  |\n";
    std::cout << std::setw(w1 - 1) << " ---------------\n";
    std::cout << std::setw(w1 + 1) << " -------------------\n";
    std::cout << std::setw(w1 + 2) << "|   2. Description  |\n";
    std::cout << std::setw(w1 + 1) << " -------------------\n";
    std::cout << std::setw(w1) << "   -----------------\n";
    std::cout << std::setw(w1 + 1) << "|   3. Exit Game  |\n";
    std::cout << std::setw(w1) << "  -----------------\n";
    std::cout << "Choose your option: ";
}

How do I put a method of a class inside of a thread? I tried to do it like it is above and also inside of a setup function but it still doesn't work no matter what.

Azazo8
  • 1
  • 1
  • whats the meaning of "not work" ? – 463035818_is_not_an_ai Aug 21 '23 at 12:43
  • 1
    At a quick glance it seems like you want something like ncurses or similar, together with an event-driven system. – Some programmer dude Aug 21 '23 at 12:43
  • This seems to be a bit unusual practice problem from a C++ textbook that attempts to teach how to correctly use threads in C++. Are you sure you didn't misunderstand something about what you need to do, for this practice problem? – Sam Varshavchik Aug 21 '23 at 12:44
  • Have you considered just using a state machine rather than a thread? – Jesper Juhl Aug 21 '23 at 12:44
  • 2
    There is no reason to use threads here. You'd need to coordinate usage with the one resource, which means using a mutex, which means no benefit to threading and lots of downsides. – Eljay Aug 21 '23 at 12:44
  • I'm trying to make a simple implementation of papers please game in a console so no it's not a practice problem just part of my project. I'm a bit newbie just 2 monts of learning – Azazo8 Aug 21 '23 at 12:48
  • Does this answer your question? [How to execute a class member function in a separate thread using C++11 thread class?](https://stackoverflow.com/questions/15734598/how-to-execute-a-class-member-function-in-a-separate-thread-using-c11-thread-c) – Zig Razor Aug 21 '23 at 12:48
  • the error is: "Error: Illegal operation on a bound member function expression" – Azazo8 Aug 21 '23 at 12:49
  • Minor point: if the test in `if (k % 2 == 0)` fails, you know that `k % 2` is 1, so there's no need to test it again. You can change `else if (k % 2 == 1)` to `else`. – Pete Becker Aug 21 '23 at 12:52
  • I don't really know what is state machine. I googled how to make my loop independent from rest of the code and it showed me threads – Azazo8 Aug 21 '23 at 12:53
  • 1
    The correct syntax is `std::thread t1(&Menu::title_up_down, this);`. – Pete Becker Aug 21 '23 at 12:53
  • @PeteBecker It actually compiles when I corrected the syntax but after running and getting to the point where the thread is implemented I get a Debug Error stating abort() has been called – Azazo8 Aug 21 '23 at 12:58
  • `t1` gets destroyed at the end of `Menu::setup` - if the thread has not been `join`ed at that point the destructor calls `std::terminate` – UnholySheep Aug 21 '23 at 13:04
  • @UnholySheep Yes I forgot to add t1.join(), thank all of you guys it's working now – Azazo8 Aug 21 '23 at 13:06
  • use c++20 `std::jthread` for auto join and lambda wrap your method to save a few more bytes: `std::jthread t1{ [this]{title_up_down();} };` in your case, this is more readable. `jthread` has support for `std::stop_token` too; you can send a stop request to it to break an event loop, but that requires more work. – Red.Wave Aug 21 '23 at 17:14

0 Answers0