Putting a string in system()

This is a discussion on Putting a string in system() within the C++ Programming forums, part of the General Programming Boards category; I'm trying to make a program that outputs a set string, and a variable into system(). However, I can't make ...

  1. #1
    Registered User
    Join Date
    Jun 2009
    Posts
    45

    Putting a string in system()

    I'm trying to make a program that outputs a set string, and a variable into system(). However, I can't make system() accept it.

    Code:
    #include <iostream>
    #include <stdlib.h>  //For system()
    
    using namespace std ;
    
    int main()
    {
    string name ;
    string newstring ;
    
    cout << "Enter a string:" ;
    cin >> name ;
    newstring = "mkdir "+name;              //Adds mkdir and your input.
    
    system(newstring);              //Is the problem.
    
    //cout << newstring <<endl;              /*For testing*/
    }
    Note that mkdir is just an example. I've already been advised to type 5 more letters.
    Last edited by Muscovy; 07-14-2009 at 07:45 PM.

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    system will only accept a C-style string. Fortunately you can convert a std::string to a C-style string with the .c_str() member function.

  3. #3
    Registered User
    Join Date
    Jun 2009
    Posts
    45
    Do I use it like this?
    Code:
    .c_str(newstring)

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    22,303
    No, like this:
    Code:
    system(newstring.c_str());
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #5
    Registered User
    Join Date
    Jun 2009
    Posts
    45
    Thanks!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 2
    Last Post: 03-05-2009, 10:25 AM
  2. Replies: 4
    Last Post: 03-03-2006, 02:11 AM
  3. Replies: 4
    Last Post: 06-13-2005, 10:03 AM
  4. Classes inheretance problem...
    By NANO in forum C++ Programming
    Replies: 12
    Last Post: 12-09-2002, 03:23 PM
  5. creating class, and linking files
    By JCK in forum C++ Programming
    Replies: 12
    Last Post: 12-08-2002, 02:45 PM

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