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

Username:   Password: 
 RegisterRegister   
 Trivia program with count function not working correctly
Index -> Programming, Turing -> Turing Help
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
bestmom77777




PostPosted: Wed Jan 25, 2006 7:08 pm   Post subject: Trivia program with count function not working correctly

Firstly I would like to say I did read the page that you reckoned no one would look at - how not to post! Hopefully I'm doing it right. I have also used the search function using the word "count", but couldn't find anything. And I have searched quite a few tutorials. But because I'm not sure what I'm looking for, it's a bit hard.

I should explain that I do not know Turing and am trying to help my son who has written a program and one small part does not seem to work. I can code web pages and did learn a little DOS way way back when, so am understanding most of his coding, but not all. So please forgive me if I appear stupid - I'm not really Laughing

His program has 15 questions and when run, a random selection of 10 will be asked. The else if loop bit works fine and each question pops up in turn. But he is trying to get the program to keep score and at the end show the score. Without pasting the whole thing, I'm hoping I have got the right bits shown below. I would be very grateful if anyone has any suggestions as to what he has done wrong. I've checked carefully for the usual missing space, colon, etc. and can't see anything obvious. Have just shown the first question

code:
var userScore : int
userScore := 0

loop
     for count : 1 .. 10
       var QuestNum : int
       randint (QuestNum, 1, 9)

if QuestNum = 1 then
   var answer1 : string
put "What was the only word ever spoken by the Polka Dot Door's Polkaroo?"
   put "a) Polka"
   put "b) Polkaroo"
   put "c) Uh Oh!"
   put "d) Jellybeans"
     get answer1
   if answer1="b" then
   put "That's right! How did you know that? Nevermind, I'm phsycic - I know. You get a point."
      userScore := userScore +1
   else put "I'm sorry, you're not as smart as you think! You don't get a point."
   end if
delay (1500)

elsif QuestNum = 2 then
   var answer2 : string   
put " "


etc etc

At the end he has the following

code:

        finished := true
        Music.PlayFileStop
        end if
    end for
end loop


Hopefully the above is enough for someone to understand what I'm getting at. I apologise if my explanation is a bit confused - I'm confused too.

Thank you
Sponsor
Sponsor
Sponsor
sponsor
person




PostPosted: Wed Jan 25, 2006 7:30 pm   Post subject: (No subject)

i dont think im understanding u properly, but cant u just have this at the very end of the program?

code:

put userScore
bestmom77777




PostPosted: Wed Jan 25, 2006 8:04 pm   Post subject: (No subject)

geez, shows how dangerous a little understanding can be. I'm sorry, I got it a bit wrong.

The program is supposed to stop asking questions after 10 have been answered. But it keeps asking questions. So I was partly right in as much as the Score bit doesn't come up, but that's because the count loop doesn't stop and it won't come up until it does.

var userScore : int
userScore := 0

loop
for count : 1 .. 10
var QuestNum : int
randint (QuestNum, 1, 9)

if QuestNum = 1 then
var answer1 : string
put "What was the only word ever spoken by the Polka Dot Door's Polkaroo?"
put "a) Polka"
put "b) Polkaroo"
put "c) Uh Oh!"
put "d) Jellybeans"
get answer1
if answer1="b" then
put "That's right! How did you know that? Nevermind, I'm phsycic - I know. You get a point."
userScore := userScore +1
else put "I'm sorry, you're not as smart as you think! You don't get a point."
end if
delay (1500)

elsif QuestNum = 2 then
var answer2 : string
put " "

etc etc

At the end he has the following

finished := true
Music.PlayFileStop
end if
end for
end loop


So the bits in bold are the ones that aren't working. Sorry about the mix up.
Rasta Fella




PostPosted: Wed Jan 25, 2006 8:07 pm   Post subject: (No subject)

bestmom77777 wrote:
Firstly I would like to say I did read the page that you reckoned no one would look at - how not to post!

Well, did you read the part about using [code.][syntax] tags?
Nevertheless, at the end of your code do this:
code:
put "Your Final Score Was"..userScore

This will output the amount of points he/she had at the end of the quiz.
Martin




PostPosted: Wed Jan 25, 2006 8:11 pm   Post subject: (No subject)

The problem is the for loop inside of the main loop. A loop loop just goes around infinitely

code:
loop
    ...
end loop


will execute whatever ... is until someone calls the exit command to leave the loop.

Your code looks like this:
code:
loop
    for count: 1 .. 10
        ...
    end for
end loop


So what is happening is that it goes into the loop, goes through the for loop 10 times, hits end loop and jumps back up to the top of the loop. This then causes it to go through the for loop again ... and again ... and again.

The simple solution is just to get rid of the loop ... end loop (but still leave the for loop).

Hope that helps (keep asking questions if you need to!)
Martin




PostPosted: Wed Jan 25, 2006 9:01 pm   Post subject: (No subject)

One other thing to do would be move the Music.PlayFileStop outside of the for loop, otherwise the music will stop after the first question.
bestmom77777




PostPosted: Wed Jan 25, 2006 9:26 pm   Post subject: (No subject)

Martin, thank you so much. I should have got that on my own (duh). I know enough about coding to have figured that out, but I just couldn't see it. I had got part way by thinking that the end commands needed to be up higher.

If I could have run the program I might have got there, but we don't have it at home, so that didn't help. Anyway you sure did, so thank you again. And now I've found this site, I'm sure I'll find a use for the Java as I use that and get in a pickle quite often Laughing
Martin




PostPosted: Wed Jan 25, 2006 9:31 pm   Post subject: (No subject)

We're here to help.

Get your son to ask his teacher for a copy of Turing - as far as I know, schools often provide it free of charge to students.
Sponsor
Sponsor
Sponsor
sponsor
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  [ 8 Posts ]
Jump to:   


Style:  
Search: