Hi all. Fairly new to C++ first off. So I wrote a program that has a main menu with several calculators to choose from and after the user has input the data and the calculations have run, I have a prompt that shows up asking the user if they would like to return to the main menu or close the program. From reading on the forum I copied a command into my program that opens itself up and closes the old one if the user selects "Main Menu." The only way this works though is if I manually enter the programs file path into the ShellExecute command.
My question is, is there a way to make the program automatically recognize where it is saved on the computer that way I can reference the file path to open itself up in a new window as opposed to manually typing the file path that is it stored in? I want to be able to save the program anywhere on the computer and not have to worry about editing the code to manually input the file path to where it's stored. Below is an excerpt of the code I'm currently using. Again, I'm new so I'm not too familiar with what the ShellExecute command is actually doing because I copied and pasted it from another post on this forum.
cout << "What would you like to do?" << endl;
cout << endl;
cout << "1 - Main Menu" << endl;
cout << "2 - Exit" << endl;
cout << endl;
cin >> end_decision;
if (end_decision < 1 || end_decision > 3)
{
cout << "Invalid option. Please choose from one of the menu options and press enter." << endl;
cout << endl;
cin >> end_decision;
That works perfect for identifying the location of where the program is stored. However, I'm still a little trumped on how to incorporate that into the ShellExecute command so that the program opens itself in a new window and closes the old one.
Again, the overall intention is that when the user selects "Main Menu" that the program opens itself in another window, and closes the old one. And my current code works perfect for that except like I explained earlier, it only works if I manually type the file path into the ShellExecute command. Is there a way to assign the file path from the code you posted to a variable and insert that variable into the shellexecute command?
I also probably should have asked if there might be an easier way to accomplish what I'm trying to accomplish. I don't care how I accomplish it, I just want to basically restart the program when the user chooses main menu but I also want to be able to save the program anywhere on my computer without having to go in and manually edit the code to reflect the file path every time I move the file. End goal is i'd like to write a program, build an .exe version of it, and e-mail it to family members and friends. I want them to be able to download the program and not have to worry about going in and editing the source code to edit the file path where it is saved. I want to program to simply open itself again in another window and close the old window.
Is there a way to assign the file path from the code you posted to a variable and insert that variable into the shellexecute command?
Yes that's the easiest part.
1 2 3 4
std::string exename = GetAppName();
ShellExecuteA(0, "open", exename.c_str(), 0, 0, SW_NORMAL);
// try to close the old one
exit(0);
I also probably should have asked if there might be an easier way to accomplish what I'm trying to accomplish. I don't care how I accomplish it, I just want to basically restart the program when the user chooses main menu
If you show us the whole code someone might find an easier way.