Variable in system()

I have a bit of code which uses system() the command it gives has parameter1 which changes all the time. So I'm trying to put in a variable. My question, is this possible if so how do I do this?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
#include <cstdlib>
#include <iostream>

using namespace std;

int main()
{
    string varPar1 = "qwerty";
    system("start \"\" \"C:\\map1\\map2\\map3\" \"executable.exe\" \"parameter1\" \"parameter2\"");
    
    //tested
    system("start \"\" \"C:\\map1\\map2\\map3\" \"executable.exe\"  varPar1  \"parameter2\"");

}
Last edited on
First of all, using system() is highly discouraged.

Now, you need to build your string before calling system. For example:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#include <cstdlib>
#include <iostream>
#include <string>

std::string build_string(std::string param)
{
    return "echo " + param + "!";
}

int main()
{
    std::string par = "Hello";
    std::string line = build_string(par);
    std::system(line.c_str());
}
Hello!
Last edited on
Ty, its works great, maybe you already noticed im just a beginner. So i have a quesotion about your answer. What is
std::
doing infront of every thing. What should i search for if i want to find a tutorial about it?

BTW. Yes, i know about the system() but this program is just for me.
It is a namespace where all names from standard library are stored.
http://www.cplusplus.com/forum/beginner/61121/
Also read: http://stackoverflow.com/questions/1452721/why-is-using-namespace-std-considered-bad-practice

Read more on namespaces in C++ and scope resolution operators: http://www.cplusplus.com/doc/tutorial/namespaces/
Topic archived. No new replies allowed.