INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Jobs

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

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

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

(OP)
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?

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

In Tcl is everything string. You have to test if string is integer:

CODE

#A simple script that make possible to count from 0 up to given positive number
proc main {} {
  set upTo " "
  while {!([string is integer -strict $upTo] && ($upTo > 0))} {
    puts -nonewline "Insert a positive number: ";
    flush stdout;
    gets stdin upTo;
    # test on integer
    if ![string is integer -strict $upTo] {
      puts "Error: Not an integer !";
    }
    # test on positive
    if {$upTo<=0} {
      puts "Error: Negative Number !";
    }
  }
  # Count
  puts "Counting to \$upTo = '$upTo':"
  for {set count 0} {$count<=$upTo} {incr count} {
    puts "$count";
  }  
}

# execute main
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

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

(OP)
Ok, thanks!

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

(OP)
Ok, i had understood this.

However, thanks.

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members!

Resources

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close