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

Username:   Password: 
 RegisterRegister   
 Reading a question and the correct answer from a data file
Index -> Programming, Turing -> Turing Help
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
downisleft




PostPosted: Mon Mar 09, 2009 6:01 pm   Post subject: Reading a question and the correct answer from a data file

So... a little suck. my problem being i don't understand how to get both a answer and a question from a single .txt file.
The problem is :
Create a history drill program that:

1. Reads a history question and the correct answer from a data file;
2. Presents the question to the user;
3. Uses a subroutine to get the user's answer to the question;
4. Checks the user's answer against the correct answer and provides feedback;
5. Keeps track of the number of correct and incorrect answers;
6. Repeats steps 1 through 5 several times;
7. At the end of the quiz, informs the user of the number of correct and incorrect answers

And my txt file looks like this

code:

--> Questions <--

1.   Canada becomes a country.  
2.   Transcontinental railway completed.        
3.   Jacques Cartier arrives in Canada.        
4.   French and English recognised as official languages.      
5.   Canada wins the first hockey challenge against the Soviets.        
6.   The Constitution and Charter of Rights adopted.    
7.   Oh Canada adopted as the national anthem.  
8.   The maple leaf becomes the Canadian flag.  
9.   The Gst is introduced.    
10. Newfoundland becomes the last Canadian province.

--> Answers <--

1. 1867
2. 1885
3. 1534
4. 1969
5. 1972
6. 1982
7. 1980
8. 1965
9. 1991
10. 1949



--

I have worked with stream before... however, each word goes into a array
code:

var stream : int
var studentnumber : array 1 .. 19 of int
var studentname : array 1 .. 19 of string
var studentgender : array 1 .. 19 of string
var studentage : array 1 .. 19 of string
var studentm1 : array 1 .. 19 of int
var studentm2 : array 1 .. 19 of int
var studentm3 : array 1 .. 19 of int
var studentm4 : array 1 .. 19 of int
var studentm5 : array 1 .. 19 of int
var c := 0
var m : array 1 .. 19 of int
for i : 1 .. 19
    m (i) := 0
end for

open : stream, "h://student.txt", get

for i : 1 .. 19
    get : stream, studentnumber (i), studentname (i), studentgender (i), studentage (i), studentm1 (i), studentm2 (i), studentm3 (i), studentm4 (i), studentm5 (i)
end for

(Above code used as a example how stream was used before)


My question being, how (if possible) do i get turing to take the whole question? Then compare answers from the 1 txt file im using...

Thanks in advance~
Sponsor
Sponsor
Sponsor
sponsor
Insectoid




PostPosted: Mon Mar 09, 2009 6:19 pm   Post subject: RE:Reading a question and the correct answer from a data file

get bleh : * will get the whole line. Then just cut off the number from the start.
downisleft




PostPosted: Mon Mar 09, 2009 6:50 pm   Post subject: Re: Reading a question and the correct answer from a data file

a start! but that still does not separate the questions which is what im having problems doing :3|

So

code:

var stream : int
open : stream, "h://historyset.txt", get

get : stream, *


just gets is ALL which isnt nice to play with... maybe i could get it line by line? Would be helpful T___T
Insectoid




PostPosted: Mon Mar 09, 2009 8:00 pm   Post subject: RE:Reading a question and the correct answer from a data file

That should only get 1 line. Try replacing the comma with a colon. I swear that's the syntax (though I have been wrong in the past).
saltpro15




PostPosted: Mon Mar 09, 2009 8:02 pm   Post subject: RE:Reading a question and the correct answer from a data file

insectoid is right, a colon is the correct syntax
downisleft




PostPosted: Mon Mar 09, 2009 9:04 pm   Post subject: Re: Reading a question and the correct answer from a data file

Victory is close! I can feel it Smile

code:


var line : string
var fileNumber : int
open : fileNumber, "h://history.txt", get

loop
    exit when eof (fileNumber)
    get : fileNumber, line : *
    put line
end loop



so it runs, and spits out all the info! So now i would take each line and store it into a array right?

question1
answer1
question2
answer2
nsoforth

Atleast... thats what i think i should do next
downisleft




PostPosted: Mon Mar 09, 2009 9:28 pm   Post subject: Re: Reading a question and the correct answer from a data file

updated txt file~
code:

Canada becomes a country.
1867
Transcontinental railway completed.
1885
Jacques Cartier arrives in Canada.     
1534
French and English recognised as official languages.
1969
Canada wins the first hockey challenge against the Soviets.
1972
The Constitution and Charter of Rights adopted.
1982
Oh Canada adopted as the national anthem.
1980
The maple leaf becomes the Canadian flag.
1965
The Gst is introduced.
1991
Newfoundland becomes the last Canadian province.
1949


And then the Program!

code:

var line : string
var fileNumber : int
var q1, q2, q3, q4, q5, q6, q7, q8, q9, q10 : array 1 .. 20 of string
var a1, a2, a3, a4, a5, a6, a7, a8, a9, a10 : array 1 .. 20 of int
var na1, na2, na3, na4, na5, na6, na7, na8, na9, na10 : int
var c := 0

open : fileNumber, "h://history.txt", get


for i : 1 .. 20
    get : fileNumber, q1 (i), a1 (i), q2 (i), a2 (i), q3 (i), a3 (i), q4 (i), a5 (i), q6 (i), a6 (i), q7 (i), a7 (i), q8 (i), a8 (i), q9 (i), a9 (i), q10 (i), a10 (i) : *
end for

put "Time for the first question!"
put q1
get na1
if na1 = a1 then
    put "Correct!!"
    c := c + 1
else
    put "Im sorry, the correct answer was ", a1
end if

put "Question 2!"
put q2
get na2
if na2 = a2 then
    put "Correct!!"
    c := c + 1
else
    put "Im sorry, the correct answer was ", a2
end if

put "Question 3!"
put q3
get na3
if na3 = a3 then
    put "Correct!!"
    c := c + 1
else
    put "Im sorry, the correct answer was ", a3
end if

put "Question 4!"
put q4
get na4
if na4 = a4 then
    put "Correct!!"
    c := c + 1
else
    put "Im sorry, the correct answer was ", a4
end if
 
put "Question 5!"
put q5
get na5
if na5 = a5 then
    put "Correct!!"
    c := c + 1
else
    put "Im sorry, the correct answer was ", a5
end if

put "Question 6!"
put q6
get na6
if na6 = 6 then
    put "Correct!!"
    c := c + 1
else
    put "Im sorry, the correct answer was ", a6
end if

put "Question 7!"
put q7
get na7
if na7 = a7 then
    put "Correct!!"
    c := c + 1
else
    put "Im sorry, the correct answer was ", a7
end if

put "Question 8!"
put q8
get na8
if na8 = a8 then
    put "Correct!!"
    c := c + 1
else
    put "Im sorry, the correct answer was ", a8
end if

put "Question 9!"
put q9
get na9
if na9 = a9 then
    put "Correct!!"
    c := c + 1
else
    put "Im sorry, the correct answer was ", a9
end if

put "Final Question!"
put q10
get na10
if na10 = a10 then
    put "Correct!!"
    c := c + 1
else
    put "Im sorry, the correct answer was ", a10
end if


put "You got ", c, " out of 10!"
put "Thanks for playing! Have a nice day!"




So im getting a few errors... and im not too sure why... ive looked over it a lot of times... maybe i shouldn't use a array? Even then... it outputs oddly

Help? Sad
downisleft




PostPosted: Tue Mar 10, 2009 1:06 pm   Post subject: Re: Reading a question and the correct answer from a data file

code:

var line : string
var fileNumber : int
var q1, q2, q3, q4, q5, q6, q7, q8, q9, q10 : string
var answer1, answer2, answer3, answer4, answer5, answer6, answer7, answer8, answer9, answer10 : int
var na1, na2, na3, na4, na5, na6, na7, na8, na9, na10 : int
var c := 0

open : fileNumber, "h://history.txt", get


