Jump to content


Photo

Visual Basic help


  • Please log in to reply
18 replies to this topic

#1 N-sane Noob

N-sane Noob

    Noob Leader

  • Publishing Betazoids
  • PipPipPip
  • 873 posts
  • Gender:Male
  • Location:Canada
  • Interests:Taking over stuff

Posted 10 January 2007 - 09:12 PM

My question is how do I make an ongoing average. For example I have a list and and everytime I enter a number the number will appear on the list one under another. How do I make an average of those number. But lets say i find the average of those numbers but when i enter another number, the new average should appear.

Another question:

I know this sounds pretty easy but how do I write a list to file and also read it.

Edited by N-sane Noob, 10 January 2007 - 09:14 PM.


#2 EclipsE

EclipsE

    Young Padawan

  • Members
  • Pip
  • 22 posts
  • Gender:Male
  • Location:Belgrade, Serbia
  • Interests:Programming, skating, rapping...

Posted 16 January 2007 - 10:05 PM

VB... my speciality :)
Ok, now, we go from the start. Make a Listbox, textbox, commandbutton and a label, and give them some names like lstNumbers, txtNumber, cmdAdd, lblAverage. From the names you should see what is what. Now, in cmdAdd_Click procedure, add this:

If IsNumeric(txtNumber.Text) Then
	lstNumbers.AddItem txtNumber.Text
	txtNumber.Text = ""
	CalculateAverage
Else
	MsgBox "Only numbers are allowed!"
End If

The first line checks if only numbers are present in the textbox. If it's true, then add the number to the list, empty the textbox and call the sub CalculateAverage, see down... If not, display a message box with info "Only numbers are allowed!". Easy. Moving on...

Private Sub CalculateAverage()
Dim i As Integer
Dim averagenum As Double

For i = 0 to lstNumbers.ListCount - 1

	averagenum = averagenum + CDbl(lstNumbers.List(i))

Next i

averagenum = averagenum / lstNumbers.ListCount
lblAverage.Caption = "Average Number is: " & averagenum

End Sub

Easy, ain't it? Hehe...

Sorry, in the next code i did not capitalise some words and did some spaces but i want to hurry a little bit...
As for file input and output, see this:
For output:
Dim i as integer, ff as integer
ff=freefile   'get a free file

open "c:\list.lst" for output as #ff   'open the file for ouput 

	for i = 0 to lstNumbers.ListCount-1  'from the first to the last in the list
		print #ff, lstNumbers.List(i)		 'write it down
	next i	 'next

close #ff   'close the file

For input:
Dim ff as integer, aline as string
ff=freefile   'get a free file

if not dir("c:\list.lst") then exit sub   'if the file does not exist exit the sub

open "c:\list.lst" for input as #ff	'open file

	while not eof(ff)   'while we are not at the end of the file
	
		line input #ff, aline   'read a line
		lstnumbers.additem aline  'add to the list

	wend

	'we can calculate the average now
	CalculateAverage

close #ff   'close file

If you're stuck somewhere, say it!

#3 N-sane Noob

N-sane Noob

    Noob Leader

  • Publishing Betazoids
  • PipPipPip
  • 873 posts
  • Gender:Male
  • Location:Canada
  • Interests:Taking over stuff

Posted 16 January 2007 - 10:35 PM

thanks a lot. Know I have another problem. I am doing a who wants to be a millionaire game and I am trying to randomize the questions but a good way I found is ARRAYS but I know very little about arrays.

Is there an easier way?

#4 EclipsE

EclipsE

    Young Padawan

  • Members
  • Pip
  • 22 posts
  • Gender:Male
  • Location:Belgrade, Serbia
  • Interests:Programming, skating, rapping...

Posted 17 January 2007 - 01:11 PM

The easiest thing is - arrays :P
It isn't complicated at all. Array 101 - starting with arrays

So, what is an array? An array is a variable of a type(String, Integer, Long etc...) that has many members. I can't explain it well. But, here is a sample:

Dim strQuestions(10) As String

strQuestions(0) = "How much money does Bill Gates have?"
strQuestions(1) = "When was the first computer invented?"
etc etc...

The first line declares an array with 10 members, the type of string. But the lower bound is 0 so it means we have 11 members. We could do this:

Dim strQuestions(1 To 10) As String
And now we have 10 members starting from 1 to 10.

Now, the other lines in the example.
strQuestions(0) = "How much money does Bill Gates have?"
and the other one
strQuestions(1) = "When was the first computer invented?"

We can assign a value to an array member just like to some variables.

ArrayName(MemberIndex) = Value

That simple :)

Now, you say you were doing a game like millionare. A easy way is to do it with arrays of user types. Check this:

Private Type TQuestion
	strQuestion As String
	strAnswer As String
	strAnswers(1 To 4) As String
End Type

Dim arrQuestion(1 To 10) As TQuestion

You can manage every value of the arrQuestions array. Like this:

With arrQuestions(1)
	.strQuestion = "How much money does Bill Gates have?"
	.strAnswer = "Noone knows!"
	.strAnswers(1) = "10 billion dollars"
	.strAnswers(2) = "20 billion dollars"
	.strAnswers(3) = "50 billion dollars"
	.strAnswers(4) = "Noone knows!"
End With

With arrQuestions(2)
	 .strQuestion = "A question"
	 .strAnswer = "The right answer"
	 .strAnswers(1) = "Wrong answer"
	 .strAnswers(2) = "Wrong answer"
	 .strAnswers(3) = "The right answer"
	 .strAnswers(4) = "Wrong answer"
 End With

And the rest you got to figure out! If you need help or if you want me to make the quiz (i can do it very fast) just say! I'll do anything to help people!

:lol:

#5 N-sane Noob

N-sane Noob

    Noob Leader

  • Publishing Betazoids
  • PipPipPip
  • 873 posts
  • Gender:Male
  • Location:Canada
  • Interests:Taking over stuff

Posted 17 January 2007 - 01:59 PM

Yes please. If you have time...I would appreciate 100% and when you will ever need help I'll be the first one to offer :rolleyes:

#6 EclipsE

EclipsE

    Young Padawan

  • Members
  • Pip
  • 22 posts
  • Gender:Male
  • Location:Belgrade, Serbia
  • Interests:Programming, skating, rapping...

Posted 17 January 2007 - 03:08 PM

:) I even made it with a cool GUI!

Usually, i would recommend to store questions and answers in a seperate file crypted but still, it's your choice! (if you want to do that i can help too :rolleyes: )

If you need anything, say! I'm always here to help

btw, The project is commented, but still if you don't understand something, i'm here...

Attached Files


Edited by EclipsE, 17 January 2007 - 03:09 PM.


#7 N-sane Noob

N-sane Noob

    Noob Leader

  • Publishing Betazoids
  • PipPipPip
  • 873 posts
  • Gender:Male
  • Location:Canada
  • Interests:Taking over stuff

Posted 21 January 2007 - 01:13 PM

lol...thanks a lot...but the thing is this is to professional for me.....u even used GUI ...lol...all i wanted was some command buttons and random question by using arrays :D

#8 EclipsE

EclipsE

    Young Padawan

  • Members
  • Pip
  • 22 posts
  • Gender:Male
  • Location:Belgrade, Serbia
  • Interests:Programming, skating, rapping...

Posted 21 January 2007 - 04:06 PM

Well... Just, think of the labels as command buttons! If the labels were command buttons and there was no GUI, the code would be... the same...

#9 N-sane Noob

N-sane Noob

    Noob Leader

  • Publishing Betazoids
  • PipPipPip
  • 873 posts
  • Gender:Male
  • Location:Canada
  • Interests:Taking over stuff

Posted 21 January 2007 - 05:11 PM

lol sorry i am really bad at this..:D

#10 EclipsE

EclipsE

    Young Padawan

  • Members
  • Pip
  • 22 posts
  • Gender:Male
  • Location:Belgrade, Serbia
  • Interests:Programming, skating, rapping...

Posted 21 January 2007 - 07:57 PM

