Thread: Problem using system() command.

  1. #1
    mysterious mongoose
    Join Date
    Apr 2005
    Posts
    18

    Problem using system() command.

    I'm making a program that makes use of the net send command in windows. Anyway, the problem is that i cant use strings as part of the argument in the system() command. I know that there is one way to fix the problem, and that is to have my program create and execute a batch file, but i wanted to know if there was an easier way. Here is my program.

    Code:
    #include <iostream>
    
    using namespace std;
    
    int main()
    {
    string message;
    cout<<"This program will send a message to everyone on the network.\nWhat do you want the message to say?\n";
    getline(cin, message, '\n');
    system("net send * " + message);
    /* I also tried:
    string command = "net send * " + message;
    system (command)
    */
    cout<<"your message has been sent!\n";
    cin.get();
    cin.get();
    }
    Does anyone know a way to get it to work, or do i have to make a batch file?

  2. #2
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    Sure, like this:

    Code:
    char s[100], t[70];
    
    strcpy(s, "type ");
    strcat(s, t);
    
    system(s);
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  3. #3
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    I would guess something like the following would work;

    Code:
    #include <the header that declares system()>
    #include <string>
    #include <iostream>
    
    using namespace std;
    
    int main()
    {
        string message;
        cout<<"This program will send a message to everyone on the network.\nWhat do you want the message to say?\n";
        getline(cin, message, '\n');
        string command = "net send * ";
        command += message;
        system(command.c_str());
        cout<<"your message has been sent!\n";
    }
    The reason is that the system() function usually accepts a char * argument, not a C++ string object. And char *'s cannot be simply added, although C++ strings support such an operation.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. the system command
    By happyclown in forum C Programming
    Replies: 7
    Last Post: 01-02-2009, 07:14 AM
  2. Replies: 3
    Last Post: 06-13-2005, 07:28 AM
  3. Problem Reporting System. Need Advide!
    By brunomiranda in forum Tech Board
    Replies: 9
    Last Post: 09-25-2003, 09:21 PM
  4. Using Variables with th 'System' command
    By dapernia in forum Linux Programming
    Replies: 2
    Last Post: 09-15-2003, 05:11 PM
  5. system command
    By vk in forum C Programming
    Replies: 13
    Last Post: 04-03-2002, 07:44 PM