Programming C, C++, Java, PHP, Ruby, Turing, VB
Computer Science Canada 
Programming C, C++, Java, PHP, Ruby, Turing, VB  

Username:   Password: 
 RegisterRegister   
 Splitting my program into multiple files (wrong argument type)
Index -> Programming, Turing -> Turing Help
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
fletch_to_99




PostPosted: Thu Apr 19, 2012 12:13 pm   Post subject: Splitting my program into multiple files (wrong argument type)

What is it you are trying to achieve?
Trying to break my program into multiple files.

What is the problem you are having?
Argument is wrong type once in a seprate file however when all in one file the program runs great

Describe what you have tried to solve this problem
I'm not sure what to do. My teacher didn't know what was wrong either however he stated I have the wrong desgin for my code (not using it as Object Oriented but more like procedural). This is my first attempt at having multiple files for the program, any tips would help! Smile

Post any relevant code (You may choose to attach the file instead of posting the code if it is too long)
See code below
Matt L C10Q6 Main.t
Turing:

include "Matt L C10Q6 Types.t"
include "Matt L C10Q6 Calculations.t"

proc setup (var students : array 1 .. * of Assignment)
    for i : 1 .. upper (students)
        students (i).Number := i
        put "What is the ", students (i).Number, " students name?"
        get students (i).Name
        put "What grade is ", students (i).Name, " in?"
        get students (i).Grade
        put "What is ", students (i).Name, "'s mark?"
        get students (i).Mark
    end for
end setup

proc menu (var students : array 1 .. * of Assignment)
    var calc : ^Calculations
    new Calculations, calc
    var opt : int
    cls
    put "What would youl like to do with the data?"
    put "1. Clear everything and enter more data"
    put "2. Find the avrage mark"
    put "3. Find the median"
    put "4. Sort the grades in decending order"
    put "5. Search for a certian student and display their info"
    put "6. Change a students information"
    put "7. Search for the highest mark in the class"
    put "8. Search for the lowest mark in the class"
    put "9. Display all information"
    get opt
    case opt of
        label 1 :
            var option : string
            put "Are you sure (Y/N): " ..
            get option
            cls
            if Str.Upper (option) = "Y" then
                for i : 1 .. upper (students)
                    students (i).Number := i
                    put "What is the ", students (i).Number, " students name?"
                    get students (i).Name
                    put "What grade is ", students (i).Name, " in?"
                    get students (i).Grade
                    put "What is ", students (i).Name, "'s mark?"
                    get students (i).Mark
                end for
            end if
            menu (students)
        label 2 :
            cls
            put "The grade avrage is: ",calc -> calcAvg (students)
        label 3 :
        label 4 :
        label 5 :
        label 6 :
        label 7 :
        label 8 :
        label 9 :
    end case
end menu

var num : int := 1
put "How many students do you want to add to the list: " ..
get num
var students : array 1 .. num of Assignment
setup (students)
menu (students)


Matt L C10Q6 Types.t

Turing:

type Assignment :
    record
        Number : int
        Name : string
        Grade : int
        Mark : int
    end record


Matt L C10Q6 Calculations.t

Turing:

class Calculations
    export calcAvg
   
    include "Matt L C10Q6 Types.t"

    fcn calcAvg (students : array 1 .. * of Assignment) : int
        var total, count : int := 0
        for i : 1 .. upper (students)
            total := total + students(i).Mark
            count := count + 1
        end for
        result total div count
    end calcAvg

end Calculations



Please specify what version of Turing you are using
Latest (4.1.1?)
Sponsor
Sponsor
Sponsor
sponsor
Tony




PostPosted: Thu Apr 19, 2012 1:17 pm   Post subject: Re: Splitting my program into multiple files (wrong argument type)

fletch_to_99 @ Thu Apr 19, 2012 12:13 pm wrote:

Argument is wrong type once in a seprate file however when all in one file the program runs great

Which function / line is the problem on? What is the expected type? What is the provided type?
Latest from compsci.ca/blog: Tony's programming blog. DWITE - a programming contest.
fletch_to_99




PostPosted: Thu Apr 19, 2012 2:58 pm   Post subject: RE:Splitting my program into multiple files (wrong argument type)

It's line 53 in the main file. Its calling the calcAvg function in the Calculations class which is expecting: students : array 1 .. * of Assignment. This is whats provided: var students : array 1 .. num of Assignment.
DemonWasp




PostPosted: Thu Apr 19, 2012 3:26 pm   Post subject: RE:Splitting my program into multiple files (wrong argument type)

You're running into a fundamental design flaw in Turing. Basically, include doesn't work like you're hoping it does.

What include "foo.t" actually does:
- open "foo.t" and grab its contents
- put those contents in place of the include statement in the original file.

What's happening in your program:
- Main.t includes Types.t (awesome, now Assignment is defined)
- Main.t includes Calculations.t
- Calculations.t includes Types.t (now Assignment is defined twice, in different "scopes")

The problem results because the Assignment type is defined twice, and the types are not considered to be the same.

Competent languages are designed to avoid this problem (by providing the programmer the ability to solve the problem, or by solving the problem inherently in their design). C and C++ take the former route, with #ifdef and #define, while Java and most other high-level languages take the latter route.

In Turing, the best I've been able to do is to make sure that each file is included exactly once. You probably want:
- Calculations.t includes Types.t
- Main.t includes Calculations.t (but NOT Types.t).

It's possible to re-create this in one file, to help you understand and play with it:

Turing:

type my_type :
    record
        name : string
    end record
   
class MyClass
    %import my_type
    export do_something
   
    % To make this example work, comment out this copy of my_type and uncomment
    % import my_type
    type my_type :
        record
            name : string
        end record
   
    proc do_something ( objects : array 1 .. * of my_type )
        % code here...
    end do_something
end MyClass


var objects : array 1..4 of my_type

var myClass : ^MyClass
new myClass
myClass -> do_something ( objects )
fletch_to_99




PostPosted: Thu Apr 19, 2012 6:35 pm   Post subject: Re: RE:Splitting my program into multiple files (wrong argument type)

DemonWasp @ Thu Apr 19, 2012 3:26 pm wrote:
You're running into a fundamental design flaw in Turing. Basically, include doesn't work like you're hoping it does.

What include "foo.t" actually does:
- open "foo.t" and grab its contents
- put those contents in place of the include statement in the original file.

What's happening in your program:
- Main.t includes Types.t (awesome, now Assignment is defined)
- Main.t includes Calculations.t
- Calculations.t includes Types.t (now Assignment is defined twice, in different "scopes")

The problem results because the Assignment type is defined twice, and the types are not considered to be the same.

Competent languages are designed to avoid this problem (by providing the programmer the ability to solve the problem, or by solving the problem inherently in their design). C and C++ take the former route, with #ifdef and #define, while Java and most other high-level languages take the latter route.

In Turing, the best I've been able to do is to make sure that each file is included exactly once. You probably want:
- Calculations.t includes Types.t
- Main.t includes Calculations.t (but NOT Types.t).

It's possible to re-create this in one file, to help you understand and play with it:

Turing:

type my_type :
    record
        name : string
    end record
   
class MyClass
    %import my_type
    export do_something
   
    % To make this example work, comment out this copy of my_type and uncomment
    % import my_type
    type my_type :
        record
            name : string
        end record
   
    proc do_something ( objects : array 1 .. * of my_type )
        % code here...
    end do_something
end MyClass


var objects : array 1..4 of my_type

var myClass : ^MyClass
new myClass
myClass -> do_something ( objects )


Thanks for the reply! I will try this out at school tomorrow. I use java at home to make plugins for minecraft and in Java for each class in a separate file you have to import everything again, that's what stumped me in turing. Again, thanks!!
Dreadnought




PostPosted: Fri Apr 20, 2012 5:46 pm   Post subject: Re: Splitting my program into multiple files (wrong argument type)

fletch_to_99 wrote:
I use java at home to make plugins for minecraft and in Java for each class in a separate file you have to import everything again, that's what stumped me in turing.

Turing has import which is closer to the equivalent in java (it works with export and lets you use things like module, class and unit to organize code). The include statement just opens the Turing file it is given and copies the code in the place of the include statement. It does not care about code organization.

For example you can even do this (which looks kinda dumb):
Turing:
/*      file1.t     */

for i:1..10
    include "file2.t"


Turing:
/*       file2.t      */

put i
end for

Notice that I split a for loop, and use a for loop variable in the second file that is only declared in the first file. Anyway, all this to say that import may have been what you wanted to use.
Display posts from previous:   
   Index -> Programming, Turing -> Turing Help
View previous topic Tell A FriendPrintable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic

Page 1 of 1  [ 6 Posts ]
Jump to:   


Style:  
Search: