Monday, July 27, 2009

Help in C++?

I'm very new to C++. Can anyone tell me why this function doesn't work?





bool UnlockDrive(char PW[70])


{


if (PW!="") {


char ExecString[350];


ExecString = "";


char sys1[90];


sys1 = "start \"mount.exe /v locked.dsk /l z /h n /c n /p \"";


char sys2[90];


sys2 = "\" /m rm /q\"";


strcat(ExecString, sys1);


strcat(ExecString, PW);


strcat(ExecString, sys2);


system(ExecString);


}


return true;


}





I'm using Borland Turbo C++ version 10.0. I get the following error when I try to build the application:





Lvalue required





It is referring to the lines:





ExecString = "";


sys1 = "start \"mount.exe /v locked.dsk /l z /h n /c n /p \"";


sys2 = "\" /m rm /q\"";





Any help would be greatly appreciated!

Help in C++?
ExecString, sys1 and sys2 have their size declared, I think you need to set them to a string literal containing the same number of characters. What you can do is this instead:





char *ExecString = NULL, *sys1 = NULL, *sys2 = NULL;


sys1 = "start \"mount.exe /v locked.dsk /l z /h n /c n /p \"";


sys2 = "\" /m rm /q\"";


strcat(ExecString, sys1);


strcat(ExecString, PW);


strcat(ExecString, sys2);


system(ExecString);
Reply:Since u r using C++ use standard library's string class std::string instead of char *:





using namespace std;


std::string execstring;


execstring = "" ....


system(execstring.c_str()); Report It

Reply:The problem is ExecString="". This is in conflict with the definition just one line above.





There are many ways to fix this.


(1) If you change that line to strcpy( ExecString, "" ) you should be fine.





(2) Or you could eliminate that line change the strcat(ExecString, sys1); to strcpy(ExecString, sys1);





Good luck!

order flowers

No comments:

Post a Comment