7 Replies - 418 Views - Last Post: 02 May 2014 - 02:46 PM Rate Topic: -----

#1 sterion66  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 8
  • Joined: 30-April 14

Beginner - Debug Fail

Posted 30 April 2014 - 02:19 PM

Hi guys,

I'm basically creating a little hangman project as I have self taught basic coding of VBA I actually want to follow my childhood dream of computer programming. So I'm getting started and creating a portfolio of work with the hope someone can take me on at some point. Basically the user picks a letter, thats assigned to a variable. It counts how many times that letter crops up, loops until all * has been replaced with said letter.

  int posn =0;
                int posk = 0;
                while (lettersFound < letterCount)
                {
                    posn = word.IndexOf(letter, posk, wordLen); //ERROR LINE//
                    Answer.Remove(0, posn);
                    Answer = Answer.Insert(posn, letter);
                    lettersFound = letterCount - 1;
                    posk = posn + 1;
                }


The problem arises here. It finds the first position of the letter, replaces it loops back as there is 1 more letter to find, increases posk by 1 so the starting index should be further along and therefore the posn should be greater. However I get this error instead.

"An unhandled exception of type 'System.ArgumentOutOfRangeException' occurred in mscorlib.dll

Additional information: Count must be positive and count must refer to a location within the string/array/collection."

I've used step through and everything does as it's meant to until this 2nd loop. posk is now at 5 and wordLen is 7 with 1 letter in between being the letter in question....

Is This A Good Question/Topic? 0
  • +

Replies To: Beginner - Debug Fail

#2 andrewsw  Icon User is online

  • Fire giant boob nipple gun!
  • member icon

Reputation: 3223
  • View blog
  • Posts: 10,819
  • Joined: 12-December 12

Re: Beginner - Debug Fail

Posted 30 April 2014 - 03:04 PM

Just looking at it logically:
while (lettersFound < letterCount)
{
    lettersFound = letterCount - 1;
    posk = posn + 1;

lettersFound is always less then letterCount (1 less) so the loop will never end. (In fact, lettersFound is always set to the same number as letterCount doesn't change.)

So posk will just increase, eventually exceeding the end of the word.

You need to think through the logic. Map out the steps with pencil and paper if it helps.

This post has been edited by andrewsw: 30 April 2014 - 03:07 PM

Was This Post Helpful? 0
  • +
  • -

#3 sterion66  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 8
  • Joined: 30-April 14

Re: Beginner - Debug Fail

Posted 02 May 2014 - 02:11 PM

After taking your advice I changed it to lettersFound++; with the same error occuring on the " posn = word.IndexOf(letter, posk, wordLen);" line

If it helps here are the variable answers after going through the loop once

letter "N"
letterCount 2
lettersFound 1
posk 2
posn 1
wordLen 7
word ENGLAND
Was This Post Helpful? 0
  • +
  • -

#4 Skydiver  Icon User is online

  • Code herder
  • member icon

Reputation: 3469
  • View blog
  • Posts: 10,701
  • Joined: 05-May 12

Re: Beginner - Debug Fail

Posted 02 May 2014 - 02:17 PM

At the time you get the error, what is the value of posk?
Was This Post Helpful? 0
  • +
  • -

#5 sterion66  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 8
  • Joined: 30-April 14

Re: Beginner - Debug Fail

Posted 02 May 2014 - 02:19 PM

It's 2 skydive. I have narrowed the problem down to posk as I set a new line up after where it goes through the exact same process without looping(after posk++) using posl instead of posn. So it must be something to do with posk?
Was This Post Helpful? 0
  • +
  • -

#6 Skydiver  Icon User is online

  • Code herder
  • member icon

Reputation: 3469
  • View blog
  • Posts: 10,701
  • Joined: 05-May 12

Re: Beginner - Debug Fail

Posted 02 May 2014 - 02:26 PM

*sigh*

From MSDN:

Quote

Exceptions
count or startIndex is negative.
-or-
startIndex is greater than the length of this string.
-or-
count is greater than the length of this string minus startIndex.

Was This Post Helpful? 0
  • +
  • -

#7 sterion66  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 8
  • Joined: 30-April 14

Re: Beginner - Debug Fail

Posted 02 May 2014 - 02:39 PM

Ok maybe I've misunderstood the usage of this method but isn't posk the startIndex and at 2 it's less than wordLen?
Was This Post Helpful? 0
  • +
  • -

#8 sterion66  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 8
  • Joined: 30-April 14

Re: Beginner - Debug Fail

Posted 02 May 2014 - 02:46 PM

Ok after a bit of research I get what you mean now. I have reduced wordLen by the posk and jobs a good one :) Now to work on some others bits(replace method etc..)

Thanks for your time guys!!!
Was This Post Helpful? 1
  • +
  • -

Page 1 of 1