Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

input a char or a string but read a number: strange script behaviour

Status
Not open for further replies.

paskali

Programmer
Apr 6, 2012
13
IT
Hi all, i am new on this forum and i am "almost" new about tcl/tk.
OK, my question is simple:
i have write a simple script to count from 0 to any positive number; please read following:

#A simple script that make possible to count from 0 up to given positive number
proc main {} {
puts -nonewline "Insert a positive number: ";
flush stdout;
gets stdin upTo;
if {$upTo<=0} {
puts "Error";
main;
}
for {set count 0} {$count<=$upTo} {incr count} {
puts "$count";
}
}
main;

If i make an error and insert a negative number, it tell me "error" then repeat the main proc again but if i insert a char, or a string, it does not tell me any error and it counts apparently up to infinity. Why?
 
In Tcl is everything string. You have to test if string is integer:
Code:
[COLOR=#0000ff]#A simple script that make possible to count from 0 up to given positive number[/color]
[COLOR=#804040][b]proc[/b][/color] main {} {
  [COLOR=#804040][b]set[/b][/color] upTo [COLOR=#ff00ff]" "[/color]
  [COLOR=#804040][b]while[/b][/color] {!([[COLOR=#804040][b]string[/b][/color] is integer -strict [COLOR=#008080]$upTo[/color]] && ([COLOR=#008080]$upTo[/color] > [COLOR=#ff00ff]0[/color]))} {
    [COLOR=#804040][b]puts[/b][/color] -nonewline [COLOR=#ff00ff]"Insert a positive number: "[/color];
    [COLOR=#804040][b]flush[/b][/color] stdout;
    [COLOR=#804040][b]gets[/b][/color] stdin upTo;
[COLOR=#0000ff]    # test on integer[/color]
    [COLOR=#804040][b]if[/b][/color] ![[COLOR=#804040][b]string[/b][/color] is integer -strict [COLOR=#008080]$upTo[/color]] {
      [COLOR=#804040][b]puts[/b][/color] [COLOR=#ff00ff]"Error: Not an integer !"[/color];
    }
[COLOR=#0000ff]    # test on positive[/color]
    [COLOR=#804040][b]if[/b][/color] {[COLOR=#008080]$upTo[/color]<=[COLOR=#ff00ff]0[/color]} {
      [COLOR=#804040][b]puts[/b][/color] [COLOR=#ff00ff]"Error: Negative Number !"[/color];
    }
  }
[COLOR=#0000ff]  # Count[/color]
  [COLOR=#804040][b]puts[/b][/color] [COLOR=#ff00ff]"Counting to [/color][COLOR=#6a5acd]\$[/color][COLOR=#ff00ff]upTo = '[/color][COLOR=#008080]$upTo[/color][COLOR=#ff00ff]':"[/color]
  [COLOR=#804040][b]for[/b][/color] {[COLOR=#804040][b]set[/b][/color] count [COLOR=#ff00ff]0[/color]} {[COLOR=#008080]$count[/color]<=[COLOR=#008080]$upTo[/color]} {[COLOR=#804040][b]incr[/b][/color] count} {
    [COLOR=#804040][b]puts[/b][/color] [COLOR=#ff00ff]"[/color][COLOR=#008080]$count[/color][COLOR=#ff00ff]"[/color];
  }  
}

[COLOR=#0000ff]# execute main[/color]
main;
Code:
C:\Users\Roman\Work>tclsh zemir.tcl
Insert a positive number: a
Error: Not an integer !
Insert a positive number: -5
Error: Negative Number !
Insert a positive number: 0
Error: Negative Number !
Insert a positive number: 5
Counting to $upTo = '5':
0
1
2
3
4
5
 
Ok, i had understood this.

However, thanks.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top