locked
Do Loop: Add a field from a structure to a variable?

    Question

  • I'm a bit confused about what I'm being asked to do.

    A question for an assignment wants me to write a do loop to "...add what is in the Income field from the Schools structure to a variable named Total." The structure and array have already been established; I completely get how to do that. The problem is this question isn't making sense to me, and I've been going through my book, the internet, and all to try to figure out what's being asked of me. Anyone have any suggestions?

    I can provide more info if needed as well. Thanks a lot in advance.

    Sunday, May 01, 2011 7:36 PM

Answers

  • Doh, of course. Got it! So here's the final code when using integerIndex as identifier:

    Dim integerIndex As Integer = 0
    Dim Total As Decimal
    
    Do Until integerIndex > 150
     If Decimal.TryParse(SchList(integerIndex).incomeString, income) Then
     Total += income
     Else
     MessageBox.Show("Invalid income string in Schools item number" & integerIndex.ToString() & ". Item was skipped.")
     End If
     integerIndex += 1
    Loop
    

     I'd say it's finally in a completed state, correct? Thanks a ton for all the help, Blackwood, it's beyond very much appreciated.


    "My work is not a piece of writing designed to meet the taste of an immediate public but was done to last forever." - Thucydides, ancient Greek historian



    • Marked as answer by Salacon Sunday, May 01, 2011 10:50 PM
    • Edited by Salacon Sunday, May 01, 2011 10:51 PM Editing code to be as it should look when finished, so I can Mark As Answer for others that happen to need it.
    Sunday, May 01, 2011 10:40 PM

