Jump to content

  • Log In with Google      Sign In   
  • Create Account


Looping Issues


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
12 replies to this topic

#1 Phil_93   Members   -  Reputation: 146

Like
0Likes
Like

Posted 24 August 2012 - 08:03 AM

Yet again I've ran into some more problems with the tutorials I'm doing. The program I have built is supposed to tell you how many g's are in the word debugging when you type it into the text box. However it keeps returning a value of 0. I know I need to add a loop to my program but I'm honestly not sure how, I've been stuck with this for the past hour or so and I really cant figure it out. Any help would be greatly appreciated.

private void button1_Click(object sender, EventArgs e)
{
int LetterCount = 0;
string strText = "Debugging";
string letter;
for (int i = 0; i < strText.Length; i++)
{
letter = strText.Substring(0, 1);

if (letter == "g")
{
LetterCount++;
}

}

textBox1.Text = "g appears " + LetterCount + " times";
}

Also I would like to ask, how long did it take you guys to get the hang of programming? I'm having to practically start over with all this. I barely passed my first year at university, I missed half of the year as I was in hospital and I've never been able to fully catch up with programming. I really want to be able to get the hang of it but at the moment it just seems like its never going to happen.

Sponsor:

#2 SiCrane   Moderators   -  Reputation: 9338

Like
3Likes
Like

Posted 24 August 2012 - 08:10 AM

Every time through the loop you set letter to be the first letter of the string. You never update the Substring() call for i.

#3 Phil_93   Members   -  Reputation: 146

Like
0Likes
Like

Posted 24 August 2012 - 08:18 AM

Thanks I think I understand, to update it could I add an else after my if statement?

#4 soylentrob   Members   -  Reputation: 120

Like
3Likes
Like

Posted 24 August 2012 - 09:00 AM

This statement letter = strText.Substring(0, 1); always returns "D" when strText equals "Debugging" because the first parameter is the starting position and you are always setting it to zero.

Instead of setting the first parameter to zero, set it to i so that it iterates through the string.

#5 Phil_93   Members   -  Reputation: 146

Like
1Likes
Like

Posted 24 August 2012 - 09:12 AM

Oh right now I see instead of it constantly being set to zero if I set it to i it puts it through the loop?, thank you very much for that. I tried literally everything but that. Thank you so much man :)

#6 ShadowValence   Members   -  Reputation: 369

Like
2Likes
Like

Posted 24 August 2012 - 01:33 PM

Oh right now I see instead of it constantly being set to zero if I set it to i it puts it through the loop?


The loop is already present. It's your 'for' command:
[source lang="csharp"]for (int i =0; i < strText.Length; i++){ //Everything inside of these brackets is performed until i = (strText.Length - 1)}[/source]

And because the Substring(x,y) command is inside of the curvy brackets, it is performed with each loop iteration. So it is already being 'put through the loop' (as you questioned). The function Substring(x,y) has two parameters. The first (labeled 'x' here) is the starting index and the second (labeled 'y') is the number of characters to read from 'x'. What's important to understand is that the computer doesn't have the ability to read like a human it keeps the string contents as an array of characters - . The word "Debugging" would look something like: {'D','e','b','u','g','g','i','n','g'}. And by using the substring command, you're asking the computer to return a new array of characters 'y' characters long starting from position 'x'. In your case, you were asking for a new 'string' of characters 1 character long starting from position 0.

By following @soylentrob's advice, you're now asking for a new string of characters 1 character long starting from position i. And i is incremeneted by one (i++) each time the for loop repeats.

Edited by ShadowValence, 30 August 2012 - 01:17 PM.


#7 Telastyn   Crossbones+   -  Reputation: 3712

Like
0Likes
Like

Posted 24 August 2012 - 01:53 PM

Also I would like to ask, how long did it take you guys to get the hang of programming?


Somewhere between 90 seconds and 12 years depending on your definition of 'hang of it'.

It didn't really take long (~ week) to avoid many of these looping bugs, but it took a while (~ 4-6 years) before I stopped occasionally being 'blind' to this sort of error without a debugger.

That's key though. This sort of error would take about 5 seconds to identify if you stepped through the code with a debugger. Are you unfamiliar with your debugger, or were just looking at the wrong spot or what?

#8 Phil_93   Members   -  Reputation: 146

Like
0Likes
Like

Posted 24 August 2012 - 05:49 PM

Are you unfamiliar with your debugger, or were just looking at the wrong spot or what?


Very unfamiliar, as I said in my topic post I missed a lot of time and work at university due to being in hospital for half of the year. Due to this I never got the hang of programming and only just passed my first year, I'm having to start from the beginning all over again because of the time I've missed and never caught up so honestly speaking I know very little about what I'm doing.

#9 Phil_93   Members   -  Reputation: 146

Like
0Likes
Like

Posted 24 August 2012 - 05:52 PM

I'd just like to say thank you to everyone for the help. It really means a lot.

#10 Telastyn   Crossbones+   -  Reputation: 3712

Like
1Likes
Like

Posted 24 August 2012 - 08:16 PM

Very unfamiliar, as I said in my topic post I missed a lot of time and work at university due to being in hospital for half of the year.


Yup, stuff happens. Now though, you know what sort of problems can be mitigated through the use of a debugger. Getting experience with it will provide immediate dividends.

#11 Phil_93   Members   -  Reputation: 146

Like
0Likes
Like

Posted 25 August 2012 - 08:43 AM

Yeah thanks, I'm slowly getting the hang of it all but when I get stuck I really do get stuck. Like you said at least now I know what problems I can identify using the debugger.

#12 SiCrane   Moderators   -  Reputation: 9338

Like
3Likes
Like

Posted 25 August 2012 - 09:41 AM

Note that there are other ways to rewrite the loop that might make it more obvious what's going on. First, you can avoid the somewhat confusing SubString() call in favor of an string index.
for (int i = 0; i < strText.Length; i++) {
  if (strText[i] == 'g') {
    LetterCount++;
  }
}
Also C# supports foreach loops that look at each element of a collection without needing to manually extract the elements via an index or whatever.
foreach (char letter in strText) {
  if (letter == 'g') {
    LetterCount++;
  }
}
Of course, there are a lot of other ways to write this, but many of them would only constitute showing off rather than efficient solutions.

#13 ShadowValence   Members   -  Reputation: 369

Like
0Likes
Like

Posted 30 August 2012 - 01:17 PM

... you can avoid the somewhat confusing SubString() call in favor of an string index.


I'm so glad you wrote this, SiCrane. I had considered adding it to my previous post but figured it'd be a bit too long. :D




Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS