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

Username:   Password: 
 RegisterRegister   
 Loading data from text file into two separate arrays
Index -> Programming, Turing -> Turing Help
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
haayyeess




PostPosted: Wed Jan 07, 2015 12:31 pm   Post subject: Loading data from text file into two separate arrays

What is it you are trying to achieve?
For some reason all of the data I entered cleared... -.-

Basically I want to load data into two seperate arrays from one text file. I have divided the file in the following format.
M represents Multiple choice, and B reps T/F, and will be used to recognize the two later in my "game"
Vertical bars, | seperate the question from the answer, and this is what i want to store in seperate arrays, the questions and the answers.
The multiple vertical bars seperating answers will be used in the game for formatting.
EX:
MTest 1 ans is a|a)rock|b)pet|c)fish|d)tent|
BGoats R REALLY COOLF|f


What is the problem you are having?
I cannot load the data into two arrays... IS it even possible? How can I go about doing it?


Describe what you have tried to solve this problem
Many things, including for's with indexing.. I'm not even sure how the get function reads along in the txt file.. this may be helpful.


Post any relevant code (You may choose to attach the file instead of posting the code if it is too long)
<Answer Here>

Problem code, messy sorry.. Tired of trying things, decided i need real help.
Turing:


%LOAD QUESTIONS
procedure Load_QuestionsNAnswers (var Question : array 1 .. MAXQ of string (STRNGLNTH), Answer : array 1 .. MAXQ of string (STRNGLNTH), var QCounter : nat1)

      % VARIABLES
    var FileNum : int
    var FileName : string (30)
   
      % ASSIGNMENTS 
    QCounter := 0

    %%%OPEN
    cls % KEEP IT CLEAN
    put "What file name do you want to read from?"
    put "I'm assuming it's in the 'Saved Questions' folder."
    put "Please include the full file name..."
    put "For Example:  Test.txt, .txt must be included!"
    get FileName

    open : FileNum, "F:\\12\\Semester One\\English\\Hamlet Creative Project\\Saved Questions\\" + FileName, get

    %STORING DATA
    loop
        exit when eof (FileNum)
        QCounter := QCounter + 1  %questions R paired w/ ans.
       
        for Index : 1 .. ord ("|")
            get : FileNum, Question (QCounter) %Get all Q, store q array
        end for
            get : FileNum, Answer (QCounter) :%Get all answers, store in ans array
    end loop

    %%%CLOSE
    close : FileNum
    cls
   
    colour(brightgreen)
    locate(17,17)
    put "File loaded successfully..."
    delay(1000)


end Load_QuestionsNAnswers




ORIGINAL CODE:

Turing:


%LOAD QUESTIONS
procedure Load_Questions (var Question : array 1 .. MAXQ of string (STRNGLNTH), var QCounter : nat1)

      % VARIABLES
    var FileNum : int
    var FileName : string (30)
   
      % ASSIGNMENTS 
    QCounter := 0

    %%%OPEN
    cls % KEEP IT CLEAN
    put "What file name do you want to read from?"
    put "I'm assuming it's in the 'Saved Questions' folder."
    put "Please include the full file name..."
    put "For Example:  Test.txt, .txt must be included!"
    get FileName

    open : FileNum, "h:\\12\\Semester One\\English\\Hamlet Creative Project\\Saved Questions\\" + FileName, get
   
    %STORING DATA
    loop
        exit when eof (FileNum)
        QCounter := QCounter + 1
        get : FileNum, Question (QCounter) :*
        %QCounter := QCounter + 1
    end loop

    %%%CLOSE
    close : FileNum
    cls
   
    colour(brightgreen)
    locate(17,17)
    put "File loaded successfully..."
    delay(1000)


end Load_Questions



Please specify what version of Turing you are using
4.1.1
Sponsor
Sponsor
Sponsor
sponsor
Insectoid




PostPosted: Wed Jan 07, 2015 2:53 pm   Post subject: RE:Loading data from text file into two separate arrays

