user14537238
user14537238

Reputation: 21

tcl error: rong # args: should be "set varName ?newValue?"

Write a Tcl program called grades.tcl which reads numeric grades (one to a line) from a file called grades.dat. Create another file called letter grades.dat which contains both the numeric grade and the letter grade (sepated by a tab) on a line. Also, please compute the average numeric grade and echo it to the screen. Don’t forget to close up the files before exiting. You will need to learn about the Tcl commands of open and close . Please convert the code into a procedure (proc in Tcl) called assign grade and call the procedure when you need to assigna letter grade. The input file is provided below 89,78,56,43,100,99,91,56,45,38,63,77,78,81,89,54,63

and I am trying to solve it as following:

#!/bin/env tclsh

proc assign_grade{}{set count 0
set value 0 set in[open "letter_grades.dat" a+]
set out[open "grades.dat" r]
while{[gets $out line]>=0}{puts $in  $line
if{expr $line>=90}{puts $in "\tA"}
elseif{expr $line>=80}{puts $in "\tB"}
elseif{expr $line>=70}{puts $in "\tC"}
elseif{expr $line>=60}{puts $in "\tD"}
elseif{expr $line<60}{puts $in "\tF"} {puts $in"\n"}
set value $value+$line incr count}close $in
close $out echo "the average is:"
echo $value/$count}

now it shows error the following:

wrong # args: should be "set varName ?newValue?"

while executing

set value 0 set in[open "letter_grades.dat" a+]

Upvotes: 0

Views: 514

Answers (1)

glenn jackman
glenn jackman

Reputation: 246837

Consider this, (almost) exactly the same as your code, but with correct Tcl whitespace.

proc assign_grade {} {
    set count 0
    set value 0
    set in [open "letter_grades.dat" a+]
    set out [open "grades.dat" r]
    while {[gets $out line]>=0} {
        puts $in  $line
        if {$line>=90} {
            puts $in "\tA"
        } elseif {$line>=80} {
            puts $in "\tB"
        } elseif {$line>=70} {
            puts $in "\tC"
        } elseif {$line>=60} {
            puts $in "\tD"
        } elseif {$line<60} {
            puts $in "\tF"
        } 
        puts $in "\n"
        set value [expr {$value+$line}]
        incr count
    }
    close $in
    close $out
    puts "the average is: [expr {$value/$count}]"
}

Read the rules of Tcl, and think about why this is different from your code.

Upvotes: 1

Related Questions