for i : 1 .. 20
    get : fileNumber, q1 : *, answer1 : *, q2 : *, answer2 : *, q3 : *, answer3 : *, q4 : *, answer4 : *, q5 : *, answer5 : *, q6 : *, answer6 : *, q7 : *, answer7 : *, q8 : *, answer8 : *, q9 : *,
        answer9 : *, q10 : *, answer10 : *
end for

put "Time for the first question!"
put q1
get na1
if na1 = answer1 then
    put "Correct!!"
    c := c + 1
else
    put "Im sorry, the correct answer was ", answer1
end if

put "Question 2!"
put q2
get na2
if na2 = answer2 then
    put "Correct!!"
    c := c + 1
else
    put "Im sorry, the correct answer was ", answer2
end if

put "Question 3!"
put q3
get na3
if na3 = answer3 then
    put "Correct!!"
    c := c + 1
else
    put "Im sorry, the correct answer was ", answer3
end if

put "Question 4!"
put q4
get na4
if na4 = answer4 then
    put "Correct!!"
    c := c + 1
else
    put "Im sorry, the correct answer was ", answer4
end if

put "Question 5!"
put q5
get na5
if na5 = answer5 then
    put "Correct!!"
    c := c + 1
else
    put "Im sorry, the correct answer was ", answer5
end if

put "Question 6!"
put q6
get na6
if na6 = answer6 then
    put "Correct!!"
    c := c + 1
else
    put "Im sorry, the correct answer was ", answer6
end if

put "Question 7!"
put q7
get na7
if na7 = answer7 then
    put "Correct!!"
    c := c + 1
else
    put "Im sorry, the correct answer was ", answer7
end if

put "Question 8!"
put q8
get na8
if na8 = answer8 then
    put "Correct!!"
    c := c + 1
else
    put "Im sorry, the correct answer was ", answer8
end if

put "Question 9!"
put q9
get na9
if na9 = answer9 then
    put "Correct!!"
    c := c + 1
else
    put "Im sorry, the correct answer was ", answer9
end if

put "Final Question!"
put q10
get na10
if na10 = answer10 then
    put "Correct!!"
    c := c + 1
else
    put "Im sorry, the correct answer was ", answer10
end if


put "You got ", c, " out of 10!"
put "Thanks for playing! Have a nice day!"




It just doesn't like me i guess... only 1 error now...

ERROR : 'get' width applies only to strings and char(n)

I... don't understand? Google failed to help me here
Sponsor
Sponsor
Sponsor
sponsor
DemonWasp




PostPosted: Tue Mar 10, 2009 1:34 pm   Post subject: RE:Reading a question and the correct answer from a data file

You're using:
Turing:
get : stream, variable : *

with the :* part in order to get an entire line, so that you can include spaces into the questions.

However, integers can't reasonably have spaces in them. If there are numbers separated by a space: 1234 5678 , then that's two separate numbers. So, you should be using:
Turing:
get : stream, integerVariable


For future reference, CompSci.ca supports a nonstandard BBCode tag: you can add syntax highlighting by using [ syntax = "Turing" ] [ / syntax ] around your code instead of [ code ] tags (all minus the spaces, of course).
downisleft




PostPosted: Tue Mar 10, 2009 3:10 pm   Post subject: Re: Reading a question and the correct answer from a data file

^___________________________^!!!! This is why i love programming, that sweet feeling of perfecting something!!


Turing:


put "Time for the first question!"
put " "
get : fileNumber, q1 : *
get : fileNumber, a1 : *
put q1
get na1
if na1 = a1 then
    put "Correct!!"
    c := c + 1
else
    put "Im sorry, the correct answer was ", a1
end if

delay (1000)
cls
put "Question 2!"
put " "
get : fileNumber, q2 : *
get : fileNumber, a2 : *
put q2
get na2
if na2 = a2 then
    put "Correct!!"
    c := c + 1
else
    put "Im sorry, the correct answer was ", a2
end if



It works if you get them at each line... ^____^ And if the var are strings
Thanks to everyone who helped
Special thanks to DemonWasp, picking out the strings thing, how to get properly Very Happy
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  [ 10 Posts ]
Jump to:   


Style:  
Search: