Looping Issues

Started by
11 comments, last by ShadowValence 11 years, 7 months ago
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.
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.
Thanks I think I understand, to update it could I add an else after my if statement?
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.
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 :)

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.

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?

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.
I'd just like to say thank you to everyone for the help. It really means a lot.

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.

This topic is closed to new replies.

Advertisement