I don't know what's wrong with this code I'm running on Just BASIC - basic

[Start]
n=INT((RND*10)+1)
PRINT "I have thought of a number."
PRINT "Try to guess it!"
PRINT "Enter your guess."
INPUT g
[Right]
IF g=n THEN GOTO [Finish]
**IF g < n PRINT "Your guess was wrong. Try again. The correct answer was "; n;"." THEN GOTO [Start]
IF g > n PRINT "Your guess was wrong. Try again! The correct answer was "; n;"." THEN GOTO [Start]**
[Finish]
PRINT "Well done! You guessed it."
END

IF condition THEN positive ELSE negative
However, this
IF g > n THEN PRINT "Your guess was wrong. Try again! The correct answer was "; n;"."; GOTO [Start]
Won't do what you want because the IF...THEN...ELSE sentence ends after the print and subsequent GOTO will run in either case.
In most BASICs, you can do this:
IF g > n THEN
PRINT "Your guess was wrong. Try again! The correct answer was "; n;".";
ELSE
GOTO Start
END IF
But I'm not sure about your particular flavor.

Although it could be wrutten with less GOTOs, this should work too:
[Start]
n=INT((RND*10)+1)
PRINT "I have thought of a number."
PRINT "Try to guess it!"
PRINT "Enter your guess."
INPUT g
[Right]
IF g=n THEN GOTO [Finish]
PRINT "Your guess was wrong. Try again. The correct answer was "; n;"."
GOTO [Start]
[Finish]
PRINT "Well done! You guessed it."
END

Related

the tax calculation inside bash function giving error: syntax error in expression

code is not working. I think this might have to do with the floating point in the constant " TAX_FACTOR=0.0070. The code is:
ShowTax() {
local TAX_FACTOR=0.0070
echo "Enter the property's value."
read propertyValue
tax=$((expr $propertyValue*$TAX_FACTOR )) #there is a problem
echo "The property tax is:" "$tax"
}
# Main
echo " Enter lot number of property or 0 to end"
read lotNumber
while(( $lotNumber != 0 ));do
showTax
echo "Enter the lot number for the next property or 0 to end"
read lotNumber
done

Check if userinput contains substring

Hello please help me with this one!
I would like to check if the user input URL contains the defined SUBSTRING or not.
If yes I would like to GOTO LONG else GOTO SHORT
Thank you!
#echo off
setlocal enabledelayedexpansion enableextensions
SET /P "URL= Input the link of the video: "
SET "SUBSTRING=?filter=archives&sort=time"
ECHO !URL! | FINDSTR /C:"!SUBSTRING!">nul
IF ERRORLEVEL 1 (GOTO SHORT) ELSE GOTO LONG
:LONG
SET LINK=1
ECHO THIS IS A LONG LINK
ECHO "THE LINK NUMBER IS %LINK%"
ECHO !URL!
GOTO END
:SHORT
SET LINK=0
ECHO THIS IS A SHORT LINK
ECHO "THE LINK NUMBER IS %LINK%"
ECHO !URL!
GOTO END
:END
pause
Using double quotes properly helps fix some of your problems. There shouldn't be any need to use delayed expansion if you use quotes for the echo as well.
#echo off
SET /P "URL=Input the link of the video:"
SET "SUBSTRING=?filter=archives"
ECHO "%URL%"|FINDSTR /C:"%SUBSTRING%">nul
IF ERRORLEVEL 1 (GOTO SHORT) ELSE GOTO LONG
:LONG
SET LINK=1
ECHO THIS IS A LONG LINK
ECHO "THE LINK NUMBER IS %LINK%"
GOTO END
:SHORT
SET LINK=0
ECHO THIS IS A SHORT LINK
ECHO "THE LINK NUMBER IS %LINK%"
GOTO END
:END
pause
Update Showing execution of batch file.
C:\Users\Squashman\Desktop>test.bat
Input the link of the video:https://www.twitch.tv/videos/578427308?filter=archives&sort=time
THIS IS A LONG LINK
"THE LINK NUMBER IS 1"
Press any key to continue . . .
So this is the working code. I will now mention the problems I had so others can learn from it
I must not enable setlocal enabledelayedexpansion
I have to use quotes around variables for the SET command
Because I have delayedexpansion disabled I must use percentages instead of exclamation marks when I want to use a variable in a command later
Correct me if I am wrong but thats what I figured out with these guys' help and on my own by trial and error
I made this list and updated the code so if a newbie like me gets stuck they can have a look at this post.
Once again thank you guys!
#echo off
SET /P "URL=Input the link of the video:"
SET "SUBSTRING=?filter=archives"
ECHO "%URL%"|FINDSTR /C:"%SUBSTRING%">nul
IF ERRORLEVEL 1 (GOTO SHORT) ELSE GOTO LONG
:LONG
SET LINK=1
ECHO THIS IS A LONG LINK
ECHO "THE LINK NUMBER IS %LINK%"
GOTO END
:SHORT
SET LINK=0
ECHO THIS IS A SHORT LINK
ECHO "THE LINK NUMBER IS %LINK%"
GOTO END
:END
pause

