SRL Archive - Help

Fast queston


rj

2012-12-19 22:39:31

I am using input querey to ask for a value, how do i made it ask for a integer rather then a string? The code I am using:
Procedure  AskGuess;
begin
If (InputQuery('Guess number', 'Number', Guess)) Then
begin
writeln(Guess);
end;
end;

DannyRS

2012-12-19 22:47:57

WriteIn(IntToStr(''Guess'));

I'm not 100% sure i understand your problem though

riwu

2012-12-19 22:48:23

You can convert the answer to integer with StrToIntDef().

rj

2012-12-19 22:50:30

From: riwu
You can convert the answer to integer with StrToIntDef().


didn't work :9
[Error] C:\Simba\Scripts\Guessinggame.simba(10:50): Type mismatch at line 9
Compiling failed.

riwu

2012-12-19 22:52:52

From: rjj95
didn't work :9
[Error] C:\Simba\Scripts\Guessinggame.simba(10:50): Type mismatch at line 9
Compiling failed.

What did you do with it? Post the code.

rj

2012-12-19 22:54:48

From: riwu
What did you do with it? Post the code.


Program Game;
const
RANDOM_PICK = 100;
var
Number,Tries,GuessInt:Integer;
Response,Guess:String;
Procedure AskGuess;
begin
If (InputQuery('Guess number', 'Number', Guess)) Then
begin
Writeln(IntToStr(Guess));
end;
end;
Procedure PickNumber;
begin
Number := Random(RANDOM_PICK);
Writeln('' + IntToStr(Round(Number)))
AskGuess;
end;
Begin
PickNumber;
end.

riwu

2012-12-19 22:56:02

From: rjj95
Program Game;
const
RANDOM_PICK = 100;
var
Number,Tries,GuessInt:Integer;
Response,Guess:String;
Procedure AskGuess;
begin
If (InputQuery('Guess number', 'Number', Guess)) Then
begin
WriteIn(IntToStr('Guess));
end;
end;
Procedure PickNumber;
begin
Number := Random(RANDOM_PICK);
Writeln('' + IntToStr(Round(Number)))
AskGuess;
end;
Begin
PickNumber;
end.

lol…you read a post not by me and thought it's me..


From: DannyRS
WriteIn(IntToStr(''Guess'));

I'm not 100% sure i understand your problem though

See? I didn't post that.
It throws a type mismatch because Guess is already a string, and not an integer.

rj

2012-12-19 22:58:59

Yeilds the same result

Program Game;
const
RANDOM_PICK = 100;
var
Number,Tries,GuessInt:Integer;
Response,Guess:String;
Procedure AskGuess;
begin
If (InputQuery('Guess number', 'Number', Guess)) Then
begin
Writeln(IntToStr(Guess));
end;
end;
Procedure PickNumber;
begin
Number := Random(RANDOM_PICK);
Writeln('' + StrToIntDef(Guess);
AskGuess;
end;
Begin
PickNumber;
end.

riwu

2012-12-19 23:02:06

From: rjj95
Yeilds the same result


Writeln(IntToStr(Guess));

Because you still have this.
Also if you read StrToIntDef have 2 parameters. Use StrToInt() if you do not want a default value. (which has its consequence as the result being ambiguous if input = 0)

rj

2012-12-19 23:05:49

From: riwu
Because you still have this.
Also if you read StrToIntDef have 2 parameters. Use StrToInt() if you do not want a default value. (which has its consequence as the result being ambiguous if input = 0)


got it thanks!

Wardancer

2012-12-19 23:13:11

Erm… IntToStr stands for Integer to String. In other words, it converts an integer value into a string one. There is no need to (actually you can't) use IntToStr on a string. You'll get a type mismatch error. On this case, you already specified Guess as a string so there is no need to transform it as writeln writes…strings.

riwu

2012-12-19 23:32:08

From: Wardancer
you already specified Guess as a string so there is no need to transform it as writeln writes…strings.

Writeln does not just write strings ;)

Wardancer

2012-12-19 23:38:21

From: riwu
Writeln does not just write strings ;)


Kept it simple, but I wonder if we could argue that it only writes strings. Some variables just happen to be strings plus other things! =).

riwu

2012-12-19 23:40:54

From: Wardancer
Kept it simple, but I wonder if we could argue that it only writes strings. Some variables just happen to be strings plus other things! =).

Array of variants? It can write that directly as well.
I'd consider writeln(1) simpler than writeln(IntToStr(1));

Wardancer

2012-12-19 23:45:12

From: riwu
'strings plus other things!'? array of variants? It can write that directly as well.
I'd consider writeln(1) simpler than writeln(IntToStr(1));


My point was much more rhetoric than technical, seeing variant as "integers + strings", therefore it isn't virtually incorrect to say that writeln only writes strings. Awful joke coming from a sleep deprived person.

That being said, you're totally right that the OP should use variant instead of string or integer.

riwu

2012-12-19 23:46:52

From: Wardancer
My point was much more rhetoric than technical, seeing variant as "integers + strings", therefore it isn't virtually incorrect to say that writeln only writes strings. Awful joke coming from a sleep deprived person.

That being said, you're totally right that the OP should use variant instead of string or integer.

huh…i still don't get why you say writeln writes only strings and why variant should be used…

Wardancer

2012-12-19 23:59:11

From: riwu
huh…i still don't get why you say writeln writes only strings and why variant should be used…


The part about string is mostly rhetorical and brings nothing to this thread. Not worth trying to explain it as it was just a joke to start with and I'd rather not confuse anyone with an argument that, at the end of the day, doesn't help.

As for the suggestion regarding variant, my understanding is that it converts the variable to the type needed for the particular use. I might be wrong though as you suggested using an array of variant and I never played around with this type of variable before.

Brandon

2012-12-20 00:00:44

From: riwu
huh…i still don't get why you say writeln writes only strings and why variant should be used…



He's partially correct. Writeln uses implicit conversion to string I think?

writeln(int);  //probably converts that int to a string first then prints.


I don't think there is any function out there, in any language, that can just print the string/literal equivalent of something without some sort of lexical casting somewhere along the lines.

It's probably why Implicit concatenation doesn't happen:

writeln("Foo: " + someint);  //compile time error.

but explicit does:


writeln("Foo: " + ToStr(someint));

Some languages have explicit concat or aren't type strict.