Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

General Discussion

Why does my goto doesn't work? (C Programming)

#include <stdio.h>
#include <Windows.h>

int main()
{
    char string[100000];
    int words, numbers, others, i;

    Jason:

    words = numbers = others = i = 0;

    printf("Please insert Characters to be calculated!");
    gets(string);

    while (string[i] != '\0')


    {
        if ((string[i] >= 'a' && string[i] <= 'z') || (string[i] >= 'A' && string[i] <= 'Z'))
        {
            words++;
        }
        else if (string[i] >= '0' && string[i] <= '9')
        {
            numbers++;
        }
        else
        {
            others++;
        }

        i++;
    }

    while (string != ' ') goto Jason;
    {
        if (string == ' ') {
        printf("You have tpyed total of %d Words, %d Numbers, %d Blanks / Symbols.", words, numbers, others);
        }
    }


    Sleep(100000000);
    return 0;
}

Moderator edited: Markdown added to code so that it renders properly in the forums and topic switched to "General Discussion" as it is not directly related to C# or any course material. -JN:sparkles:

1 Answer

I'm not entirely sure what you're trying to do near the end, but the conditions you're checking for seem a bit backwards. In my changes below I assumed that you are wanting to go to your "Jason" label if the string is empty, and print the calculations if the string is not empty.

On a side note, your "words" variable may be more appropriately names "letters" since it seems to be calculating the number of letters in the string, and not full words.

#include <stdio.h>
#include <Windows.h>

int main()
{
    char string[100000];
    int letters, numbers, others, i;

    Jason:

    letters = numbers = others = i = 0;    

    printf("Please insert Characters to be calculated!");
    gets(string);

    while (string[i] != '\0')


    {
        if ((string[i] >= 'a' && string[i] <= 'z') || (string[i] >= 'A' && string[i] <= 'Z'))
        {
            letters++;
        }
        else if (string[i] >= '0' && string[i] <= '9')
        {
            numbers++;
        }
        else
        {
            others++;
        }

        i++;
    }

    if (string == ' ') 
    {    
      goto Jason;
    }
    if (string != ' ') {
      printf("You have tpyed total of %d Letters, %d Numbers, %d Blanks / Symbols.", letters, numbers, others);
    }


    Sleep(100000000);
    return 0;
}