Launching a program

Hey guys, I am extremely new to C++, I used to code vb.net and moved onto C#, but I didn't enjoy it as it felt extremely close to vb.net, so I moved on to C++. That being said I have come across a problem, I am trying to make a console application (CLR) that prints a certain message and then launches an application here is my source code so far.

1
2
3
4
5
6
7
8
9
10
11
12
#include "StdAfx.h"
#include <cstdlib>
#include <iostream>
using namespace std;

int main ()
{
  cout << "lol filler text thats all this shit is";
  system("'C:\\Program Files\\Conquer Online 2.0\\play.exe'"); //launch the program?
  system("pause");
  return 0; 
}	

Is that right? Am I leaving something out? Thanks for your feedback guys. Oh and also I forgot to say, could you help me understand how I would make it say Would you like to play now? then you type yes and it launches the program, if you say no it closes the application?
cout << "Would you like to play now?" instead of cout << "lol filler text thats all this shit it"
Last edited on
Only a slight change:

 
system("\"C:\\Program Files\\Conquer Online 2.0\\play.exe\"");


Now with the escaped characters, Windows see this:

"C:\Program Files\Conquer Online 2.0\play.exe"

...and should execute the program as if you had typed it at the command line.

To ask the user, first create a char array to hold the answer (actually a string is the safest way):

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#include <iostream>
#include <string>
using namespace std;

int main ()
{
    string input;

    cout << "Would you like to play?\n";
    cin >> input;
    if (input == "yes")
    {
        system("\"C:\\Program Files\\Conquer Online 2.0\\play.exe\"");
    }

    system("pause");
    return 0; 
}


Last edited on
can I add a else close? or how can I completely kill the application? like so:
1
2
3
4
5
6
7
8
 if (input == "Yes" || input == "yes")
    {
        system("\"C:\\Program Files\\Conquer Online 2.0\\play.exe\"");
    } 
           else (input == "No" || input == "no") 
    { 
        //Close command here 
    { 


Would something like that work?
To completely kill the application from any function, you can use exit(). Some consider it bad form, but it works.

-Albatross
ok, so something like
1
2
3
4
5
6
7
8
 if (input == "Yes" || input == "yes")
    {
        system("\"C:\\Program Files\\Conquer Online 2.0\\play.exe\"");
    } 
           else (input == "No" || input == "no") 
    { 
        return 0;
    {  
?
You don't use any logic after an "else" statement. Only after "else if" do you need logic. Also, the problem with the approach you posted is that, even if you use "else if" there, you don't handle the case in which the user enters something completely different. What if the user types, "Maybe"?

You can either:

1) Execute the program after "yes" or "Yes", and consider all other input to mean "No" by defaut:

1
2
3
4
5
6
7
8
9
10
11
12
cin >> input;

if (input == "Yes" || input == "yes")
{
    system("\"C:\\Program Files\\Conquer Online 2.0\\play.exe\"");
}
else
{
    return 0;
}

//... rest of program ... 


or 2) Check for valid input before doing anything at all:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
cin >> input;

do
{
    if (input == "Yes" || input == "yes")
    {
        system("\"C:\\Program Files\\Conquer Online 2.0\\play.exe\"");
    }
    else if (input == "No" || input == "no")
    {
        return 0;
    }
    else 
    {
        cout << "Invalid input.  Please try again:\n";
        cin >> input;
        continue; //Check again
    }     
}
while (false);
Last edited on
so essentially:

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
 #include "StdAfx.h"
#include <iostream>
#include <string>
using namespace std;

int main (){
    string input;

    cout << "Would you like to play?\n";
cin >> input;

while (false)
{
    if (input == "Yes" || input == "yes")
    {
        system("\"C:\\Program Files\\Conquer Online 2.0\\play.exe\"");
    }
    else if (input == "No" || input == "no")
    {
        return 0;
    }
    else 
    {
        cout << "Invalid input.  Please try again:\n";
        cin >> input;
        continue; //Check again
    }     
} 


[EDIT:
When I use the above code I get this error
PracticeProgram.cpp(30): fatal error C1075: end of file found before the left brace '{' at 'PracticeProgram.cpp(7)' was matched
Last edited on
Oh... by the way... is there any way you can get rid of those system() calls? Even if you're using a platform-specific function, it's typically a better idea than system().

-Albatross, now in HD!
Last edited on
Ditto on Albatross' comment.

That error is just because you didn't fully close the main() function. Here:

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
32
33
34
35
36
#include "StdAfx.h"
#include <iostream>
#include <string>
using namespace std;

int main ()
{    
    string input;

    cout << "Would you like to play?\n";
    cin >> input;

    do
    {
        if (input == "Yes" || input == "yes")
        {
            system("\"C:\\Program Files\\Conquer Online 2.0\\play.exe\"");
        }
        else if (input == "No" || input == "no")
        {
            return 0;
        }
        else 
        {
            cout << "Invalid input.  Please try again:\n";
            cin >> input;
            continue; //Check again
        }     
    } 
    while (false);

    cin.get(); //Alternative to using system("pause") - yes, two of them.
    cin.get();

    return 0;
}


Also I see you've mistakenly used a "while" instead of "do-while". I changed that in my post almost immediately but you must have copied it very quickly! Anyway it needs to be do-while in this case.

Last edited on
It works! Thanks everyone for your support and help!
Topic archived. No new replies allowed.