Thread: A variable in "System"

  1. #1
    Registered User
    Join Date
    Oct 2006
    Posts
    8

    A variable in "System"

    Hi,

    Ive read around and searched, but i cannot find any help to my question.

    I was wondering, if you want to add a variable in the System() command, how would that look/work?

    Code:
    system("net localgroup "string/variable for group" "string/variable for user" /add");
    // Olsson

  2. #2
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    Build the command in a single string variable first, then pass that string variable to the system command (you'll need to use the c_str() method if using C++ strings).

    If your "string/variable for group" variables are C++ strings, you could also just use + to add them all together inside the system call.

  3. #3
    Registered User
    Join Date
    Oct 2006
    Posts
    8
    Quote Originally Posted by Daved
    Build the command in a single string variable first, then pass that string variable to the system command (you'll need to use the c_str() method if using C++ strings).

    If your "string/variable for group" variables are C++ strings, you could also just use + to add them all together inside the system call.
    Could you give me an example for these? I've been looking around, cant find any good ones.

  4. #4
    Registered User
    Join Date
    Oct 2006
    Location
    Canada
    Posts
    1,243
    Code:
    #include<iostream>
    #include<string>
    using namespace std;
    
    int main()
    {
    	string test;
    
    	test = "this is ";
    	test += "a string";
    
    	cout << test;
    }
    output: this is a string
    hopefully this gives you the answer.

  5. #5
    Registered User
    Join Date
    Oct 2006
    Posts
    8
    thank you nadroj, helped me. Im still pondering my head over how i should get this in a system command.. Any sollutions to that?

  6. #6
    Registered User
    Join Date
    Oct 2006
    Location
    Canada
    Posts
    1,243
    its the same prinicible. cout is an object with parameters and functions, and system is a function with parameters. pass 1 string parameter to the system call. use the functions in 'string' to append, insert, or whatever you have to do to create one string as you displayed above.

  7. #7
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    Why don't you show us your code snippet to see where you are going wrong. In nadroj's example, you call system like this: system(test.c_str());

  8. #8
    Registered User
    Join Date
    Oct 2006
    Posts
    8
    Quote Originally Posted by Daved
    Why don't you show us your code snippet to see where you are going wrong. In nadroj's example, you call system like this: system(test.c_str());
    Uhm sure.

    Its kinda ugly right now, havent really done much to it. Its for a school project thing.

    The use of the program is to List users/groups in the computor using system commands and you should be able to change the password/groups of users.

    Code:
    #include <cstdlib>
    #include <iostream>
    
    using namespace std;
    
    int main(int argc, char *argv[])
    {
    
        system("net users > users.txt");
        system("net localgroup > groups.txt");
        
        system("net users");
        cout<<"What user would you like to add, to what group?" <<endl;
        string user;
        getline (cin, user);
        system("net localgroup");
        cout<<"You would like to add "<< user <<" to what group?" <<endl;
        string group;
        getline (cin, group);    
        cout<<user<<" has been added to group "<< group <<endl;
    
           string systemcmd;
               systemcmd = "net localgroup ";
               systemcmd += group;
               systemcmd += " ";
               systemcmd += user;
               systemcmd += " /add";
    
        cout<<"Executing - "<<endl<<systemcmd<<endl;
        system(systemcmd.c_str());
        cin>>user[1];
        return 0;
    }
    I got the append thing to work now also, thanks guys.

    As you can see, i have alot to do and change to it but its coming along. Thanks for your help.

  9. #9
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    It actually doesn't look too bad. A couple things you should note, though. First, you are using the string class (good) so you need to #include <string>. It might work without it on your compiler, but on some compilers it only works partially and others not at all, so it is best to always put it in there. The other thing is that technically it is possible for the user string variable to not have two characters. When you read into user[1] you are setting the second character of the string, which might crash your program if the string has only one character or is empty. Just use cin.get() to read a character from the stream and pause your program, it does basically the same thing.

  10. #10
    Registered User
    Join Date
    Oct 2006
    Posts
    8
    Yup, fixed that user[1] and im adding some more features, its soon done. Thanks for all your help, once again.

  11. #11
    Registered User VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,607
    And you need system to do this why?

    I'm not a big fan of system().

  12. #12
    Registered User
    Join Date
    Oct 2006
    Posts
    8
    Quote Originally Posted by Bubba
    And you need system to do this why?

    I'm not a big fan of system().
    Well, if you could give me some tips that'd be great! Im really new to all of this, and this was a project at school.. But if you have any other ways to do this, please show =)

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Question about printing a variable???
    By Hoser83 in forum C++ Programming
    Replies: 2
    Last Post: 03-31-2006, 01:57 PM
  2. How accurate is the following...
    By emeyer in forum C Programming
    Replies: 22
    Last Post: 12-07-2005, 12:07 PM
  3. Replies: 2
    Last Post: 04-12-2004, 01:37 AM
  4. write Variable and open Variable and get Information
    By cyberbjorn in forum C++ Programming
    Replies: 2
    Last Post: 04-09-2004, 01:30 AM
  5. Variable question I can't find answer to
    By joelmon in forum C++ Programming
    Replies: 3
    Last Post: 02-12-2002, 04:11 AM