Computer Science Canada

Getting Certain Values From Files

Author:  ryder [ Sat May 28, 2022 12:15 am ]
Post subject:  Getting Certain Values From Files

What is it you are trying to achieve?
Im trying to make my program read the information from a text file(like shown below) and calculate the cost for each
event, and the total cost of all the events.

(First line is event name, second is price per ticket, third is number of tickets)

Event 1 - Jun 10
12.34
2

What is the problem you are having?
I have zero clue on how to make it read from a certain line and to get the info into a variable so that I can do these calculations. Just know how to open the text file and display it.




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

Turing:


var fevent , price: int
var event : string

open : fevent, "event.txt", get

loop
    exit when eof (fevent)
    get : fevent, event :*
    put event
end loop

close (fevent)



Please specify what version of Turing you are using
Turing 4.1.1

Author:  scholarlytutor [ Sat May 28, 2022 8:40 pm ]
Post subject:  Re: Getting Certain Values From Files

The main issue here is that you've just started learning how to get information from a text file, and you're trying to do a task that is much more complicated.

I'll help get you started off. Since you are processing text files, any numbers you extract will need to be converted to int or real data types. Do you know how to do that?

Second, I'd recommend organizing your text file differently. Do you really need a date? If not, delete it. Then, put your ticket price and number of tickets on the same line, like so:

12.34 2

You don't need a number 1 to say that is the first line. Since the data from this event is all on line 1, it is obvious this is the first event.

Now, when you loop, grab the first number with one variable and the other number with another variable. Don't use :* because that grabs an entire line of text.

Give this a try and feel free to ask any other questions.

Author:  ryder [ Sun May 29, 2022 12:06 am ]
Post subject:  Re: Getting Certain Values From Files

Hi, thanks for your response. This is a school project so yes the text file needs to be formatted exactly like that. No I do not know how to convert things i extract into int or real types …. at least I don’t think. As for the Event 1 , I just put that for the sake of simplicity , it will actually say something like “Christmas Carol” or anything else random like that.

Author:  scholarlytutor [ Sun May 29, 2022 12:57 pm ]
Post subject:  RE:Getting Certain Values From Files

Ok, if you can't change it, you can make 3 variables for each: one for event, one for price, and one for quantity. Get the whole line for each one using :* Then make 3 separate get statements for each variable inside your loop. Use put statements to check if it worked.

Once that is working, if you have several events to store in your file, you'll likely need 3 arrays. If you don't know how to use arrays, feel free to ask.

To convert strings to numbers, you'll need strint() and strreal(). The Turing reference found in the help section explains how they work.

Try that out and let me know how it goes.


: