Format trouble

I need help to format a drive where the user enters the letter that needs to be formmatted and then it formats that drive. Need help desperately.

Have tried using variables here is my code:

#include <iostream>

int main() {
std::cout << "Welcome to Magic Memory Stick Creator Version 1.5. Please follow all instructions on screen." << std::endl;
std::cout << "Please enter the drive in which your psp is plugged in." << std::endl;
char iDrive;
std::cin >> iDrive;
std::cout << "The drive " << iDrive << " will now be formatted." << std::endl;
system("format iDrive:");
return 0;
}


I am new to C++
You don't happen to be a perl programmer?

"format iDrive:" is a string literal (ie, constant). You are expecting that the compiler will substitute the contents of your iDrive variable in place of the iDrive in the string literal. This does not happen.

You need to build the string yourself. For example:

1
2
3
4
5
6
#include <string>

// ...

std::string greeting = std::string( "Hello" ) + ' ' + "World!";
std::cout << greeting << std::endl;


Outputs

Hello World!
Could you fix the code for me!! I am studying strings at the moment and trying to get to grips with it!!

Thanks
Last edited on
Well I all but did fix it for you. The only other step you need is to extract the (const) char* from the string because system() expects a const char*.

1
2
3
4
5
std::cout << "Enter directory: " << flush;
string dir;
cin >> dir;
string command = "dir " + dir;
system( command.c_str() );

will that format a user inputted drive
Here is my code:

#include <iostream>
#include <string>

int main() {
std::cout << "Welcome to Magic Memory Stick Creator Version 1.5. Please follow all instructions on screen." << std::endl;
std::cout << "Please plug in your PSP and put it onto USB mode." << std::endl;
std::cout << "Please enter the drive in which your psp is plugged in." << std::endl;
std::string dir;
std::cin >> dir;
std::string ; command = ( "dir " ) + ' ' + ( "dir" );
;system(command.c_str() );
return 0;
}


I keep getting these errors:

1>c:\documents and settings\timbo\my documents\visual studio 2008\projects\magic memory stick creator\magic memory stick creator\main.cpp(10) : error C2065: 'command' : undeclared identifier

1>c:\documents and settings\timbo\my documents\visual studio 2008\projects\magic memory stick creator\magic memory stick creator\main.cpp(10) : error C2110: '+' : cannot add two pointers

1>c:\documents and settings\timbo\my documents\visual studio 2008\projects\magic memory stick creator\magic memory stick creator\main.cpp(11) : error C2065: 'command' : undeclared identifier

1>c:\documents and settings\timbo\my documents\visual studio 2008\projects\magic memory stick creator\magic memory stick creator\main.cpp(11) : error C2228: left of '.c_str' must have class/struct/union

Please help me because I am new to C++
You have an extraneous semicolon between std::string and command.
Also you have another one before system.

"dir" is the command to list a directory's contents. "format" as you originally had is used to format.
Thanks.

Can you fix this code by changing the exact code rather than giving me snippets.

Because when I run the program it does this:

Please enter drive in which psp is plugged in
J
Volume in drive C has no label
Volume serial number is 5843-E8D8

Directory of c:\Documents and Settings\Timbo\My Documents\Visual Studio2008\Projects\Magic Memory Stick Creator

File not found
Press any key to continue...
I don't generally just write the code for the person; that does not teach the person anything, and the programming profession is all about continual education.

It sounds like it couldn't find the format.exe / format.com executable.

All you have to do to my original code was replace "dir " with "format ".
#include <iostream>
#include <string>

int main() {
std::cout << "Welcome to Magic Memory Stick Creator Version 1.5. Please follow all instructions on screen." << std::endl;

std::cout << "Please plug in your PSP and put it onto USB mode." << std::endl;

std::cout << "Please enter the drive in which your psp is plugged in." << std::endl;

std::string format;

std::cin >> format;

std::string command = "format " + format;

system( command.c_str() );

return 0;
}

That is the code and it just says

Invalid Drive Specification.
It most probably would fail with invalid drive specification.

If the drive to be formatted was the A drive, and the user entered A

then your format command would be format A
That command isn't quite right.
What is then
The drive name should have a colon : at the end..
So it should be format A:
so where would i add this after all formats or just 1?? And does that mean it won't be user inputed
Once the user has input the drive letter, we can check the last character position to see if it is a colon.
If the user has forgotton the colon we can add one.

So we can have something like this:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
#include <iostream>
#include <string>

using namespace std;

int main() {
std::cout << "Welcome to Magic Memory Stick Creator Version 1.5. Please follow all instructions on screen." << std::endl;

std::cout << "Please plug in your PSP and put it onto USB mode." << std::endl;

std::cout << "Please enter the drive in which your psp is plugged in." << std::endl;

std::string format;

std::cin >> format;

//Check if user has remembered to add the colon to the drive letter
if( format.at (format.length()-1) != ':'  )
{
    //if not we add it ourselves
    format += ':';
}

std::string command = "format " + format;

system( command.c_str() );

system("pause");

return 0;
}
Topic archived. No new replies allowed.