Thread: System Command with variables

  1. #1
    Registered User S0n1C's Avatar
    Join Date
    Oct 2006
    Posts
    12

    System Command with variables

    Hey guys, I'm just starting to get back into C++ Programming, and forget how to call a System Command and input variable values into the command. Currently I am creating a little Internet Utility program, one of the options of the program is to allow the user to delete his/her current gateway route, and add a new one. How ever when creating a new one, you must enter the IP Address you would like to use as well as the network mask and gateway. Here is what I have already, but i know for a fact that it doesn't work.

    Code:
    /*Variables*/
            char IP, mask, gateway;      //user input information
            system("cls");               //clear screen
            SetConsoleTitle("Add Gateway route");             //reset Console title
            cout<<"Please enter IP Address:\n"<<flush; //promted to enter ip address
            cin>>IP;      //enter IP Address
            cout<<"Please enter network mask:\n"<<flush //prompted to enter network mask
            cin>>mask;    //enter network mask
            cout<<"Please enter network gateway:\n"<<flush //promted to enter network gateway
            cin>>gateway; //enter network gateway
            /*Add gateway route*/
            system("route add", IP, mask, gateway);
            cout<<"\n\nPress <ENTER> to continue..."<<flush;         //promted to continue
            cin.get();       //hold screen
    Thanks in advance
    S0n1C!

  2. #2
    Registered User Noir's Avatar
    Join Date
    Mar 2007
    Posts
    218
    IP, mask, and gateway should be string instead of char. char only holds one character and string can hold more than you'd ever need. Since they're strings, you can just add them together into one and then pass that to system():
    Code:
    /*Variables*/
            string IP, mask, gateway;      //user input information
            system("cls");               //clear screen
            SetConsoleTitle("Add Gateway route");             //reset Console title
            cout<<"Please enter IP Address:\n"<<flush; //promted to enter ip address
            cin>>IP;      //enter IP Address
            cout<<"Please enter network mask:\n"<<flush //prompted to enter network mask
            cin>>mask;    //enter network mask
            cout<<"Please enter network gateway:\n"<<flush //promted to enter network gateway
            cin>>gateway; //enter network gateway
            /*Add gateway route*/
            string command = "route add " + IP + " " + mask + " " + gateway;
            system( command.c_str() );
            cout<<"\n\nPress <ENTER> to continue..."<<flush;         //promted to continue
            cin.get();       //hold screen

  3. #3
    Registered User S0n1C's Avatar
    Join Date
    Oct 2006
    Posts
    12
    Hey thanks, i knew about the string thing, sorry about that I was just forgot about it. Thanks i knew that you could ass 'strings' to the system command i just wasn't sure how, thanks again.

    S0n1C!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 4
    Last Post: 06-13-2005, 09:03 AM
  2. Replies: 3
    Last Post: 06-13-2005, 07:28 AM
  3. Why Can't C++ Be Used to Develop Operating System?
    By Antigloss in forum C++ Programming
    Replies: 7
    Last Post: 05-27-2005, 06:16 AM
  4. Putting variables in SYSTEM
    By mycro in forum C++ Programming
    Replies: 3
    Last Post: 05-29-2003, 07:59 PM
  5. System Calls && Variables
    By Okiesmokie in forum C++ Programming
    Replies: 6
    Last Post: 03-06-2002, 09:10 PM