noob like

so i need to insert the options into a system command, anyone know a way to do this that will work?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#include <iostream>
#include <string>
using namespace std;


int main()
{
        cout << "Options:" << endl << endl;
        cout << "1. Create new SoftAP" << endl;
        cout << "2. Rename the current SoftAP" << endl;
        cout << "3. Reset the password of the current SoftAP" << endl;
        cout << "4. Turn on the SoftAP" << endl;
        cout << "5. Turn off the SoftAP" << endl << endl;
    cout << "Please enter the number of your choice below.\n>";

            string option1;
cin shit blah blah you get the point;
            system("netsh blah blah blah" option1);

    return 0;
}
You mean call netsh via system() with some dynamic string?
well i messed that up, i want someone to enter option one and it filles the system command with the proper netsh script
Is just using a switch/case or if/else not sufficient? I'm not sure exactly how this would be something that requires special treatment.
Maybe
1
2
3
std::string op = "script";
	std::string com = "netsh exec " + op;
	system(com.c_str());
well ive found that you cant use variables with system() thats only problem with this. just try to compile something like this

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#include <iostream>
#include <string>
#include <windows.h>
using namespace std;


int main()
{
       string test = "cls";
       system(test);

    system("pause");
    return 0;
}

Use the c_str() member function like in my example.
Last edited on
im stuck again, this wont compile even if i take out the "name1" piece.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
#include <iostream>
#include <string>
#include <windows.h>
using namespace std;


int main()
{
    string input;
        cout << "Options:" << endl << endl;
        cout << "1. Create new SoftAP" << endl;
        cout << "2. Rename the current SoftAP" << endl;
        cout << "3. Reset the password of the current SoftAP" << endl;
        cout << "4. Turn on the SoftAP" << endl;
        cout << "5. Turn off the SoftAP" << endl << endl;
    cout << "Please enter the number of your choice below.\n>";
cin >> input;

    if(input=="1")
    {
       cout << "Enter a name for the SoftAP\n\n>" << endl << endl;
        string name1;
        cin >> name1;
       std::string op = "script";
       std::string com = "netsh exec " + op;
	   system(com.c_str("netsh wlan set hostednetwork mode=allow ssid="name1" key=beloitwi2394 keyUsage=persistent"));

    }


    return 0;
}
Last edited on
oh shoot. nvm i get what you mean now
ok heres exactly what i need to happen and i confused the hell out of myself with what you had, adapt this?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
#include <iostream>
#include <string>
#include <windows.h>
using namespace std;


int main()
{
    string input;
        cout << "Options:" << endl << endl;
        cout << "1. Create new SoftAP" << endl;
        cout << "2. Rename the current SoftAP" << endl;
        cout << "3. Reset the password of the current SoftAP" << endl;
        cout << "4. Turn on the SoftAP" << endl;
        cout << "5. Turn off the SoftAP" << endl << endl;
    cout << "Please enter the number of your choice below.\n>";
cin >> input;

    if(input=="1")
    {
       cout << "Enter a name for the SoftAP\n\n>" << endl << endl;
        string name1;
        cin >> name1;
        string netsh = "netsh wlan set hostednetwork mode=allow ssid=";
        string netsh2 = " key=password keyUsage=persistent";
       system(netsh+name1+netsh2);

    }


    return 0;
}
or something like this? idk im a noob

1
2
3
4
5
6
7
8
9
10
    if(input=="1")
    {
       cout << "Enter a name for the SoftAP\n\n>" << endl << endl;
        string name1;
        cin >> name1;
        string netsh = "netsh wlan set hostednetwork mode=allow ssid=";
        string netsh2 = " key=beloitwi2394 keyUsage=persistent";
       system(netsh.c_str&&name1.c_str&&netsh2.c_str());

    }
Your first post (using +) was closer to what you need. After using the + to add all the strings together, call the c_str() method. e.g. (str1+str2).c_str()
system(netsh+name1+netsh2).c_str();


error: cannot convert 'std::basic_string<char>' to 'const char*' for argument '1' to 'int system(const char*)'|
You need to pass the result of c_str() to the system() function, not the other way around.
could i have an example? sorry this is a new concept
Topic archived. No new replies allowed.