Also, if you changed the answer options to an array, cmdAnswer(1) to cmdAnswer(4), and then set dblAnswerReal2 (or whatever you want to name it, I'd personally stick with "dblCorrectAnswer" or something easily recogniseable) to 4 or whichever of the answers is right (I assume you might have other questions with a different answer) you'd probably find the code to be a lot more flexible and modifiable in future...
...for instance, you could:
* Jumble up the possible answers so the right one isn't always in the same place
* Set up the 4 lines which fill cmdAnswer1 to cmdAnswer4 into only 3 lines using a loop (if you stored the questions in an array too)
* Have more than 4 possible answers but only display the correct one and 3 possible wrong answers (adds to the replay value)
And I am sure there's other stuff that could be done...and of course this post is only useful if you're actually doing more than one question, so if it's just the one it's probably better to ignore me :-)
I love helping noobs with their VB problems (probably because, as an amateur programmer, I am only slightly better at VB than them :-)) but if you SERIOUSLY want to get help for free from a community such as VBForums, you have to first have a grounding (basic knowledge) in VB6, otherwise you're way too much work to help...You've got to give a little if you want to get help from us, in other words!
And we DON'T do your homework. If your tutor doesn't teach you enough to help you make the project without his or her help, FIND A BETTER TUTOR or try reading books on programming! We are happy to help with minor things regarding the project, but you have to understand the rest of it if you want our help to be useful.
i am doing like over 50 questions for my exams but just for test run over the holidays iam trying to make a game with just 4 questions. I changed the things to string and they work but now the stupid thing is the answers are getting messed up for question number 2, 3 and 4. Just to let you know i have been only programming for like 3 months and iam not used to high level vs terminology lol, can any of you help me out with the problems iam having.
problems:
- answers not working
- stupid ways of making the code (using alot of ors in 1 algorithm)
- and any others mistakes you find
vb Code:
Option Explicit
'Hassan Waheed
' exam
Private strAnswerReal1 As String
Private strAnswerReal2 As String
Private strAnswerReal3 As String
Private strAnswerReal4 As String
Private dblMoney As Double
Private dblQuestionAccumaltor As Double
Private Sub cmdInstructions_Click()
MsgBox "Hello, Player. Welcome to the who wants to be a" _
& " Hassanillionaire. You are gonna start off with $500 and" _
& " for every question you get right you gain $2000 but if" _
& " you get 1 question wrong then you lose $1000." _
& " Your goal is to win as much cash as you can."
End Sub
Private Sub cmdStart_Click()
dblMoney = 500
dblQuestionAccumaltor = dblQuestionAccumaltor + 1
MsgBox " Get ready to wrestle your mind for a Million dollars"
If dblQuestionAccumaltor = 1 Then
Call Question1
End If
End Sub
Private Sub cmdAnswer1_Click()
If cmdAnswer1.Caption = strAnswerReal1 Or cmdAnswer1.Caption = strAnswerReal2 _
Or cmdAnswer1.Caption = strAnswerReal3 Or cmdAnswer1.Caption = strAnswerReal4 Then
Just quickly perusing through the code, it does seem a bit bloated...but that's due to your level of expertise in VB6 so it's allowed :-)
An array is a group of variables with the same name that are recognised in code as different by the number in brackets after the name. For instance, you have:
Code:
Sub Question1()
lblQuestionBox.Caption = "When was pakistan first made?"
cmdAnswer1.Caption = " 1947 "
cmdAnswer2.Caption = " 1961 "
cmdAnswer3.Caption = " 1945 "
cmdAnswer4.Caption = " 1955 "
strAnswerReal1 = "1947"
End Sub
Whereas most people would use an array to store the values in (for instance) strQuestion(1) and strAnswer(1,1) to strAnswer(1,4) like so...
These variables would be declared as public elsewhere so they can be set on form_load (or elsewhere...personally I would save the questions in a text file and load them into the program as I run it) and all you then have to do is set the control captions on your form with what's in the array.
There's a lot more I want to say (like how to get rid of having to have the "right answer" variable by using the jumbling idea I said above and having the first answer in the list always being the right one) but I'm holding back on saying too much so it doesn't confuse you :-)
With regards to your actual question about the bug, I'm not sure without actually trying the code for myself, and hopefully someone else can spot it as I didn't see anything after a quick look through
I love helping noobs with their VB problems (probably because, as an amateur programmer, I am only slightly better at VB than them :-)) but if you SERIOUSLY want to get help for free from a community such as VBForums, you have to first have a grounding (basic knowledge) in VB6, otherwise you're way too much work to help...You've got to give a little if you want to get help from us, in other words!
And we DON'T do your homework. If your tutor doesn't teach you enough to help you make the project without his or her help, FIND A BETTER TUTOR or try reading books on programming! We are happy to help with minor things regarding the project, but you have to understand the rest of it if you want our help to be useful.
thank you, i have been researching for the past few days and almost everysingle trvia who wants to be a mllionare games i found contain a text file
(notepad, word) where all the questions are stored. I want to do that too but the thing is that i dont know how to do. we havnt learned that in the course.
it would be greatly appreciated if you show me a code with just 1 question in a text file. and could you also show me how u check if the answer is right or not. i mean like when the person clicks the command button how would you check that the caption of that command button is the answer.
thank you, i have been researching for the past few days and almost everysingle trvia who wants to be a mllionare games i found contain a text file
(notepad, word) where all the questions are stored. I want to do that too but the thing is that i dont know how to do. we havnt learned that in the course.
it would be greatly appreciated if you show me a code with just 1 question in a text file. and could you also show me how u check if the answer is right or not. i mean like when the person clicks the command button how would you check that the caption of that command button is the answer.
Give me 5 minutes and I'll write a quick project with the same questions you provided above...although it might be a bit advanced if I add all the other stuff (like jumbling the answers) but I'll do what I can :-)
I love helping noobs with their VB problems (probably because, as an amateur programmer, I am only slightly better at VB than them :-)) but if you SERIOUSLY want to get help for free from a community such as VBForums, you have to first have a grounding (basic knowledge) in VB6, otherwise you're way too much work to help...You've got to give a little if you want to get help from us, in other words!
And we DON'T do your homework. If your tutor doesn't teach you enough to help you make the project without his or her help, FIND A BETTER TUTOR or try reading books on programming! We are happy to help with minor things regarding the project, but you have to understand the rest of it if you want our help to be useful.
thanks alot, i have some other questions too it would be great if you could help me, do you have msn or something?
I don't use IM, and there is no need - like many people here I get instant email notification of replies to threads I have posted in, and will come back when I have the time to do so (if I am already on the forums, that may be a matter of seconds after your post).
- answers not working
That is because you have the Or's, which are not what you want. For example, in this part of the code:
Code:
Private Sub cmdAnswer1_Click()
If cmdAnswer1.Caption = strAnswerReal1 Or cmdAnswer1.Caption = strAnswerReal2 _
Or cmdAnswer1.Caption = strAnswerReal3 Or cmdAnswer1.Caption = strAnswerReal4 Then
You either need to make the checks much more complex (for each of the 4 conditions also check if that is the current question number), or make it much simpler - by having just one strAnswerReal variable (rather than one per question), and only checking that one variable.
- stupid ways of making the code (using alot of ors in 1 algorithm)
- and any others mistakes you find
You have bad organisation which leads to repetition etc, including:
If you move lines 38-44 to just after the End If, you can remove lines 48-54 (as they do exactly the same thing, and in either case one of them will run. The same applies to the other buttons.
It would be better to have just one routine for the questions (using If statements or a Select Case to choose which part to run based on dblQuestionAccumaltor), rather than 4 separate ones. You can then remove lines 58-68 (and equivalents) from each button, and can even move lines 38-44 into it too.
These two (fairly simple) changes should reduce the amount of code by about 50%... and make it much simpler to alter/maintain too.
it would be greatly appreciated if you show me a code with just 1 question in a text file.
For getting data from text files, see the "Files" section of our Classic VB FAQs(in the FAQ forum)
Last edited by si_the_geek; Dec 22nd, 2009 at 03:05 PM.
o man thanks that makes sence but i read those FAQs already and i dont even understand that stuff since it is pretty advance. i am waiting for the other person to reply, he helped me out alot.
Okay, I've chucked together a basic quiz but I HAVEN'T done a few things including a scoring system and loading it from a text file...I'll leave that part up to you. I've also commented (probably not well enough) throughout the code where I could, and I welcome you to paste bits and ask about it and people will help.
ALSO, I am very lazy when it comes to variables, I rarely follow the naming conventions (proper names, using str at the start so you can tell it's a string, the usual) so forgive some of the weird variable names in it.
I'm sure you can take this code and modify it to your hearts content with other features...this will get you started though :-P
One thing you might want to do is change it so it doesn't use the msgbox when telling you right or wrong, I just added that to make it quick :-P
I love helping noobs with their VB problems (probably because, as an amateur programmer, I am only slightly better at VB than them :-)) but if you SERIOUSLY want to get help for free from a community such as VBForums, you have to first have a grounding (basic knowledge) in VB6, otherwise you're way too much work to help...You've got to give a little if you want to get help from us, in other words!
And we DON'T do your homework. If your tutor doesn't teach you enough to help you make the project without his or her help, FIND A BETTER TUTOR or try reading books on programming! We are happy to help with minor things regarding the project, but you have to understand the rest of it if you want our help to be useful.
I do, but I don't give it out to people for discussing programming. Feel free to post questions about specific parts of the code, or you can do as most of us do and google for the term (like rnd or public, etc) and see what you can find out about how to use it in your program :-)
I love helping noobs with their VB problems (probably because, as an amateur programmer, I am only slightly better at VB than them :-)) but if you SERIOUSLY want to get help for free from a community such as VBForums, you have to first have a grounding (basic knowledge) in VB6, otherwise you're way too much work to help...You've got to give a little if you want to get help from us, in other words!
And we DON'T do your homework. If your tutor doesn't teach you enough to help you make the project without his or her help, FIND A BETTER TUTOR or try reading books on programming! We are happy to help with minor things regarding the project, but you have to understand the rest of it if you want our help to be useful.