You're not the first and not the last... It reminds me when i was a begginer, except i needed to figure out everything all by myself... Man, i am programming for like 7 years in VB so, you're a lucky man having someone to help you on your way to success! :closedeyes:

#11 N-sane Noob

N-sane Noob

    Noob Leader

  • Publishing Betazoids
  • PipPipPip
  • 873 posts
  • Gender:Male
  • Location:Canada
  • Interests:Taking over stuff

Posted 21 January 2007 - 08:01 PM

lol...to tell u the secret....this is for a school project....im getting most of the stuff.....but arrays is for a higher grade...

#12 EclipsE

EclipsE

    Young Padawan

  • Members
  • Pip
  • 22 posts
  • Gender:Male
  • Location:Belgrade, Serbia
  • Interests:Programming, skating, rapping...

Posted 22 January 2007 - 04:58 AM

:)

Anything i could do for you?

#13 N-sane Noob

N-sane Noob

    Noob Leader

  • Publishing Betazoids
  • PipPipPip
  • 873 posts
  • Gender:Male
  • Location:Canada
  • Interests:Taking over stuff

Posted 22 January 2007 - 01:18 PM

I wish you could make me...only if u want....a new one with only command buttons and just the label with questions......using arrays,,,,thank you..please please ;)....like....newbish looking...lol

#14 EclipsE

EclipsE

    Young Padawan

  • Members
  • Pip
  • 22 posts
  • Gender:Male
  • Location:Belgrade, Serbia
  • Interests:Programming, skating, rapping...

Posted 22 January 2007 - 03:24 PM

Here you go! No user defined types, only arrays. And one 2-dimensional array, the one with the answers.

Oh, and remove the comments if you are going to use it for a school project... :)

Attached Files



#15 N-sane Noob

N-sane Noob

    Noob Leader

  • Publishing Betazoids
  • PipPipPip
  • 873 posts
  • Gender:Male
  • Location:Canada
  • Interests:Taking over stuff

Posted 22 January 2007 - 03:56 PM

lol....man..ur too good......even tho this is newbish for u ...its professional for what i learn....lol....theres stuff like init question....and a lot that i didnt learn....what i needed was this.....4 command buttons and random questions and the money......lol....thats really pro.....i just wanted something so simple.....the code is very advanced for me....lol

#16 EclipsE

EclipsE

    Young Padawan

  • Members
  • Pip
  • 22 posts
  • Gender:Male
  • Location:Belgrade, Serbia
  • Interests:Programming, skating, rapping...

Posted 22 January 2007 - 05:43 PM

That's simple! You got 4 command buttons and random questions, and the money

InitQuestions:

In this sub you init the questions, give them answers and what is the right answer. An example:

strQuestion(1) = "What's my creator's display name on p2l?"
strAnswers(1, 1) = "Daredevil"
 strAnswers(1, 2) = "EclipsE"
 strAnswers(1, 3) = "DaCr00k"
 strAnswers(1, 4) = "ZeroCool"
strAnswerIndex(1) = 2

The first line is the question you want. It'll be the first question because between ( and ) is the number 1. To init the second question you would write:

strQuestion(2) = "My question"

and so on and so on...

Beware that the number of questions is limited, there can only be 10 questions in this example because of this:

Private strQuestion(1 To 10) As String
Private strAnswerIndex(1 To 10) As Integer
Private strAnswers(1 To 10, 1 To 4) As String

You see that everywhere says 1 To 10. To make more questions change the 10 in every line to the number of questions you want to have.

Next, if you make more questions be sure to change the following lines:

currentQuestion = Int(Rnd * 11)
If currentQuestion < 1 Then currentQuestion = 1
If currentQuestion > 10 Then currentQuestion = 10

to

currentQuestion = Int(Rnd * (NumberOfQuestions + 1))
 If currentQuestion < 1 Then currentQuestion = 1
 If currentQuestion > NumberOfQuestions Then currentQuestion = NumberOfQuestions

replace NumberOfQuestions with how many questions do you have.

and replace this

For i = 1 To 10

with

For i = 1 To NumberOfQuesions

Again, the numberofquestions is a number that represents how much questions do you have.

Ok, let's go back to the init routine:

