just questions
Moderator: DosItHelp
-
- Posts: 6
- Joined: 26 Nov 2011 11:05
just questions
I'm trying to make a game using DOS code, notepad, and a .bat extension.
How do I prevent the program from closing when the the user types in a space?
How do I set a single variable to echo something on multiple lines? Like this...
variable=(
echo blah blah
echo yadda yadda
)
And how do I use the goto command to go to a certain line?
How do I prevent the program from closing when the the user types in a space?
How do I set a single variable to echo something on multiple lines? Like this...
variable=(
echo blah blah
echo yadda yadda
)
And how do I use the goto command to go to a certain line?
Re: just questions
some examples:
would output:
to use GOTO, use labels.
would output:
Code: Select all
set cat=13
set dog=7
echo %dog%
echo %cat%
would output:
Code: Select all
13
7
to use GOTO, use labels.
Code: Select all
:loop
echo Hello world
pause
goto loop
would output:
Code: Select all
Hello World
Press any key to continue...
-
- Posts: 6
- Joined: 26 Nov 2011 11:05
Re: just questions
Thank you. But whenever I use "set/p input" and type in a space, the window closes. why?
Re: just questions
'
Code: Select all
@echo off
set /p "$input=input :"
echo.$input=%$input%_
pause
exit /b 0
-
- Posts: 6
- Joined: 26 Nov 2011 11:05
Re: just questions
Okay. Now I'm trying to set "choice1" to "look around" (no quotes.)
I keep getting an error and then it closes.
I keep getting an error and then it closes.
Re: just questions
QUESTION 1:PART 1: I can see that you are a beginner, so I will phrase this simply:
VARIABLES WITH PAUSING=
So if you want the user to type in something, you can use set /p to get them to type in something without the file closing suddenly.
For example:
It will freeze until the user types in something, and cmd will set what the user types to the value of the variable.
PART 2: You could use the "pause" command to stop it closing until the user presses any key:
Or if you don't want it to show up as "press any key to continue...":
QUESTION 2: You could do this:
QUESTION 3: You need to set tag/s at certain point/s in a batch file. For example:
This should answer your questions.
VARIABLES WITH PAUSING=
Code: Select all
set /p (random character that you want to represent your variable)=(something the user will see)
So if you want the user to type in something, you can use set /p to get them to type in something without the file closing suddenly.
For example:
Code: Select all
set /p a=Enter a number:
It will freeze until the user types in something, and cmd will set what the user types to the value of the variable.
PART 2: You could use the "pause" command to stop it closing until the user presses any key:
Code: Select all
pause
Or if you don't want it to show up as "press any key to continue...":
Code: Select all
pause >nul
QUESTION 2: You could do this:
Code: Select all
set "a=(text. But make sure that whatever goes here is on the amount of lines that you want.)"
REM Such as set "a= echo hello (
REM) echo hello world (
REM ) echo hello world again"
%a%
QUESTION 3: You need to set tag/s at certain point/s in a batch file. For example:
Code: Select all
REM place a ":" before a letter or word to indicate that it is a tag.
:a
echo Hi
pause >nul
goto :a
REM The program will jump to :a. This simple batch file is called a loop.
This should answer your questions.
-
- Expert
- Posts: 442
- Joined: 01 Aug 2010 17:13
- Location: Canadian Pacific
- Contact:
Re: just questions
benneburger wrote:How do I prevent the program from closing when the the user types in a space?
You need to watch the handling of special characters in batch. Characters within quotation marks are "deactivated", and your script needs to prepare for blank variables (or those containing only a space like what you're trying). & | % ! to name a few can all cause problems.
The problem you are most likely having:
Code: Select all
set /p "var=Enter Number: "
if %var%==1 (echo:Var is 1.) else echo:Var is 2.
If the user enters a space, it reads
Code: Select all
if ==1
That's going to cause a syntax error. Run your scripts from an open command line so you can see the errors it outputs. Solution, as I said:
Code: Select all
set /p "var=Enter Number: "
if "%var%"=="1" (echo:Var is 1.) else echo:Var is 2.
benneburger wrote:How do I set a single variable to echo something on multiple lines?
Code: Select all
set "var=echo:Hello&echo:World&echo:Orange"
:: Or...
set var=echo:Hello^&echo:World^&echo:Orange
%var%
^ (caret) is the escape character in batch. & joins multiple commands on the same line. If it's not within quotation marks, it will cause an error without an escape caret because command prompt will interpret it as-is.
Code: Select all
set var=echo:Hello&echo:World&echo:Orange
Will read as...
Code: Select all
set var=echo:Hello
echo:World
echo:Orange
benneburger wrote:And how do I use the goto command to go to a certain line?
Make a label where you want to go, then write goto followed by that label's name.
Code: Select all
:start
echo:Hello
goto start
If you mean a specific line number, that's complicated and beyond your skill level, and also just a terrible and pointless idea anyway, because as soon as you add a new line between somewhere, every reference would be broken.
-
- Posts: 6
- Joined: 26 Nov 2011 11:05
Re: just questions (SOLVED)
Through thorough experimentation that stretched my 15 yr old mind to its limits, I have solved my problem using this code:
set /p "$input= "
set $input="%$input%"
With this, the user can enter as many words and spaces as they desire with no issue.
I'm so proud of myself, I can almost jump for joy!
set /p "$input= "
set $input="%$input%"
With this, the user can enter as many words and spaces as they desire with no issue.
I'm so proud of myself, I can almost jump for joy!
-
- Posts: 6
- Joined: 26 Nov 2011 11:05
Re: just questions
Thank you all. I hereby declare this thread done.
-
- Expert
- Posts: 442
- Joined: 01 Aug 2010 17:13
- Location: Canadian Pacific
- Contact:
Re: just questions
Yes, that's one way of being able to use your %$input% variable without continually having to place it between quotation marks. Just watch your handling because now those quotation marks are part of that variable, and you'll need to write %$input:~1,-1% to remove them without modifying the variable. For longer scripts you should keep note of such things. It's a little complicated for novices. 
