Jump to content
  • Advertisement
Sign in to follow this  
Phil_93

Looping Issues

This topic is 2294 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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.

Share this post


Link to post
Share on other sites
Advertisement
Every time through the loop you set letter to be the first letter of the string. You never update the Substring() call for i.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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 :)

Share this post


Link to post
Share on other sites

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

Share this post


Link to post
Share on other sites

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?

Share this post


Link to post
Share on other sites

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.

Share this post


Link to post
Share on other sites

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.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!