code:
    loop
        exit when eof (FileNum)
        QCounter := QCounter + 1  %questions R paired w/ ans.
       
        for Index : 1 .. ord ("|")
            get : FileNum, Question (QCounter) %Get all Q, store q array
        end for
            get : FileNum, Answer (QCounter) :*  %Get all answers, store in ans array
    end loop


You want to load the data in this following order:
question, answer, question, answer, etc.

Your code is loading the data in this order:
question, question, question, question... ('|' times, however long that is)..., answer.

Can you think of a way to fix this? Hint: You only need to move one line of code.
haayyeess




PostPosted: Wed Jan 07, 2015 8:34 pm   Post subject: RE:Loading data from text file into two separate arrays

Isectoid,

No no I know that, but even that doesn't work with what I have.. If i need to use that indexing for loop, where do I use the indexing counter? Currently I don't use it...

Maybe I was way off with what I was trying there too.

I want to read through a question section until I hit a vertical bar, then I want to store the remainder in the array for the answer, and do the same for the next line down, so on...
Insectoid




PostPosted: Wed Jan 07, 2015 9:56 pm   Post subject: RE:Loading data from text file into two separate arrays

Quote:
I want to read through a question section until I hit a vertical bar


You know, it's kinda funny, because the way you implemented this actually sort of works in other languages. You can't do that with a for loop in Turing though (well, maybe with some serious quackery, but no sane person would).

You could do it that way. Or you could restructure your input file so that you don't have to. Your input file only needs to be machine readable. It can look like a huge mess to humans and it wouldn't matter. In Turing, you can easily read one character, one entire word, or one entire line. Anything else takes more (potentially a lot more) work. Look at your data and try to find a way to organize it in your file so that you only have to use those three methods in a very simple way.
haayyeess




PostPosted: Wed Jan 07, 2015 10:04 pm   Post subject: RE:Loading data from text file into two separate arrays

I figured such.. I'm very analytically minded, and can think simple in the such of programs, but sometimes Turing works against me.

There's no way I can do it like this eh? I really want to load the data into two separate arrays. I was trying to avoid having to load both a question and an answer file.

My other option is just displaying certain parts of what's loaded into one array... but I don't think that's very "clean".

I want to aim to avoid making the data in the file to extreme. I actually would like users to be able to use the program with other sets of data. It's going to be a quizzing program.
Tony




PostPosted: Wed Jan 07, 2015 10:07 pm   Post subject: RE:Loading data from text file into two separate arrays

Read the entire line, then figure out where to split it with the help of index. The gotcha is that the length of the line is limited by what you can fit into Turing's string, which is 255 chars.
Latest from compsci.ca/blog: Tony's programming blog. DWITE - a programming contest.
haayyeess




PostPosted: Wed Jan 07, 2015 10:12 pm   Post subject: RE:Loading data from text file into two separate arrays

255 Char limit wont be an issue to me. (currently)

SO through reading the entire line..
Do I still have to store it into one index first?

OR

Do you mean, get the line, then sift through it, divide it up, and store it in separate arrays while the file is open?

If so, how do I even do that? My in depth understanding of how the file loading actually works is either too limited, or Turing wont allow this...
Insectoid




PostPosted: Wed Jan 07, 2015 10:28 pm   Post subject: RE:Loading data from text file into two separate arrays

Quote:
I actually would like users to be able to use the program with other sets of data


Actually, making it very easy for the computer to read will, in this case, make it very easy for humans to read or write. You could also make a very simple program that lets people design quizzes, which would write them to a file in the proper format automatically.

Quote:
SO through reading the entire line..
Do I still have to store it into one index first?


You need to store the entire line in one variable. Then you can separate it out.

