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!

float or integer: help with a script

Status
Not open for further replies.

paskali

Programmer
Apr 6, 2012
13
IT
hi all, i have wrote a simple script that take a number and return
either it is an integer or a float. However, if i make a mistake and
type a char or a string, it goes in crash and esc to prompt command.
My question is simple: how can i to resolve this behaviour? Below it is
my code:
Code:
#############################################################################################
# This script ask for a number then tell you either integer or float and
print its exp form #
#############################################################################################
#
proc main {} {
	# ask for a number
	puts -nonewline "Insert a number: ";
	flush stdout;
	gets stdin number;
	# check for integer
	if {int($number)==$number} {
		puts "the number [expr int([format %f $number])] is integer";
	} else { 
		puts "the number [format %f $number] is float";
	}
	# print exp form
	puts "its exponential form is [format %e $number]";
}
# call main
main;
Thanks!
 
Hi

Similar problem was discussed recently in thread287-1681203. Please read that too.
Code:
[b]proc[/b] main [teal]{}[/teal] [teal]{[/teal]
    [gray]# ask for a number[/gray]
    [b]puts[/b] [teal]-[/teal]nonewline [green][i]"Insert a number: "[/i][/green]
    [b]flush[/b] stdout
    [b]gets[/b] stdin number
    [highlight][b]if[/b] [teal]{[/teal][teal]![[/teal][b]string[/b] is double [navy]$number[/navy][teal]][/teal][teal]}[/teal] [teal]{[/teal][/highlight]
        [highlight][b]puts[/b] [green][i]"ERROR : $number is not a numeric value"[/i][/green][/highlight]
        [highlight][b]return[/b][/highlight]
    [highlight][teal]}[/teal][/highlight]
    [gray]# check for integer[/gray]
    [b]if[/b] [teal]{[/teal]int[teal]([/teal][navy]$number[/navy][teal])==[/teal][navy]$number[/navy][teal]}[/teal] [teal]{[/teal]
        [b]puts[/b] [green][i]"the number [expr int([format %f $number])] is integer"[/i][/green]
    [teal]}[/teal] [b]else[/b] [teal]{[/teal]
        [b]puts[/b] [green][i]"the number [format %f $number] is float"[/i][/green]
    [teal]}[/teal]
    [gray]# print exp form[/gray]
    [b]puts[/b] [green][i]"its exponential form is [format %e $number]"[/i][/green]
[teal]}[/teal]

Feherke.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top