I am very new to programming in general and am trying to teach myself C++. The program I am in the midst of writing is coming along nicely, in each version I am wanting to add funtionality. The program is a very basic “file sorter” that will sort “loose” files into folders based on file type for a given directory. in v0 everything was hardcoded so to change which directory it sorted you had to change the coding. V1 included an update so that it will prompt the user in the cli which directory they wanted to organize. now in v2 I am working on a way to prompt the user which folder types they want to include, currently the folders it creates are automatic and hardcoded: Images, documents, videos, etc… SO my question is this – currently the function that creates the folders references a unordered_map and my instict for an elegant solution is to prompt the user for each folder name asking a Y/N and then somehow void or turn off the folders that they dont want. Can I input functions in my map? Should I create a seperate function for each different folder? How might you solve this particular problem? Any help is appreciate, thanks so much!
The map—
std::unordered_map<std::string, std::string> extensions_mapping = {
{".pdf", "PDFs"},
{".doc", "Documents"},
{".docx", "Documents"},
{".xls", "Spreadsheets"},
{".xlsx", "Spreadsheets"},
{".csv", "Spreadsheets"},
{".ppt", "Presentations"},
{".pptx", "Presentations"},
{".jpg", "Images"},
{".png", "Images"},
{".gif", "Images"},
{".jfif", "Images"},
{".jpeg", "Images"},
{".mp4", "Videos"},
{".avi", "Videos"},
{".mkv", "Videos"},
{".mp3", "audio"},
{".wav", "audio"},
{".zip", "Compressed"},
{".rar", "Compressed"},
{".exe", "Programs"},
{".h", "src"},
{".c", "src"},
{".py", "src"}
};
The function that creates the folders—
for (const auto pair : extensions_mapping) {
fs::path folder_path = downloads_path + "/" + pair.second;
if (!fs::exists(folder_path)) {
fs::create_directory(folder_path);
}
}
So I have currently tried to figure out what exactly that map is and what ways I can manipulate the data. I know that I could certainly create a new function with a cout prompt for each folder type that I want but that feels like a terribly hacky solve and I am honestly at a loss for what I can and cant do in this language. Dont kill me, I started learning how to program like last week and I know that I have a TON more to learn and this is probably a very simple issue and this may not be the best forum to ask this question. However I learn best through brute force haha and if you are willing to throw me your two cents I would be forever grateful.
Eric is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.