You should have an array for questions and an array of an array of answers (you can clean this up with structs but I dunno if you've seen those yet). If you haven't learned about multi-dimensional arrays yet then this should be fun.

Your loading code should look something like this then, based on your data format:

1. Read a whole line.
2. Isolate and store the question type
3. Isolate and store the question
4. isolate and store the correct answer (a, b, c, t, f, whatever)
5. Isolate and read the first answer, repeat for every answer
7. repeat for every question

Of course, putting things on a new line in a text file both A) isolates things for you that you might want isolated and B) implies a lot of data so you can remove a lot of arguably useless characters such as | from the file and dramatically reduce the size, so I'm still advocating changing your format. You're making things way, way harder for yourself otherwise.
Sponsor
Sponsor
Sponsor
sponsor
haayyeess




PostPosted: Wed Jan 07, 2015 10:37 pm   Post subject: RE:Loading data from text file into two separate arrays

Okay, I may design that program at another date, but prior to that I have to get this one working for ENGLISH on Friday, believe it or not. ha-ha.

I could see my computer science teacher for a lesson on "structs", assuming they're not too complex (which I doubt) I'll manage.

If I was to change the formatting, won't I be causing grief through having trouble tracking between questions and answers..? Multiple choice will have four answer choices, T/F only two.

Sorry I guess it either matches users data, or it doesn't..
Insectoid




PostPosted: Thu Jan 08, 2015 12:01 am   Post subject: RE:Loading data from text file into two separate arrays

Quote:
If I was to change the formatting, won't I be causing grief through having trouble tracking between questions and answers..? Multiple choice will have four answer choices, T/F only two.


No, it won't. You can put extra data in the file if you want, like, say the number of answers a question has. In fact, putting numbers in the file saying how much of a thing there are is a very simple way to solve this and other problems that might pop up.
haayyeess




PostPosted: Thu Jan 08, 2015 9:09 am   Post subject: Re: Loading data from text file into two separate arrays

Turing:

%LOAD QUESTIONS
procedure Load_QuestionsNAnswers (var Question : array 1 .. MAXQ of string (STRNGLNTH), Answer : array 1 .. MAXQ of string (1), var QCounter : nat1)

   
      % VARIABLES
    var FileNum : int
    var FileName : string (30)
   
      % ASSIGNMENTS 
    QCounter := 0

    %%%OPEN
    cls % KEEP IT CLEAN
    put "What file name do you want to read from?"
    put "I'm assuming it's in the 'Saved Questions' folder."
    put "Please include the full file name..."
    put "For Example:  Test.txt, .txt must be included!"
    get FileName

    open : FileNum, "F:\\12\\Semester One\\English\\Hamlet Creative Project\\Saved Questions\\" + FileName, get

    %STORING DATA
    loop
        exit when eof (FileNum)
        QCounter := QCounter + 1  %questions R paired w/ ans.
       
        get : FileNum, Question (QCounter) :* %READ + STORE the full Q + ANS
        get : FileNum, Answer (QCounter)  %READ (only 1 char)+ STORE the correct answer
       
        %TESTS
        put Question (QCounter)
        put Answer (QCounter)
        delay (1000)
        %END TESTS
    end loop

    %%%CLOSE
    close : FileNum
    cls
   
    colour(brightgreen)
    locate(17,17)
    put "File loaded successfully..."
    delay(1000)


end Load_QuestionsNAnswers



Having issues with:
Turing:

 
        get : FileNum, Question (QCounter) :* %READ + STORE the full Q
        get : FileNum, Answer (QCounter)  %READ (only 1 char)+ STORE the correct answer


"illegal get item" ... suggestions?

Here's what is in the document I am accessing (Its just a test for now):
"MWhat is the best name?|a) Brandon|b) Jim|c)Joan|d)James
a
BIs it true to be false or true?
t
MWhat is the bestestttest name?|a) Brandon|b) Jim|c)Joan|d)James
a
BIs it true to be faaaaaaaaaaaalse or true?
t"


I have also tried this....

The problem with this one is currently when I go to get the full line after confirming if it is a question or an answer.
Turing displays no issue in getting it, but it doesn't actually get anything.
I know this because when I test outputting it on the screen I get "variable has no value"