I tried to press Ctrl D in script and I broke it?

I write a simple script with an infinite while loop to test something. Here is the script:
#!/bin/bash
while true; do
echo -n "Give a number: "; read number
done
Then run my script and it was still running, I was pressed Ctrl+D and it broked!
Here is the output:
user#DESKTOP:~$ ./test.sh
Give a number: Give a number: Give a number: Give a number: Give a number: Give a number: Give a number: Give a number: Give a number: Give a number: Give a number: Give a number: Give a number: Give a number: Give a number: Give a number: Give a number: Give a number: Give a number: Give a number: Give a number: Give a number: ^C
How can I prevent this? Thank you...
You're looking to break out of the loop on read failure:
read number || break;
When read detects an error, it returns non-zero, which, in the shell, is "false". The || will see the false on the left, and thus proceed to evaluate the right, but when the read succeeds, returning zero, the || will not evaluate the right, allowing the loop to continue.
An elegant way to solve this problem is:
while read -p 'Give a number: ' number; do
...
done
If the user closes stdin using Ctrl+D then the read command exits with non-zero status which causes to loop to exit.
An additional echo -n is not necessary; read can prompt on its own using the -p option.

Comparing Input to Strings

So I'm writing a relatively simple program that prompts the user for a command, add, subtract, etc, and then prompt for numbers to complete that operation. Everything is written and it compiles fine, but when I enter a command in (add, subtract, etc.) it isn't comparing it properly. Instead of entering the operation branch of the if case, it goes to the invalid command catch I added. Here is part of the code that contains the declaration and the first if statement.
my $command = <STDIN>;
my $counter = 1;
#perform the add operation if the command is add
if (($command eq 'add') || ($command eq 'a')){
my $numIn = 0;
my $currentNum = 0;
#While NONE is not entered, input numbers.
while ($numIn ne 'NONE'){
if($counter == 1){
print "\nEnter the first number: ";
}else{
print "\nEnter the next number or NONE to be finished.";
}
$numIn = <STDIN>;
$currentNum = $currentNum + $numIn;
$counter++;
}
print "\nThe answer is: #currentNum \n";
#perform the subtract operation if the command is subtract
}`
Does anyone know why if I enter in add it skips this?
$command probably still has the new line attached to it, so eq will fail. because "add" != "add\n"
You might consider just checking the first letter of your command, say with a regular expression
$command =~ /^a/i
or use chop on $command to remove the last character.
chop($command)

How to exit main in haskell given a condition

I have a main function that does a lot of IO. At one point, however, I want to check a variable like not (null shouldBeNull) exit the whole program, without continuing, with a linux exitcode 1 and output an error message.
I've tried playing around with error "..." like putting that in an if:
if (not (null shouldBeNull)) error "something bad happened" else putStrLn "ok"
but I get a parse error (possibly incorrect indentation) :(.
Here's an altered snippet.
main :: IO ExitCode
main = do
--Get the file name using program argument
args <- getArgs
file <- readFile (args !! 0)
putStrLn("\n")
-- ... (some other io)
-- [DO A CHECK HERE], exit according to check..
-- ... (even more io)
echotry <- system "echo success"
rmtry <- system "rm -f test.txt"
system "echo done."
As you may notice, I want to do the check where I've put [DO A CHECK HERE] comment above...
Thanks for your help!
The parse error is because you're missing the then keyword in your if expression.
if condition then truePart else falsePart
For exiting, a more appropriate choice than error might be to use one of the functions from System.Exit, for example exitFailure.
So for example,
if not $ null shouldBeNull
then do putStrLn "something bad happened"
exitFailure
else putStrLn "ok"

Resources