String Array problem in PowerPoint

B

bgmeindl

I'm having a problem with a string array and can't for the life of me
figure out what's going wrong.

I am creating a generic multiple choice/multiple answer slide that I
can then use for quizzes, etc.

I start by opening the text file for reading.
I then read the number of available questions [NumberOfQuestions =
Val(ts.readline)] [this is the first entry in the text file].

I then cycle through the number of available questions and fill a
series of arrays:
The first array holds the question itself [QuestionsArray(x) =
ts.readline 'question].
The second array holds the number of possible answers that will be
listed [QuestionSettingsArray(x, 0) = Val(ts.readline) 'number of
possible answers (max=5)]
The third array holds each of the possible answers
[PossibilitiesArray(x * 1) = ts.readline 'one of 5 possible answers]
[there are 5 of these statements to ensure that all possible answers
are read]
The fourth array holds whether the possible answer is correct or
incorrect [QuestionSettingsArray(x, 1) = Val(ts.readline) 'is this a
correct answer (1=correct; 0=incorrect)]

All of this goes swimmingly. However...

Once I have finished reading the text file and look to check my
PossibilitiesArray() the elements seem to be all over the place.
I have tested that the array is reading and placing the elements
correctly through the For...Next loop and everything seems to be ok. As
long as I am reading out of the array right away, there doesn't seem to
be a problem.

I'm going nuts here... and I can't seem to figure it out. I'm hoping
someone can help.

Thanks in advance for your time and energy on this.

Bob

Here is the entire sub (the msgbox statements are my tests to see
what's going on - they will also show you my problem - in the resulting
dialog the top portion should match the portion below the line):

--------CODE---------------
Sub Read_Data_From_File()

'use commented sections for writing data to a file

Const ForReading = 1, ForWriting = 2, ForAppending = 3
Const TristateUseDefault = -2, TristateTrue = -1, TristateFalse = 0
Dim fs, f, ts
Dim s As String
Dim y As Integer
Dim x As Integer

Set fs = CreateObject("Scripting.FileSystemObject")
'fs.CreateTextFile "test1.txt" 'Create a file
Set f = fs.GetFile("MC.txt")
'Set ts = f.OpenAsTextStream(ForWriting, TristateUseDefault)
'ts.Write "Hello World"
'ts.Close
Set ts = f.OpenAsTextStream(ForReading, TristateUseDefault)
Dim TextMessage2 As String
Dim TestMessage As String
NumberOfQuestions = Val(ts.readline)

For x = 1 To NumberOfQuestions
QuestionsArray(x) = ts.readline 'question
QuestionSettingsArray(x, 0) = Val(ts.readline) 'number of
possible answers (max=5)
PossibilitiesArray(x * 1) = ts.readline 'one of 5 possible
answers
QuestionSettingsArray(x, 1) = Val(ts.readline) 'is this a
correct answer (1=correct; 0=incorrect)
PossibilitiesArray(x * 2) = ts.readline 'another of the
possible answers
QuestionSettingsArray(x, 2) = Val(ts.readline) 'is this a
correct answer
PossibilitiesArray(x * 3) = ts.readline 'another of the
possible answers
QuestionSettingsArray(x, 3) = Val(ts.readline) 'is this a
correct answer
PossibilitiesArray(x * 4) = ts.readline 'another of the
possible answers
QuestionSettingsArray(x, 4) = Val(ts.readline) 'is this a
correct answer
PossibilitiesArray(x * 5) = ts.readline 'another possible
answer
TextMessage2 = TextMessage2 & PossibilitiesArray(x * 2) & vbCrLf
QuestionSettingsArray(x, 5) = Val(ts.readline) 'is this a
correct answer
Next
For y = 1 To 5
TestMessage = TestMessage & PossibilitiesArray(y * 2) & vbCrLf
Next
MsgBox TestMessage & vbCrLf & "-------------" & vbCrLf & TextMessage2

ts.Close

End Sub

-------------End Code------------

for further information, here is the text file I am reading:

-----------Beginning of Text file-----------
5
A PDP...
4
Approves market and customer impacting initiatives
1
Ensures company wide communication
1
Is a legal document that meets Sarbanes Oxley and other regulatory
guidelines
1
Is really not that important
0

0
Which of the following is NOT a reason for having PDPs?
4
Keep everyone up to date
0
Increase stress on Rogers' employees
1
Support shareholders
0
Ensure a positive customer experience
0

0
Which is the third step in the PDP process?
5
1-Page Summary
0
Cross-Functional Pre-Work
0
Approvals
1
Implementation
0
Launch
0
What is the average time for a standard PDP from submission to launch?
4
Six days
0
Six weeks
1
Six months
0
Who knows?!
0

0
On the PDP Summary page of pdp.rogers.com you can find...
4
Program launch date
1
Final Approval level
1
Approval status of a program
1
The PDP business case
0

0
------------------End of text File------------
 
D

David M. Marcovitz

I'm sure that this is a silly question, but where are you declaring your
arrays. I assume that you declare them at the beginning of the module,
but if you don't, that could be a problem. Also, once you have read the
number of questions, do you want to Redim your arrays?
--David

--
David M. Marcovitz
Microsoft PowerPoint MVP
Director of Graduate Programs in Educational Technology
Loyola College in Maryland
Author of _Powerful PowerPoint for Educators_
http://www.PowerfulPowerPoint.com/

(e-mail address removed) wrote in @j44g2000cwa.googlegroups.com:
I'm having a problem with a string array and can't for the life of me
figure out what's going wrong.

I am creating a generic multiple choice/multiple answer slide that I
can then use for quizzes, etc.

I start by opening the text file for reading.
I then read the number of available questions [NumberOfQuestions =
Val(ts.readline)] [this is the first entry in the text file].

I then cycle through the number of available questions and fill a
series of arrays:
The first array holds the question itself [QuestionsArray(x) =
ts.readline 'question].
The second array holds the number of possible answers that will be
listed [QuestionSettingsArray(x, 0) = Val(ts.readline) 'number of
possible answers (max=5)]
The third array holds each of the possible answers
[PossibilitiesArray(x * 1) = ts.readline 'one of 5 possible answers]
[there are 5 of these statements to ensure that all possible answers
are read]
The fourth array holds whether the possible answer is correct or
incorrect [QuestionSettingsArray(x, 1) = Val(ts.readline) 'is this a
correct answer (1=correct; 0=incorrect)]

All of this goes swimmingly. However...

Once I have finished reading the text file and look to check my
PossibilitiesArray() the elements seem to be all over the place.
I have tested that the array is reading and placing the elements
correctly through the For...Next loop and everything seems to be ok. As
long as I am reading out of the array right away, there doesn't seem to
be a problem.

I'm going nuts here... and I can't seem to figure it out. I'm hoping
someone can help.

Thanks in advance for your time and energy on this.

Bob

Here is the entire sub (the msgbox statements are my tests to see
what's going on - they will also show you my problem - in the resulting
dialog the top portion should match the portion below the line):

--------CODE---------------
Sub Read_Data_From_File()

'use commented sections for writing data to a file

Const ForReading = 1, ForWriting = 2, ForAppending = 3
Const TristateUseDefault = -2, TristateTrue = -1, TristateFalse = 0
Dim fs, f, ts
Dim s As String
Dim y As Integer
Dim x As Integer

Set fs = CreateObject("Scripting.FileSystemObject")
'fs.CreateTextFile "test1.txt" 'Create a file
Set f = fs.GetFile("MC.txt")
'Set ts = f.OpenAsTextStream(ForWriting, TristateUseDefault)
'ts.Write "Hello World"
'ts.Close
Set ts = f.OpenAsTextStream(ForReading, TristateUseDefault)
Dim TextMessage2 As String
Dim TestMessage As String
NumberOfQuestions = Val(ts.readline)

For x = 1 To NumberOfQuestions
QuestionsArray(x) = ts.readline 'question
QuestionSettingsArray(x, 0) = Val(ts.readline) 'number of
possible answers (max=5)
PossibilitiesArray(x * 1) = ts.readline 'one of 5 possible
answers
QuestionSettingsArray(x, 1) = Val(ts.readline) 'is this a
correct answer (1=correct; 0=incorrect)
PossibilitiesArray(x * 2) = ts.readline 'another of the
possible answers
QuestionSettingsArray(x, 2) = Val(ts.readline) 'is this a
correct answer
PossibilitiesArray(x * 3) = ts.readline 'another of the
possible answers
QuestionSettingsArray(x, 3) = Val(ts.readline) 'is this a
correct answer
PossibilitiesArray(x * 4) = ts.readline 'another of the
possible answers
QuestionSettingsArray(x, 4) = Val(ts.readline) 'is this a
correct answer
PossibilitiesArray(x * 5) = ts.readline 'another possible
answer
TextMessage2 = TextMessage2 & PossibilitiesArray(x * 2) & vbCrLf
QuestionSettingsArray(x, 5) = Val(ts.readline) 'is this a
correct answer
Next
For y = 1 To 5
TestMessage = TestMessage & PossibilitiesArray(y * 2) & vbCrLf
Next
MsgBox TestMessage & vbCrLf & "-------------" & vbCrLf & TextMessage2

ts.Close

End Sub

-------------End Code------------

for further information, here is the text file I am reading:

-----------Beginning of Text file-----------
5
A PDP...
4
Approves market and customer impacting initiatives
1
Ensures company wide communication
1
Is a legal document that meets Sarbanes Oxley and other regulatory
guidelines
1
Is really not that important
0

0
Which of the following is NOT a reason for having PDPs?
4
Keep everyone up to date
0
Increase stress on Rogers' employees
1
Support shareholders
0
Ensure a positive customer experience
0

0
Which is the third step in the PDP process?
5
1-Page Summary
0
Cross-Functional Pre-Work
0
Approvals
1
Implementation
0
Launch
0
What is the average time for a standard PDP from submission to launch?
4
Six days
0
Six weeks
1
Six months
0
Who knows?!
0

0
On the PDP Summary page of pdp.rogers.com you can find...
4
Program launch date
1
Final Approval level
1
Approval status of a program
1
The PDP business case
0

0
------------------End of text File------------
 
B

Bob G

David

Actually, I dimension the arrays in the Declarations section so they
can be used in other Subs in the program.

At this point I hard code the sizes of the arrays as I know the number
of questions. In a future iteration of the program I will look to make
it more flexible.

Bob
 
S

Steve Rindsberg

Can you post the relevant section of code?

Actually, I dimension the arrays in the Declarations section so they
can be used in other Subs in the program.

At this point I hard code the sizes of the arrays as I know the number
of questions. In a future iteration of the program I will look to make
it more flexible.

Bob
 
B

Bob G

Here are the Declarations with the relevant ones starred:

Dim CurrentSlide As Integer
Dim QuestionsArray(10) As String <----*[to hold the questions
themselves]

Dim QuestionSettingsArray(10, 6) As Integer <----* [how many
possible answers and is this to be considered a correct answer]

Dim PossibilitiesArray(50) As String <-----* [possible answers -
max of 5 for each ?]

Dim NumberOfQuestions As Integer <-----* [total number of
questions - first entry in txt file]

Dim QuestionNumber As Integer
Dim NumberOfAnswers As Integer
Dim MC_CurrentSlide As Integer
Dim QuestionText As String
Dim Correct(5) As String
Dim Answers(5) As String
Dim AnswerOrder(5) As Integer
Dim ChosenAnswers(5) As Integer

Bob
 
S

Steve Rindsberg

Here are the Declarations with the relevant ones starred:

Good ... and the code?

BTW, if using VB/VBA, you'll generally want to use Longs wherever you're using
Integers here. IIRC, in .NET, an Integer is what VB/VBA calls a Long so if
using .Net, .NOT a problem and .NEVermind
Dim CurrentSlide As Integer
Dim QuestionsArray(10) As String <----*[to hold the questions
themselves]

Dim QuestionSettingsArray(10, 6) As Integer <----* [how many
possible answers and is this to be considered a correct answer]

Dim PossibilitiesArray(50) As String <-----* [possible answers -
max of 5 for each ?]

Dim NumberOfQuestions As Integer <-----* [total number of
questions - first entry in txt file]

Dim QuestionNumber As Integer
Dim NumberOfAnswers As Integer
Dim MC_CurrentSlide As Integer
Dim QuestionText As String
Dim Correct(5) As String
Dim Answers(5) As String
Dim AnswerOrder(5) As Integer
Dim ChosenAnswers(5) As Integer

Bob

Steve said:
Can you post the relevant section of code?



-----------------------------------------
Steve Rindsberg, PPT MVP
PPT FAQ: www.pptfaq.com
PPTools: www.pptools.com
================================================
 
B

Bob G

I listed the code in my original post.

Bob

Steve said:
Here are the Declarations with the relevant ones starred:

Good ... and the code?

BTW, if using VB/VBA, you'll generally want to use Longs wherever you're using
Integers here. IIRC, in .NET, an Integer is what VB/VBA calls a Long so if
using .Net, .NOT a problem and .NEVermind
Dim CurrentSlide As Integer
Dim QuestionsArray(10) As String <----*[to hold the questions
themselves]

Dim QuestionSettingsArray(10, 6) As Integer <----* [how many
possible answers and is this to be considered a correct answer]

Dim PossibilitiesArray(50) As String <-----* [possible answers -
max of 5 for each ?]

Dim NumberOfQuestions As Integer <-----* [total number of
questions - first entry in txt file]

Dim QuestionNumber As Integer
Dim NumberOfAnswers As Integer
Dim MC_CurrentSlide As Integer
Dim QuestionText As String
Dim Correct(5) As String
Dim Answers(5) As String
Dim AnswerOrder(5) As Integer
Dim ChosenAnswers(5) As Integer

Bob

Steve said:
Can you post the relevant section of code?


Actually, I dimension the arrays in the Declarations section so they
can be used in other Subs in the program.

At this point I hard code the sizes of the arrays as I know the number
of questions. In a future iteration of the program I will look to make
it more flexible.

Bob

David M. Marcovitz wrote:
I'm sure that this is a silly question, but where are you declaring your
arrays. I assume that you declare them at the beginning of the module,
but if you don't, that could be a problem. Also, once you have read the
number of questions, do you want to Redim your arrays?
--David

--
David M. Marcovitz
Microsoft PowerPoint MVP
Director of Graduate Programs in Educational Technology
Loyola College in Maryland
Author of _Powerful PowerPoint for Educators_
http://www.PowerfulPowerPoint.com/




-----------------------------------------
Steve Rindsberg, PPT MVP
PPT FAQ: www.pptfaq.com
PPTools: www.pptools.com
================================================

-----------------------------------------
Steve Rindsberg, PPT MVP
PPT FAQ: www.pptfaq.com
PPTools: www.pptools.com
================================================
 
S

Steve Rindsberg

I listed the code in my original post.

So you did ... my bad.

Here's the part that makes me suspicious - step through what happens with
PossibilitiesArray for each value of x

X = 1
' values go into:
PossibilitiesArray(1)
PossibilitiesArray(2)
PossibilitiesArray(3)
... and so on
PossibilitiesArray(5)
X = 2
' values go into
PossibilitiesArray(2)
PossibilitiesArray(4)
PossibilitiesArray(6)
...
PossibilitiesArray(10)
x = 3
' values go into
PossibilitiesArray(3)
PossibilitiesArray(6)
PossibilitiesArray(9)

...

so you're droppping new values into some array elements, overwriting existing ones,
and skipping others, no?

Try assigning values to

PossibilitiesArray( ((x-1)*5)+x )

(I think I've got that right)

========================================

For x = 1 To NumberOfQuestions
QuestionsArray(x) = ts.readline 'question
QuestionSettingsArray(x, 0) = Val(ts.readline) 'number of possible
answers (max=5)
PossibilitiesArray(x * 1) = ts.readline 'one of 5 possible answers
QuestionSettingsArray(x, 1) = Val(ts.readline) 'is this a correct answer
(1=correct; 0=incorrect)
PossibilitiesArray(x * 2) = ts.readline 'another of the possible
answers
QuestionSettingsArray(x, 2) = Val(ts.readline) 'is this a correct answer
PossibilitiesArray(x * 3) = ts.readline 'another of the possible
answers
QuestionSettingsArray(x, 3) = Val(ts.readline) 'is this a correct answer
PossibilitiesArray(x * 4) = ts.readline 'another of the possible
answers
QuestionSettingsArray(x, 4) = Val(ts.readline) 'is this a correct answer
PossibilitiesArray(x * 5) = ts.readline 'another possible answer
TextMessage2 = TextMessage2 & PossibilitiesArray(x * 2) & vbCrLf
QuestionSettingsArray(x, 5) = Val(ts.readline) 'is this a
correct answer
Next
 
B

Bob G

Steve

Thank you! You're right. I didn't stop to see what I was actually
doing, only what I thought I was doing. (Forest not trees syndrome). I
need to go back and rethink the logic here.

Thanks again for your time, energy, and thought on this. I really
appreciate it.

Bob
 
S

Steve Rindsberg

My pleasure, Bob. Glad we got it sorted out
Thank you! You're right. I didn't stop to see what I was actually
doing, only what I thought I was doing. (Forest not trees syndrome). I
need to go back and rethink the logic here.

Thanks again for your time, energy, and thought on this. I really
appreciate it.

Bob
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top