0

can i make a program open another program after a user selects it such as

i am making a menu and i want if the user enters 1 it opens the program accociated with 1 or will i have to paste all the files from the external program to the menu one?

2
Contributors
3
Replies
4
Views
6 Years
Discussion Span
Last Post by restrictment
0

Yes it is possible.
You can use any of the following functions to do so as well:
The standard, unportable system() process, or these ones which give you a bit more control: fork(), exec(), CreateProcess()

how would i put this the other application i want to open is called "Task 1 application b"

ive tried this

exec()
    {
    task 1 program B
    }

and both programs are in the same folder.

0

how would i put this the other application i want to open is called "Task 1 application b"

ive tried this

exec()
    {
    task 1 program B
    }

and both programs are in the same folder.

You should try reading the links that I supplied so you can understand how to use the functions.

If you need an example, here is a rather lengthy example of 'createprocess()'. *Note: This program was not created by me!*

#include <iostream>
#include <string>

#include <windows.h>

using namespace std;

// This function will ask a user for a password.
int Authenticate()
{
        string password;

        cout << "Password: ";
        cin >> password;
        if(password == "12345")
                return 1;

        return 0;
}

// This function will list the available games.
void ListGames()
{
        cout << "Games:" << endl;
        cout << "Minesweeper" << endl;
        cout << "Solitare" << endl;
        cout << "Pinball" << endl;

        // Sleep 500ms
        _sleep(500);
}

// This function launches a game.
void LaunchGame(char *command)
{
        STARTUPINFO si;
        PROCESS_INFORMATION pi;

        ZeroMemory( &si, sizeof(si) );
        si.cb = sizeof(si);
        ZeroMemory( π, sizeof(pi) );

        // Start the child process. 
        if( !CreateProcess( NULL,   // No module name (use command line)
                command,                // Command line
                NULL,              // Process handle not inheritable
                NULL,              // Thread handle not inheritable
                FALSE,            // Set handle inheritance to FALSE
                0,                        // No creation flags
                NULL,              // Use parent's environment block
                NULL,              // Use parent's starting directory 
                &si,                    // Pointer to STARTUPINFO structure
                π )                // Pointer to PROCESS_INFORMATION structure
        ) 
        {
                printf( "CreateProcess failed (%d)\n", GetLastError() );
                return;
        }

        // Wait until child process exits.
        WaitForSingleObject( pi.hProcess, INFINITE );

        // Close process and thread handles. 
        CloseHandle( pi.hProcess );
        CloseHandle( pi.hThread );
}

int main(void)
{
        string answer;

        // Authenticate the user. If authentication fails quit.
        if(!Authenticate())
        {
                cout << "Incorrect Username" << endl;
                cout << "-- CONNECTION TERMINATED --" << endl;
                _sleep(500);
                exit(1);
        }

        // Set the console title.
        SetConsoleTitle("Hello, Mr. HAXIFIX");
        
        // Loop until the user quits or wants to play a game.
        while((answer != "Yes") && (answer != "yes"))
        {
                // Empty the string so we can use it again.
                answer.empty();

                cout << "Would you like to play a game?" << endl;
                cout << "Answer: ";
                cin >> answer;

                if((answer == "No") || (answer == "no"))
                {
                        cout << "Ok, I will ask later." << endl;
                        // Sleep 1 second
                        _sleep(1000);
                }
                else if((answer == "quit") || (answer == "exit"))
                {
                        cout << "Exiting now..." << endl;
                        exit(1);
                }
        }

        // Clear the console.
        system("CLS");

        // Loop until the user is ready to quit.
        while((answer != "quit") && (answer != "exit"))
        {
                // Empty the string so we can use it again.
                answer.empty();
                cout << "What game would you like to play?" << endl;
                cout << "Type 'list games' for a list." << endl;
                cout << "Answer: ";

                // Flush stdin in case there are any stagnant carriage returns or line-feeds laying about.
                fflush(stdin);

                // We use getline() here because the answer might contain a space.
                getline(cin, answer);

                if((answer == "List Games") || (answer == "List games") || (answer == "list Games") || (answer == "list games"))
                        ListGames();
                else if((answer == "Minesweeper") || (answer == "minesweeper"))
                        LaunchGame("C:\\Windows\\System32\\winmine.exe");
                else if((answer == "Solitare") || (answer == "solitare"))
                        LaunchGame("C:\\Windows\\System32\\sol.exe");
                else if((answer == "Pinball") || (answer == "pinball"))
                        LaunchGame("C:\\Program Files\\Windows NT\\Pinball\\PINBALL.EXE");
        }

        return 0;
}

-Only works on XP I believe.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.