D
D
DimaIs2017-03-02 17:02:38
Programming
DimaIs, 2017-03-02 17:02:38

How to insert string value as function argument?

Greetings to all, there is a need to insert a string value as a function argument, what should I do? Suppose there is a code with a system function that received an argument in the form of a command to change the color (1 character - console, 2 - text).

#include <iostream>
#include <cstdlib>
using namespace std;

int main()
{
    system("color FC");
    return 0;
}

So, how can I pass the value of the string to this function, and not enter it manually? Thanks in advance

Answer the question

In order to leave comments, you need to log in

2 answer(s)
M
Mercury13, 2017-03-02
@DimaIs

1. Collect the body of the command in a string variable or string buffer, escaping the parameters if necessary.
2. If it's a wrapped string, convert it to const char* and off you go.

std::string command;
char data1 = 'F';
char data2 = 'C';
command = std::string("color ") + data1 + data2;
system(command.c_str());

S
Spetros, 2017-03-02
@Spetros

Yes, it's very simple - "pass the string value to this function" using a new string variable.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question