[Help Needed] Require Correct Input and execute bat
Is there a way to require a person to enter a certain text to get it to continue like:
Code:
const int size = 5;
char enter[size];
cout << "enter hello";
cin >> enter
and then get it to continue if the text was entered correctly, also, is there a way to shell a bat file?
Re: Require Correct Input and execute bat
..yes.
Code:
while (true)
{
cout << "Enter Hello" << endl;
cin >> hello;
if (hello == "hello")
break;
cout << "Incorrect" << endl;
}
Windows.h has the definition of the ShellExecute API. That will 'shell a bat file' for you.
chem
Re: Require Correct Input and execute bat
Re: [RESOLVED] Require Correct Input and execute bat
Whoops, forgot to ask, is this a function, or should I just add this in the main code?
Re: [RESOLVED] Require Correct Input and execute bat
umm... this doesnt work, no matter what you put, the answer is incorrect.... :(
Re: [RESOLVED] Require Correct Input and execute bat
Re: [RESOLVED] Require Correct Input and execute bat
Here you go:
Code:
#include <iostream>
#include <windows.h>
using namespace std;
int main()
{
while (true)
{
cout << "Please enter the correct password: ";
char pass[9];
cin >> pass;
if (pass == "Bobalandi")
break;
cout << "Wrong Password...\n";
}
ShellExecute(0,"OPEN","me.bat","","",SW_NORMAL);
cin.get();
return 0;
}
Re: [RESOLVED] Require Correct Input and execute bat
Try using a string:
Code:
// char pass[9];
string pass;
Remember, when you type the "password" in.. its case-sensitive.
chem
Re: [RESOLVED] Require Correct Input and execute bat
hmmm, string desnt even work with the compiler..
Re: [RESOLVED] Require Correct Input and execute bat
Oh.. you need to include <string>.
I think its best if you research the Standard Library and the most useful/common containers and types.
chem
Re: [RESOLVED] Require Correct Input and execute bat
Edit: It works with string, but now I am using this code to execute a batch, but it isnt working... any ideas?
c++ Code:
int main()
{
while (true)
{
cout << "Please enter the correct password: ";
string pass;
cin >> pass;
if (pass == "Bobalandi")
break;
cout << "Wrong Password...\n";
ShellExecute(0,"OPEN","badpass.bat","","",SW_NORMAL);
}
ShellExecute(0,"OPEN","me.bat","","",SW_NORMAL);
cin.get();
return 0;
}
When it is correct it works, but the incorrect one doesn't...
Re: [Help Needed] Require Correct Input and execute bat
One more question, is it possible to to shellexecute that .bat in the same window as the commandline app that it is in?
Re: [Help Needed] Require Correct Input and execute bat
No thats not possible. Well.. anything is possible, but what you're talking about is extremely low-level if possible..
What are you trying to do exactly.. ? Do you want that badpass batch file to execute every single time the user enters a bad password?
chem
Re: [Help Needed] Require Correct Input and execute bat
no, just once, and then end the program...
Re: [Help Needed] Require Correct Input and execute bat
You don't need a while loop for that then...
C++ Code:
#include <windows.h>
#include <iostream.h>
#include <string>
int main()
{
std::string pass;
std::cout << "Enter your password: ";
std::cin >> pass;
if (pass == "Bobalandi")
ShellExecute(0, "OPEN", "badpass.bat", "", "", SW_NORMAL);
else
ShellExecute(0, "OPEN", "me.bat", "", "", SW_NORMAL);
}
...
Have you tried putting the full path to the batch files in there?
chem
Re: [Help Needed] Require Correct Input and execute bat
well, the problem, is that it is on a usb drive... so the path is dynamic, also, the me.bat works, but the other one doesnt...
PS. Is there a way to get the bat file to execute from the same window as this console app?
Re: [Help Needed] Require Correct Input and execute bat
No you can't get it to execute from inside the console application. Its a completely different environment. If you make a console application that just lets the user type stuff in, and the user types "cd C:".. nothing will happen.
Are you sure its not a problem with your batch file then? Because the code above works for me fine..
chem
Re: [Help Needed] Require Correct Input and execute bat
ok, I'll check the file, thanks for all your help...