|
-
Sep 12th, 2007, 02:33 PM
#1
Thread Starter
Hyperactive Member
[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?
Last edited by Bobalandi; Sep 13th, 2007 at 08:51 PM.
Haikus are easy.
But sometimes they don't make sense.
Refrigerator.
-
Sep 12th, 2007, 04:52 PM
#2
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
Visual Studio 6, Visual Studio.NET 2005, MASM
-
Sep 12th, 2007, 05:41 PM
#3
Thread Starter
Hyperactive Member
Re: Require Correct Input and execute bat
Haikus are easy.
But sometimes they don't make sense.
Refrigerator.
-
Sep 12th, 2007, 05:43 PM
#4
Thread Starter
Hyperactive Member
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?
Haikus are easy.
But sometimes they don't make sense.
Refrigerator.
-
Sep 12th, 2007, 08:04 PM
#5
Thread Starter
Hyperactive Member
Re: [RESOLVED] Require Correct Input and execute bat
umm... this doesnt work, no matter what you put, the answer is incorrect....
Haikus are easy.
But sometimes they don't make sense.
Refrigerator.
-
Sep 12th, 2007, 09:57 PM
#6
Re: [RESOLVED] Require Correct Input and execute bat
Visual Studio 6, Visual Studio.NET 2005, MASM
-
Sep 13th, 2007, 03:11 PM
#7
Thread Starter
Hyperactive Member
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;
}
Haikus are easy.
But sometimes they don't make sense.
Refrigerator.
-
Sep 13th, 2007, 04:39 PM
#8
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
Visual Studio 6, Visual Studio.NET 2005, MASM
-
Sep 13th, 2007, 05:20 PM
#9
Thread Starter
Hyperactive Member
Re: [RESOLVED] Require Correct Input and execute bat
hmmm, string desnt even work with the compiler..
Haikus are easy.
But sometimes they don't make sense.
Refrigerator.
-
Sep 13th, 2007, 06:58 PM
#10
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
Visual Studio 6, Visual Studio.NET 2005, MASM
-
Sep 13th, 2007, 07:11 PM
#11
Thread Starter
Hyperactive Member
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...
Last edited by Bobalandi; Sep 13th, 2007 at 07:38 PM.
Haikus are easy.
But sometimes they don't make sense.
Refrigerator.
-
Sep 13th, 2007, 09:03 PM
#12
Thread Starter
Hyperactive Member
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?
Haikus are easy.
But sometimes they don't make sense.
Refrigerator.
-
Sep 13th, 2007, 09:49 PM
#13
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
Visual Studio 6, Visual Studio.NET 2005, MASM
-
Sep 14th, 2007, 05:45 AM
#14
Thread Starter
Hyperactive Member
Re: [Help Needed] Require Correct Input and execute bat
no, just once, and then end the program...
Haikus are easy.
But sometimes they don't make sense.
Refrigerator.
-
Sep 14th, 2007, 06:56 AM
#15
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
Visual Studio 6, Visual Studio.NET 2005, MASM
-
Sep 14th, 2007, 02:09 PM
#16
Thread Starter
Hyperactive Member
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?
Haikus are easy.
But sometimes they don't make sense.
Refrigerator.
-
Sep 14th, 2007, 06:46 PM
#17
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
Visual Studio 6, Visual Studio.NET 2005, MASM
-
Sep 14th, 2007, 06:51 PM
#18
Thread Starter
Hyperactive Member
Re: [Help Needed] Require Correct Input and execute bat
ok, I'll check the file, thanks for all your help...
Haikus are easy.
But sometimes they don't make sense.
Refrigerator.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|