M
M
Moolzv Rivers2019-08-03 22:18:10
Qt
Moolzv Rivers, 2019-08-03 22:18:10

How to return a char* string by passing variables to it?

Hello! I faced such a problem, let's start from the beginning:
I am creating a program that will generate the initial code of bots for VK using PHP, Python.
So: what problem I faced: when creating a file, I need to write there the code of a certain bot with a certain format. For example, I chose - 1. Create a bot for a group 2. Bot in Python
Next, I have a function into which it is passed what the bot is created for, and with what extension, here is the code:

char* getCode(QString mode, QString script) {
    // <...> Deleted
    else if(mode == "b" && script == "py") {
        return
                "import vk_api\n"
                "import requests\n"
                "session = requests.Session()\n"
                "login, password = '%s', '%s'"               // Problem
                "";
    }
// Если выбран бот для Группы( g ) и на php ( Там выше QString переменные в коде, но я их суда не добавил )
    else if (mode == "g" && script == "php") {
        return  "<?php\n"
                "\n"
                "// Thanks for using BotHelper\n"
                "// Author: Danil Romanov (vk: *******) \n"
                "\n"
                "include \"vk_api.php\";\n"
                "const VK_KEY = \"%s\";\n"                 // Problem \ Проблема
                "const ACCESS_KEY = \"%s\";  \n"       // Problem \ Проблема
                "const VERSION = \"5.81\";\n"
                "$vk = new vk_api(VK_KEY, VERSION); \n"
                "$data = json_decode(file_get_contents('php://input'))\n"
                "if (data->type == \"confirmation\") {\n"
                "   exit(ACCESS_KEY);\n"
                "}\n"
                "$vk->sendOK();\n"
                "// Enter this your code . . .";
    }
    else {
        QMessageBox::information(0, "Error", "Ошибка: неправильно переданы переменные в функцию\nПожалуйста, сообщите создателю программы об ошибке\nВК: *****");
    }
}

Don't pay attention, I deleted it at the beginning because I haven't written the code yet. So - as you can see, the code for the bots is almost ready, but here's the problem (I marked it with a comment), I need to transfer the text from the fields to the return string.
I tried to do like this:
// Если выбран бот на Питоне(py) и для беседы(b) ( Там выше QString переменные в коде, но я их суда не добавил )
else if(mode == "b" && script == "py") {
        return
                "import vk_api\n"
                "import requests\n"
                "session = requests.Session()\n"
                "login, password = '%s', '%s'", ui->lineEdit_7->text()  // Не видит

But what are we talking about, he does not even see the prefix ui.
I hope you understand the essence of my problem. I tried to chew and show the code as simply as possible.
Even shorter: I need to return a line with code and pass certain values ​​​​from LineEdit to it. Codes above.
Help, the program turned out beautiful, useful, and suitable. I don't want to quit at such a stupid moment.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Armenian Radio, 2019-08-03
@gbg

This is how it's done

QString i;           // current file's number
QString total;       // number of files to process
QString fileName;    // current file's name

QString status = QString("Processing file %1 of %2: %3")
                .arg(i).arg(total).arg(fileName);

Given that you have a bunch of strings, I recommend using a multi-line literal:
const auto text=QString(R"(
My program is %1
it's written by %2
for %3
)").arg("good","nice gay","fun and profit");

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question