| kingpulse (5) | |
|
Okay so i'm trying to get this c++ program to run commands on cmd. Anyway I have got it working though I can't seem to figure out how to use char's in printing to the system. I.e #include <iostream> using namespace std; #define NL '\n' int main() { char lol[50]; cout << "Please enter what you want to enter into cmd"; cout << NL; cin >> lol; system("net send" lol) } I seem to get an error with adding a second part to the system part. I can have it by itself I.e system(lol); That would work fine but I can't have two for some reason... Please help! Thanks Kingpulse | |
|
|
|
| Moschops (5981) | |
|
In C++, you cannot concatenate (i.e. join together) two char arrays by simply putting them next to each other. You have chosen to use C style arrays of char instead of proper C++ strings, which is making things difficult for yourself, but not too difficult. You can concatenate C strings (char arrays) with the strcat function. I recommend you switch to C++ strings, which are far easier to work with. | |
|
|
|
| Chervil (1206) | |
|
You need a single string. So use strcat() to join them. http://www.cplusplus.com/reference/cstring/strcat/ Alternatively use C++ strings, such as system(text.c_str()); where text is a c++ string.
| |
|
|
|
| kingpulse (5) | |
|
Okay thanks :D , but do you think you could give me an example as to how its used? I'm new to c++ and it would be really helpful. Thanks Kingpulse | |
|
|
|
| Aramil of Elixia (772) | |||
note that system is evil and should not be used but since ur new...
| |||
|
|
|||
| Chervil (1206) | |||||
Example using c-strings:
Now the same thing using C++ strings. Notice you don't need to specify the length of the string. The calls to strcpy and strcat are much simpler - see line 12 below.
| |||||
|
Last edited on
|
|||||
| Chervil (1206) | |
|
@ Aramil of Elixia this line isn't correct. The destination cannot be written to: strncat(command.c_str(), inputString.c_str(), length);(it looks like a mutant hybrid of C and C++ code). | |
|
Last edited on
|
|
| Aramil of Elixia (772) | |
| mhhhhm i wasn't thinking yeah so just turn command into a null ptr | |
|
|
|