*sigh*
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.

system(drvPath);

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;
	//system(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;

    switch(x)
    {
        case 1:
            fpath = "old";
            break;
        case 2:
            fpath = "new";
            break;
        case 3:
            fpath = "standalone";
            break;
        default:
            cout << "Invalid selection" << endl;
    }


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

	system(drvPath);

    return 0;
}

Any help would be greatly appreciated.
Thanks,
Hendo

Recommended Answers

All 3 Replies

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:

system(drvPath.c_str());

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)
    Environment.Exit(0);
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.