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 06: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
    23,641
    No, like this:
    Code:
    system(newstring.c_str());
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    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, 09:25 AM
  2. Replies: 4
    Last Post: 03-03-2006, 01:11 AM
  3. Replies: 4
    Last Post: 06-13-2005, 09:03 AM
  4. Classes inheretance problem...
    By NANO in forum C++ Programming
    Replies: 12
    Last Post: 12-09-2002, 02:23 PM
  5. creating class, and linking files
    By JCK in forum C++ Programming
    Replies: 12
    Last Post: 12-08-2002, 01:45 PM

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