String variable as a File Path

Hello, I am trying to create a simple program that gets a file path from the user, stores it in a variable and then a function such as mciSendString or ShellExecute can use to know what file to play or launch.

I am finding a difficulty implementing this tactic.

The following shows a stripped down version of what I am trying to do:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#include <iostream>
      #include <string>
      #include <Windows.h>
      using namespace std;


      string FilePath;  //The File path variable
      
      cout << "Enter the file path: \n";  

      cin >> FilePath;  //Accepting a file path from a user


      mciSendString("open FilePath", NULL, 0, 0);  //Plays the music file


      fflush(stdin);   //Pauses the program
      getchar();
      return 0;




Any help would be greatly appreciated regarding this matter.

Thank you for your time.
If mciSendString takes a const char* argument, just use the c_str() function of std::string.

Btw:
fflush(stdin); //Pauses the program
this line is undefined IIRC, you don't need it to pause if you have a getchar() and proper string handling.
I appreciate your guidance, this problem plagued me for a quite a while now.

Thanks a lot.

Oh, and for anyone else suffering from this problem, here's what I did:

1
2
3
4
5
6
7
8
9
string path;  // File path string
                
cin >> path;   //Accepting file path

path = "play " + path;              //Associating mci command "play" with the file path
const char * c = path.c_str();   //Converting path into something mci can use
	
mciSendString(c, NULL, 0, 0);   //Playing the file 



Again, thanks
FYI, you could also just do:

mciSendString(path.c_str(), NULL, 0, 0);
Topic archived. No new replies allowed.