Okay, I'm building a small console app to launch batch files. I have user inputs for selecting the drive letter, and the folder path. The batch files have the same name as the drive letter (i.e. d.bat, e.bat, etc.).

drvPath = (drive + ":\\" + fpath + "\\" + drive + ".bat");

This actually works, so the variable stores the entire path and bat file name.


This does not work. I get the following error:
error: cannot convert 'std::string' to 'const char*' for argument '1' to 'int system(const char*)'|

The bottom line is, I'm using user input to determine a drive letter and a path to launch the bat file. I don't understand the error.

Here's the entire code:

#include <iostream>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

using namespace std;

int main()
    string drive;         // holds the drive letter
	string fpath;		// holds the folder name
	string drvPath;	    // Combines the drive and folder names
	int x;              // Number for option selection

	cout << "Enter Drive Letter of this Thumb Drive: " << endl;
	cin >> drive;
	cout << "you selected drive " << drive << " for this operation." << endl;
	cout << endl;
	cout << endl;

	cout << "Select 1 if you are working on a OLD PC" << endl;
	cout << "Select 2 if you are working on a NEW PC" << endl;
	cout << "Select 3 if you are working on a STAND ALONE PC" << endl;
	cin >> x;

        case 1:
            fpath = "old";
        case 2:
            fpath = "new";
        case 3:
            fpath = "standalone";
            cout << "Invalid selection" << endl;

	drvPath = (drive + ":\\" + fpath + "\\" + drive + ".bat");
        cout << drvPath; //debug line


    return 0;

Any help would be greatly appreciated.

The error is precisely as stated. The system() function expects an argument of type const char*, and std::string doesn't have an implicit conversion to that type. You need to call the c_str() member function:


That worked brilliantly! Thank you!

Can you also tell me the C++ equivalent of this C# statement?

if (DateTime.Now >= new DateTime(2011, 8, 20)