Help with functions
Author |
Message |
NitroN2O
|
Posted: Sat Dec 02, 2017 4:03 pm Post subject: Help with functions |
|
|
What is it you are trying to achieve?
To ask the user if their Input was correct
What is the problem you are having?
How to put result from one function to another
Describe what you have tried to solve this problem
2 hours of searching and testing
Post any relevant code (You may choose to attach the file instead of posting the code if it is too long)
Turing: |
function NamePrompt () : string
var name : string
put "Enter your first name: " ..
get name
result name
end NamePrompt
function Inputs () : boolean
var tempName : string
tempName := NamePrompt ()
result true
end Inputs
function checkInput () : boolean
var checkInfo : string
var tempName : string
loop
put "Is this information Correct?(yes/no): " ..
get checkInfo
if checkInfo = "yes" then
put "Thanks ", tempName, "!" % There should be a name here but I don't know how to get it here
result true
else
tempName := NamePrompt ()
put "Thanks ", tempName, "!"
end if
exit when checkInfo = "yes" or checkInfo = "Yes"
end loop
end checkInput
%%%%%%%%%%%%%%%%%%%%
var InputMain : boolean
var CheckInput : boolean
var magicWord : string
loop
InputMain := Inputs ()
CheckInput := checkInput ()
put "Do you want to re-run the program: " ..
get magicWord
exit when magicWord not= "Yes" and magicWord not= "yes"
end loop
|
Please specify what version of Turing you are using
4.11 |
|
|
|
|
|
Sponsor Sponsor
|
|
|
TokenHerbz
|
Posted: Mon Dec 04, 2017 3:12 am Post subject: RE:Help with functions |
|
|
when you call checkinput, your declare tempname as an empty string, instead of calling a function to do it, thats the issue.
passing functions into functions can be annoying if your not careful, i dont usually think its required in this case as the function should do both the verification, etc, however if you wanted to use it over in other blocks i guess you could do something like this...
dont copy my code but you can use it to solve your problem for school.
code: |
%%a reusable function to CONFIRM things.
function confirmed : boolean
var ans: string := ""
put "Are you sure this is correct? (yes/no)"
get ans
if ans = "yes" then
result true
end if
result false
end confirmed
%%get a valid user name
function getName : string
var name: string := ""
loop
put "please Enter your name"
get name
exit when confirmed
put ""
end loop
result name
end getName
%%get a valid age
function getAge : int
var age : int := 0
loop
put "enter your age"
get age
exit when confirmed and age >= 0
put ""
end loop
result age
end getAge
%%init
var userName : string := getName
var userAge : int := getAge
%%sucess
put "Thank you for your time ", userName, " who is ", userAge
|
|
|
|
|
|
|
|
|