Using variables within system command?

This is a discussion on Using variables within system command? within the C++ Programming forums, part of the General Programming Boards category; The program seems to run with just the iostream header file on its own, why arent the others required? Just ...

  1. #16
    Registered User
    Join Date
    Aug 2005
    Posts
    13
    The program seems to run with just the iostream header file on its own, why arent the others required? Just curious

  2. #17
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,892
    Due to the way the standard library is usually implemented, some standard headers include others internally, and thus the symbols from those other headers become available, too.
    The important thing is that this is only a side effect, and a completely unpredictable at that. A different compiler, even a different standard library, might be implemented differently and make your program fail.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  3. #18
    Registered User
    Join Date
    Aug 2005
    Posts
    13
    Code:
    #include <iostream>
    #include <dos.h>
    #include <string>
    
    using namespace std;
    
    int select;
    string host, msg;
    string systemSender = "net send ";
    
    
    
    char getinfo()
    {
         system("cls");
         cout<< "------------------------\n"
             << "Potato Net Sender v0.01\n"
             << "------------------------\n\n";
         cout<< "Hostname: ";
         cin>> host;
         cout<< "Message: ";  
         getline (cin, msg, '\n');
         cin>> msg;
    }
    
    char send()
    {
         systemSender += host + " " + msg;
         system(systemSender.c_str());
    }
    
    char selection1()
    {
                cin.get();
                getinfo();
                send();
    }
    
    int exit_app()
    {
        return 0;
    }
    
    char intro()
    {
         system("cls");
         cout<< "------------------------\n"
             << "Potato Net Sender v0.01\n"
             << "------------------------\n\n";
             
         cout<< "1. Send a message to another computer\n"
             << "2. Exit\n\n";
         cout<< "Selection: ";
         cin>> select;
         
         if (select == 1)
         {
                selection1();
         }    
         else if (select == 2)
         {
              exit_app();
         }
         else
         {
             system("cls");
             cout<< "\nYou must enter either 1 or 2 to proceed\n\n";
             cout<< "Press any key to try again...";
             cin.get();
             selection1();
         }
    }
    
    int main()
    {  
        intro();
        cin.get();
    }
    Hmm I'm having another slight problem, possibly I'm using the wrong syntax for storing the string into variable msg.

    Whenever I type a message with more than one word it will just net send the first word, it doesnt seem to be capturing the whole string.

    For Example:
    Code:
    Hostname: 127.0.0.1
    Message: Hello this is a test
    That will send only "Hello" through net send.

  4. #19
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,892
    That's probably because net send only sends its first argument. You must make sure quotes appear on the command line, i.e. the string you send to system.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  5. #20
    Registered User
    Join Date
    Aug 2005
    Posts
    13
    Yeah but I want to be able to store what I type in the string without the quotes, isnt that possible?

    [edit]
    Doesnt work with quotes anyway
    Last edited by moagim; 10-09-2006 at 05:14 AM.

  6. #21
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,333
    > systemSender += host + " " + msg;
    > system(systemSender.c_str());
    If you're just being hackish and using system() to do all the dirty work for you, then you need to be more careful with the command line you construct.

    Say
    Code:
         systemSender += host + " \"" + msg + "\"";
         system(systemSender.c_str());
    to include the message you want to send in double quotes
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  7. #22
    Registered User
    Join Date
    Aug 2005
    Posts
    13
    What else could I use other than system() ? I use it cause its all I know so far, also, I dont quite understand that code you typed, and it gives me the same result as before

  8. #23
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,333
    Does this work?
    Code:
    system( "net send \"hello world\" ");
    > What else could I use other than system() ?
    Like the proper sockets interface to write true networking applications.

    You can basically do what you have at the moment using a batch file.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

Page 2 of 2 FirstFirst 12
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, 06:14 AM
  2. Replies: 6
    Last Post: 07-21-2008, 03:10 AM
  3. Replies: 30
    Last Post: 09-12-2006, 07:06 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 Calls && Variables
    By Okiesmokie in forum C++ Programming
    Replies: 6
    Last Post: 03-06-2002, 08:10 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21