All replies

  • It sounds like you already have an array of School structures and that one of the fields in the School structure is called Income.  You are being asked to loop through the array and add each element's Income field to a variable called Total.

    When you say the array and the structure have already been established, do you mean you have been given code that defines them? If so, I suggest you post the code you have been given along with what you have written so far to try to complete the assignment,

    Sunday, May 01, 2011 7:46 PM
  • Sorry, should've had this information in the original post. Yes, I had to establish the structure and array earlier in the assignment. Here that is:

     

    This is the structure:

    Structure Schools

          Dim nameString, addressString, cityString, districtString, incomeString _

    As String

    End Structure

     

    This is array:

    Dim SchList (150) as Schools

     

    I completely get that I'm going to be using incomeString and, of course, a variable called Total, but for some reason I'm at a loss at exactly what to do as far as looping through Schools and incomeString.

     

    Sunday, May 01, 2011 8:11 PM
  • This code should do what you want.

    Dim Total As Decimal
    For Each sch As Schools In SchList
      Dim income As Decimal
      If Decimal.TryParse(sch.incomeString, income) Then
        Total += income
      Else
        MessageBox.Show("Invalid income string in Schools item named " & sch.nameString & ". Item was skipped.")
      End If
    Next
    
    

    The For Each loop processes each element in the SchList array (calling the element "sch").  The incomeStream field is checked to make sure it can be converted to a decimal and if so the conversion is done and the decimal is added to Total.  If incomeStream is not valid, and MessageBox is shown and the item is ignored.  At the end of the loop, Total should contain the sum of all the valid incomeString fields in the array.

    Sunday, May 01, 2011 8:27 PM
  • Interesting, I see! But this code doesn't count as a Do Loop, does it...? If it does, then I guess that's why I wasn't getting it. I was thinking too literally; I figured if I needed a Do Loop then there would be Do and Loop involved.


    "My work is not a piece of writing designed to meet the taste of an immediate public but was done to last forever." - Thucydides, ancient Greek historian
    Sunday, May 01, 2011 8:38 PM
  • If you are required to use a Do loop rather than For or For Each loop, you should be able to modify the code I posted to do that.

    Create an integer variable (let's call it "i") to hold a number between 0 and 150 that will indicate which element of the array you are working in.  Within the loop, you can refer to fields in the structure like this

    SchList(i).incomeString

    Each time round the loop, add one to i.  End the loop when i is big enough to show you have processed all the elements.

    That should get you started, if you get stuck, come back and post the code you have.

    Sunday, May 01, 2011 8:55 PM
  • I see, I see. Hmm, crud, my brain must not be working correctly today... I came up with:

     

    Dim integerIndex As Integer = 0
    Dim booleanFound As Boolean = False
    Dim Total As Decimal
    
    Do Until booleanFound Or integerIndex > 150
        If SchList(integerIndex).incomeString += Total Then
         booleanFound = True
        Else
         integerIndex += 1
        End If
       Loop
    

     

    I definitely feel like I'm missing something, though. That doesn't seem quite right. The If Else feels necessary to properly cause the index to increase (and booleanFound is required, right?), but I don't feel like I have this set up correctly. Specifically the single statement after If feels off. Blah, I'm failing hard.
    "My work is not a piece of writing designed to meet the taste of an immediate public but was done to last forever." - Thucydides, ancient Greek historian
    Sunday, May 01, 2011 9:29 PM
  • I'm afraid I don't know what the If statement inside the loop is trying to do, but I don't think it will do what you want.

    You need to increment integerIndex every time round the loop, you should not skip that statement depending on a If statement.

    Inside the loop, you need to check that SchList(integerIndex).incomeString is a valid decimal and if so, convert it to a Decimal and add that value to Total.  I gave you the code for that in my earlier post.

    You should not need the booleanFound variable.

    Sunday, May 01, 2011 9:36 PM
  • The only way I've ever seen a way to increment an index integer is through If and Else (after Else). Perhaps it's because this is an intro class. To verify, Loop at the end is just referring to everything previous, isn't it? So if I wanted to loop integerIndex += 1 I would have to have it before Loop, right? So would this make more sense?

     

     

    Dim integerIndex As Integer = 0
    Dim Total As Decimal
    
    Do Until integerIndex > 150
     If Decimal.TryParse(sch.incomeString, income) Then
     Total += income
    integerIndex += 1
    Else MessageBox.Show("Invalid income string in Schools item named " & SchList.nameString & ". Item was skipped.") End If Loop

     

    Is it correct syntax to just have the integerIndex increment a line down from Total += income and before Else, or is that just way off? Sorry about how much trouble I'm being; I'm a beginner and just trying to figure everything out. Guess it doesn't help that I'm a very curious type, and I like to know exactly what's going on. This has been frustrating me to no end because I hate when I can't figure something out, and the whole rest of the assignment and project are complete. Thanks a lot for taking the time to work with me, Blackwood.


    "My work is not a piece of writing designed to meet the taste of an immediate public but was done to last forever." - Thucydides, ancient Greek historian
    Sunday, May 01, 2011 9:55 PM
  • Good. I think we're almost there.  Incrementing integerIndex needs to be done every time you go round the loop (if you don't increment it, then the next time round thge loop, you will be processing the same element you processed the last time), So take the integerIndex += 1 out of the If statement's Then clause and put it between End If and Loop. I think that should work for you.
    Sunday, May 01, 2011 10:10 PM
  • Ah, okay, okay. Eureka! So we're saying this should be it:

     

     

    Dim integerIndex As Integer = 0
    Dim Total As Decimal
    
    Do Until integerIndex > 150
     If Decimal.TryParse(SchList.incomeString, income) Then
     Total += income
     Else
     MessageBox.Show("Invalid income string in Schools item named" & sch.nameString & ". Item was skipped.")
     End If
     integerIndex += 1
    Loop
    

     

    That looks correct and good to go, then? And quick question: Why is sch.nameString in the Else message box area?


    "My work is not a piece of writing designed to meet the taste of an immediate public but was done to last forever." - Thucydides, ancient Greek historian
    Sunday, May 01, 2011 10:18 PM
  • That's just about it. In the TryParse statement, you are missing using integerIndex to indicate which element of the array to use. You need

    If Decimal.TryParse(SchList(integerIndex).incomeString, income) Then
    
    

    The code you have in the Else clause came from my original For Each loop and needs to be modifed to work in the Do loop

    MessageBox.Show("Invalid income string in Schools item named" & SchList(integerIndex).nameString & ". Item was skipped.")
    

    The reason for using nameString in the MessageBox is that the message is trying to help you find the item that has the error, and the name field seems like a good way of identifying the item.  Alternatively, you could just use integerIndex to identify the item like this

    MessageBox.Show("Invalid income string in Schools item number" & integerIndex.String() & ". Item was skipped.")
    

    Remember that the array elements are numbered beginning with 0, so item 2 will be the third item.

    Sunday, May 01, 2011 10:30 PM
  • Doh, of course. Got it! So here's the final code when using integerIndex as identifier:

    Dim integerIndex As Integer = 0
    Dim Total As Decimal
    
    Do Until integerIndex > 150
     If Decimal.TryParse(SchList(integerIndex).incomeString, income) Then
     Total += income
     Else
     MessageBox.Show("Invalid income string in Schools item number" & integerIndex.ToString() & ". Item was skipped.")
     End If
     integerIndex += 1
    Loop
    

     I'd say it's finally in a completed state, correct? Thanks a ton for all the help, Blackwood, it's beyond very much appreciated.


    "My work is not a piece of writing designed to meet the taste of an immediate public but was done to last forever." - Thucydides, ancient Greek historian



    • Marked as answer by Salacon Sunday, May 01, 2011 10:50 PM
    • Edited by Salacon Sunday, May 01, 2011 10:51 PM Editing code to be as it should look when finished, so I can Mark As Answer for others that happen to need it.
    Sunday, May 01, 2011 10:40 PM
  • Glad you made it. I'm afraid I see a small typo in the code I posted last.  In the MessageBox, I should have written integerIndex.ToString() instead of integerIndex.String(). Sorry about that.
    Sunday, May 01, 2011 10:43 PM
  • Ah, okay! I actually asked myself a question about that in my head when I saw it; I thought it was a bit odd to be a .String. Good deal, thank you very much! Again, very appreciated! Ah, to have that question answered... Very relieving, indeed.
    "My work is not a piece of writing designed to meet the taste of an immediate public but was done to last forever." - Thucydides, ancient Greek historian
    Sunday, May 01, 2011 10:47 PM