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>
usingnamespace 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"
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:
#include "StdAfx.h"
#include <iostream>
#include <string>
usingnamespace 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\"");
}
elseif (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
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().
#include "StdAfx.h"
#include <iostream>
#include <string>
usingnamespace 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\"");
}
elseif (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.