Ok, first up, to actually make the directories (folders) on your hard drive, you may to need to use functions that are dependant on the operating system you are using. I have not done anything like this, so I cannot help you there.
What I can do is write an algorithm that does what you want. Then write some code that does what the algorithm does.
Ok, what you should do is:
- Create a function that takes the folowing parameters.
string currrentFolder // This is the folder that you want to create the new folder into. When you call the function the first time, it should be C:\\ or C:/ If you need to make a string or char that is \, you do it this way \\
string nameOfFolder; // The name of the folder you want to create, like foobar.
int numberOfFoldersTotal; // This would be 4 all the time if you entered 4.
int numberOfFoldersLeft; // This is how many folders are left to create. So if it is 0 or less, you stop the recursion.
// If you want the folder hold parameter, you would use either "\\" or "/" I have not said that this is a required parameter, but you may want it to be. If it is a requried parameter, you should pass in C: as the initial folder instead of C:\\ or C:/ as I said above.
- In this function, it should do something like this.
-- if number of folders left is 0
--- return;
-- end if
-- make a new string with the folder name
-- make an int of the number of folders total minus the number of folders left
-- if the int is > 0
--- add this number to the string of the folder name.
-- end if
-- create the folder using the folder name you just created. If you are actually making the folder, use the name of the current folder as the place to make the new folder.
-- append the current folder with the folder hold (as you call it) and the new name of the folder
-- call the function again (recursion) with the same parameters, except the current folder has changed and the number of folders left should be one less.
- Use a function to convert an int to a string as you said itoa did not really work for you.
- Then in you main, you get the user input and call the function. You should not have to have to call one function that then calls the recursive function as your code seems to do.
Now for the code:
string intToString(int n);
void createFolders(string currentFolder, string nameOfFolder, int numberOfFoldersTotal, int numberOfFoldersLeft, string folderHold = "\\"); // or "/" for the folderHold.
int main()
{
string currentFolder = "C:\\"; // "C:/";
string nameOfFolder;
int numberOfFoldersTotal;
// get the input for the name of the folder and the number of folders.
createFolders( currentFolder, nameOfFolder, numberOfFoldersTotal, numberOfFoldersTotal + 1);
return 0;
}
void createFolders(string currentFolder, string nameOfFolder, int numberOfFoldersTotal, int numberOfFoldersLeft, string folderHold = "\\") // or "/" for the folderHold.
{
if (numberOfFoldersLeft <= 0)
return;
}
string folderNameNew = nameOfFolder;
int left = (numberOfFoldersTotal - numberOfFoldersLeft) + 1;
if (left > 0)
{
folderNameNew += intToString( left );
}
// create the folder using the folder name you just created. If you are actually making the folder, use the name of the current folder as the place to make the new folder. Do it in this line, I can't help with this :(
currentFolder += folderNameNew + folderHold;
// If you want to printout the name fo the folder, do it here using the currentFolder as the name
createFolder( currentFolder, nameOfFolder, numberOfFoldersTotal, numberOfFoldersLeft - 1, folderHold );
}
string intToString(int n)
{
ostringstream ss; // You need to include sstream for this.
ss << n;
return ss.str();
}
Of course, you might want to do some checking of the inputs from the user, to make sure the name of the folder would be valid, the number of folders to create is > 0, etc.
Good luck with it.