strAnswers(1, 1) = "Daredevil"
 strAnswers(1, 2) = "EclipsE"
 strAnswers(1, 3) = "DaCr00k"
 strAnswers(1, 4) = "ZeroCool"

That are the possible answers to the question, and one of them is the right answer. To set some possible answers type this:

strAnswers(numQ, 1) = "answer1"
  strAnswers(numQ, 2) = "answer2"
  strAnswers(numQ, 3) = "answer3"
  strAnswers(numQ, 4) = "answer4"

Where numQ is the question number to who you want to set the possible answers. numbers after the question number, can be 1, 2, 3 or 4. They represent the A, B, C and D answer on the command buttons. The text in the "" is the answer.

and the last thing:

strAnswerIndex(1) = 2

This means that the right answer to the question 1 is the second answer, and if you see the lines up you'll see that the second answer is EclipsE. Look at my display name... It says it all :)
To use the AnswerIndex

strAnswerIndex(numQ) = TheRightAnswer

numQ - the question number
TheRightAnswer - the number of the answer that is correct, can be 1, 2, 3 or 4

Ok, that's the init questions sub.

In RandomizeQuestions we do the following:

Randomize Everything with the command Randomize, this means that the program will start differently every time it is executed. Without this, the questions would be random, but always in the same order every time the program has been started.
Then we make a random number from 0 to 1 and using Rnd and then multiplying it with 11, and rounding it to a whole number using Int.
Then we check if the number is smaller than 1 and if it is, make it 1.
If the number is larger than 10, we make it 10.

And then comes the tricky part. You see, every time you give the correct answer i put the question number in a string so we would not come to that question again. Example, if i answered on questions 3, 6 and 7 the string would be like this: "|3||6||7|"
Now we check if the random number we got is a question that already was answered from the program start using InStr. We check if there is "|" the question number "|" in the string. If not, we got our next question!
But if we got it.... Well, we'll use a trick.

We'll loop from 1 to the last question and see if the question is free(not in the string). If it's not in the string we take it, set the currentQuestion and Exit For. If it's in the string we set currentQuestion to 0.

Eventually, we would go answer all questions. That's why we use currentQuestion and set it to 0. If it's 0 after the For - Next, that means no questions are left.... And you are a millionare!

I maybe left something and maybe you'll not understand everything but hey.......

:)

#17 N-sane Noob

N-sane Noob

    Noob Leader

  • Publishing Betazoids
  • PipPipPip
  • 873 posts
  • Gender:Male
  • Location:Canada
  • Interests:Taking over stuff

Posted 22 January 2007 - 08:35 PM

Okay....i have changed most of the stufff....but im not getting this...

If currentQuestion <> 0 Then DoneQuestions = DoneQuestions & "|" & currentQuestion & "|"

and this

If currentQuestion < 1 Then currentQuestion = 1
If currentQuestion > 20 Then currentQuestion = 20


#18 EclipsE

EclipsE

    Young Padawan

  • Members
  • Pip
  • 22 posts
  • Gender:Male
  • Location:Belgrade, Serbia
  • Interests:Programming, skating, rapping...

Posted 23 January 2007 - 05:41 AM

Easy.

If currentQuestion <> 0 Then DoneQuestions = DoneQuestions & "|" & currentQuestion & "|"

If the current question isn't 0 we add it to the done questions string so we know later that the question has been used.
But if it's 0 nothing will happen cuz that will mean that the game is finished.

If currentQuestion < 1 Then currentQuestion = 1
If currentQuestion > 20 Then currentQuestion = 20

We check for the boundaries. Sometimes the current question after randomizing can be 0. Then we check if it's less than 1, and if it is we set it to 1.
If the question after randomizing is larger than the max questions, we set it to 20(in your case).

#19 EclipsE

EclipsE

    Young Padawan

  • Members
  • Pip
  • 22 posts
  • Gender:Male
  • Location:Belgrade, Serbia
  • Interests:Programming, skating, rapping...

Posted 28 January 2007 - 04:23 PM

I did but all on serbian... I'm toooooo lazy to translate them :)




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users