Author Topic: Converting an integer to a string  (Read 6786 times)

0 Members and 1 Guest are viewing this topic.

Offline Cloudee1

Converting an integer to a string
« on: November 03, 2013, 11:06:15 PM »
Ok, so I am generating some random numbers via

Code: [Select]
= number1 Random(1 10)

Then I am asking the user to enter a number

Code: [Select]
Print("Guess:" #at 134 80 #width 50 #font 4 #edit @guess1 3)

The problem I am having is that it appears as though the user input is stored as a string whereas the generated number is coming across as an integer. I have not managed to figure out how to compare the two successfully. Does anyone know how to change number1 from an integer to a string such that I could then use

Code: [Select]
(if(StrCmp(number1 @guess1)) Print("true"))

or instead how to get the user input as an integer so that I can use something like

Code: [Select]
(if(== number1 @guess1) Print("true"))


Halloween Competition Brass Lantern Prop Competition Groundhog Day Competition

Offline Collector

Re: Converting an integer to a string
« Reply #1 on: November 03, 2013, 11:31:32 PM »
I am not sure how you would cast a string to an integer in SCI, but if all else fails, could you do just a series of if statements (if(Said('1')) to have it populate an int variable as 1? If you could step through an input character by character you would only need 10 if statements.
KQII Remake Pic

Offline Cloudee1

Re: Converting an integer to a string
« Reply #2 on: November 03, 2013, 11:32:44 PM »
I love it when 10 seconds after I make a post, I find the answer that I am looking for...

Turns out, there is this: ReadNumber("string") which turns an integer string into an integer. So I can transform the user input into an integer and then compare.

I used it like this

Code: [Select]
= temp1 ReadNumber(@guess1)
(if(== number1 temp1) Print("true"))

Now everything works just as it should

Collector, what you suggested would work if it were only 10 possibilities, but in this case the actual code is generating 6 numbers each between 100 and 999. I just simplified my example I posted here.
« Last Edit: November 03, 2013, 11:34:42 PM by Cloudee1 »
Halloween Competition Brass Lantern Prop Competition Groundhog Day Competition

Offline Collector

Re: Converting an integer to a string
« Reply #3 on: November 03, 2013, 11:35:26 PM »
I'm glad that there is a direct way to cast it to int. Perhaps it would be good to get this into some of the references.
KQII Remake Pic

Offline Cloudee1

Re: Converting an integer to a string
« Reply #4 on: November 03, 2013, 11:39:15 PM »
I actually found it in the studio compiled help file. after spending some time reading about variables and getting nowhere (except for the example of a for loop that I have been wondering about) I finally tried a search for "convert" and there it was.
Halloween Competition Brass Lantern Prop Competition Groundhog Day Competition

Offline Collector

Re: Converting an integer to a string
« Reply #5 on: November 04, 2013, 12:28:13 AM »
I found the entry you mentioned, but was curious about its implantation. Can it throw an exception if it tries to cast a string that cannot be cast to int? What does it do with non number characters in a string or invalid hex numbers? What if the user spells out the number instead of entering integers?
KQII Remake Pic

Offline Cloudee1

Re: Converting an integer to a string
« Reply #6 on: November 04, 2013, 08:56:34 AM »
Well, even though I have already figured this one out, I have figured it out better. In light of Collector's questions about invalid input for conversion I have taken a different approach and have managed to validate the user input at the time of entry.

Using GetNumber, prompts for user entry. If text is entered or the prompt left blank, then -1 is returned otherwise the number entered is returned. In my case, I also needed the number to be between two numbers so I simply added a simple greater than and less than check to make sure the number entered falls between. In my case the user must enter 6 numbers so each number prompt is in its own case of a changeState. If the number entered is valid, not -1 and between 100 and 999 then move on to the next case, if it is not valid, redo the current case and do the same prompt again.

Code: [Select]
(case 5
   = uinput1 GetNumber("Pick #1")
   (if((> uinput1 100) and (< uinput1 999))
      (buyingTicket:changeState(6))
   )
   (else
      (buyingTicket:changeState(5))
   )
)
Halloween Competition Brass Lantern Prop Competition Groundhog Day Competition


SMF 2.0.19 | SMF © 2021, Simple Machines
Simple Audio Video Embedder

Page created in 0.044 seconds with 22 queries.