Sign in to follow this  
Phil_93

Looping Issues

Recommended Posts

Phil_93    146
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
SiCrane    11839
Every time through the loop you set [tt]letter[/tt] 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
soylentrob    120
This statement [b]letter = strText.Substring(0, 1);[/b] 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 [b]i [/b]so that it iterates through the string.

Share this post


Link to post
Share on other sites
Phil_93    146
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
ShadowValence    385
[quote name='Phil_93' timestamp='1345821149' post='4973007']
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?
[/quote]

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 [b]Substring(x,y) [/b]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 [b]Substring(x,y) [/b]has two parameters. The first (labeled 'x' here) is the starting index and the second (labeled 'y') is the number of characters to read [b][i]from 'x'[/i][/b][i]. [/i]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 [b]i[/b]. And [b]i[/b] is incremeneted by one (i++) each time the [b]for[/b] loop repeats. Edited by ShadowValence

Share this post


Link to post
Share on other sites
Telastyn    3777
[quote name='Phil_93' timestamp='1345816990' post='4972981']
Also I would like to ask, how long did it take you guys to get the hang of programming?
[/quote]

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
Phil_93    146
[quote name='Telastyn' timestamp='1345838016' post='4973091']
Are you unfamiliar with your debugger, or were just looking at the wrong spot or what?
[/quote]

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
Telastyn    3777
[quote name='Phil_93' timestamp='1345852186' post='4973148']
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.
[/quote]

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
Phil_93    146
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.

Share this post


Link to post
Share on other sites
SiCrane    11839
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.
[code]
for (int i = 0; i < strText.Length; i++) {
if (strText[i] == 'g') {
LetterCount++;
}
}
[/code]
Also C# supports [tt]foreach[/tt] loops that look at each element of a collection without needing to manually extract the elements via an index or whatever.
[code]
foreach (char letter in strText) {
if (letter == 'g') {
LetterCount++;
}
}
[/code]
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.

Share this post


Link to post
Share on other sites
ShadowValence    385
[quote name='SiCrane' timestamp='1345909286' post='4973261']
... you can avoid the somewhat confusing SubString() call in favor of an string index.
[/quote]

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

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this