I don't know much more than I have recently read about multidimensional arrays, but my compsci teacher says they're not needed, while an above poster has suggested it.
I'm a little confused and frustrated at the moment... I just want something to work lol.

TURING:
Turing:

%LOAD QUESTIONS and ANSWERS and ISOLATE
procedure Load_Questions (var Question : array 1 .. MAXQ of string (STRNGLNTH), var Answer : array 1 .. MAXQ, 1..5 of string (STRNGLNTH), var QCounter : nat1, var ACounter : nat1)

      % VARIABLES
    var FileNum : int
    var FileName : string (30)
    var TempStorage : array 1 .. MAXQ of string (STRNGLNTH)
   
      % ASSIGNMENTS 
    QCounter := 1
    ACounter := 1 % TO MATCH ARRAY

    %%%OPEN
    cls % KEEP IT CLEAN
    put "What file name do you want to read from?"
    put "I'm assuming it's in the 'Saved Questions' folder."
    put "Please include the full file name..."
    put "For Example:  Test.txt, .txt must be included!"
    get FileName

    open : FileNum, "h:\\12\\Semester One\\English\\Hamlet Creative Project\\Saved Questions\\" + FileName, get
   
    %STORING DATA
    loop
   
        exit when eof (FileNum)
        get : FileNum, TempStorage (QCounter) :1
        %QCounter := QCounter + 1
       
        %%%%ISOLATING AND STORING DATA%%%%%%%%%%
        for Index : 1 .. 1
            put Index
            put TempStorage(Index)% NOT WORKING>>>
            delay (1200) %TEST
           
            if TempStorage (Index) = "M" or TempStorage (Index) = "B" then % Question
                get : FileNum, TempStorage (QCounter)  :*
                Question (QCounter) := TempStorage (QCounter)
                QCounter := QCounter + 1
                ACounter := 1 % NEW Q therefore no ans yet (1 unfortunately cuz array)
                put "Q" %TEST
                put Question (QCounter)
                delay (1000) %TEST
               
            else % MUST BE an Answer
                ACounter := ACounter + 1 % + 1 A for Q
                %Take whats in temp storage, store in answer array under q count, then a count.
                get : FileNum, TempStorage (QCounter) :*
                Answer (QCounter, ACounter) := TempStorage (QCounter) 
                put "ANS" %TEST
                delay(500)%TEST
                % put Question (QCounter)
                % delay (1000) %TEST
            end if
       end for
       
    end loop

    %%%CLOSE
    close : FileNum
    cls
   
    colour(brightgreen)
    locate(17,17)
    put "File loaded successfully..."
    delay(1000)


end Load_Questions




Any help is appreciated.
Insectoid




PostPosted: Thu Jan 08, 2015 6:29 pm   Post subject: RE:Loading data from text file into two separate arrays

There's a couple of issues here.

1. Procedure parameters. For some reason, Turing doesn't like assigning data to the 2nd array (if you swap so that answers is declared before questions, then the error is thrown on the questions array). I'm sure there's a reason for this, but I dunno. Solution? Remove them from the parameters section and either declare them as global variables or declare them inside the function and return them at the end of it. Furthermore, you declare QCounter in the parameter section, and then immediately set it to 0. The parameter section is not meant to declare variables that you intend to use in the function. It is meant to be used to pass data to the procedure that it can use.

For example,
code:

proc foo (int bar)
    put bar
end foo
foo (5)


The above code will output 5. If you type foo (3) it will output 3 instead. What you are doing is this:

code:

proc foo (int bar)
    bar := 5
    put bar
end foo
foo (3)
%in this case, you are discarding the value passed to bar and re-assigning it 5. bar would be better declared inside the procedure than in the procedure's declaration, as follows:
proc foo
    int bar := 5
    put bar
end foo
bar


So, definitely review how function & procedure definitions work.


Regarding strings, you do not need to specify a length for strings in Turing. var foo (string) is completely acceptable. You should only specify the length when you know the string will become that large to save some time on memory management, as re-sizing strings is actually pretty slow. And of course, a string (1) can just be declared as a char, which takes up (marginally) less memory and is easier to read.

Finally, file paths. Your monstrous gigantic file path is effectively useless because the program will only work on your computer unless you put the program in exactly that directory. There are some useful shortcuts you can use to make it work on anyone's computer- './' and '../'.

The first means 'the folder I'm in right now', which for Turing means the folder the program is stored in. If you have an image you want to load in the same folder as your program, you can just use './image.bmp' instead of the entire path (actually in Turing I think you can skip the ./ and just do 'image.bmp', but that's not the point).

With ../ you actually go up one folder. If your program is in the folder C:\\a\b\c. then '../' would refer to the folder b, so if you have an image in folder b, you can just do '../image.img'. You can even add more dots to go up further. '.../image.img' would be the same as C:\\a\image.img. You can use this to navigate to pretty much any folder anywhere on the computer. Note: I'm not sure if you need forward or backslashes for this. Experiment.

Finally, you have a lot of delays in your program for testing. Remove those and just use getch. That way all you have to do is hit a key to continue the program instead of waiting for the delay to finish.
haayyeess




PostPosted: Thu Jan 08, 2015 8:25 pm   Post subject: RE:Loading data from text file into two separate arrays

That information is all passed on to my main program, this is merely a subroutine in a different file. I assign strings lengths because I have a constant changing them, and currently restricting them to 100 characters to save memory. Nothing exceeds that. As for the string (1), I always avoid chars because they often give me errors. File path is due to the memory stick that the files on. It follows that path to please the teacher I am handing it into for English, as well as being complex enough to count towards part of my specialized high skills major in school. Yeah the getch is a good suggestion, i'll start using that.

I don't think you really answered where my error is though, if you know?
Insectoid




PostPosted: Thu Jan 08, 2015 8:52 pm   Post subject: RE:Loading data from text file into two separate arrays

Aha, I've found the problem with your Answers array. You just forgot the var keyword when declaring it.

Quote:
I assign strings lengths because I have a constant changing them, and currently restricting them to 100 characters to save memory.


You'll save more memory by letting Turing size them for you. If you're not planning on changing the strings later on (loading different questions, for example) then Turing should allocate the minimum amount of RAM to store that string. There's nothing wrong with your approach, but there is something wrong with your reason for doing so Razz

Quote:
As for the string (1), I always avoid chars because they often give me errors.


If you treat a char like an int you won't have any problems. They're functionally identical to nat1's. If they're giving you errors, then you don't know how to use them, which means you should use them more.

Quote:
File path is due to the memory stick that the files on. It follows that path to please the teacher I am handing it into for English, as well as being complex enough to count towards part of my specialized high skills major in school.


That's...completely irrelevant. I'm sure you'll get fantastic marks when your teacher plugs it in and it doesn't work because the usb drive is mounted under E:\\ on her computer. This file path is BAD. It's probably the worst offence in the entire program. If there was any part I'd ask you to change it is the file path. A long file path doesn't make it more complex, it makes it wrong. If you really want 'complexity' then just chain a bunch of ../'s together. 'pic.load (".../a../a../a../a../../a../a../a/image.png")' looks pretty complex to me. If your specialized high skills major judge/marker/whatever knows anything about programming he will likely dock marks for such a crap file path.
haayyeess




PostPosted: Fri Jan 09, 2015 10:53 am   Post subject: RE:Loading data from text file into two separate arrays

Wow, such a small little problem. I can't beleive I didn't find that over such a period of time searching. Thanks a bunch, Insectoid. I appreciate it a lot. I'd give you Karma, but apparently I can't yet. Lol. I took your advice on the stringing too. As for the file path... I'll look into it.

The rest shouldn't be too hard.

Thanks again, i'll post back if need be.
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  [ 15 Posts ]
Jump to:   


Style:  
Search: