Search code examples
cif-statementcs50isspace

cs50 initials pset2 unexpected error


I am getting an error (4, to be precise) when I try to use "i < n" in my for loop. If I take it out, I get an infinite loop. I also can't seem to get the if statement to run. Any thoughts on what I can improve?

int main()
{
    int i;
    int n;
    //Program to get the user's name and reply with their capitalized initials
    {
    //Ask user for their name
    printf("What is your full name?\n");
    }
    //look for 1st character of each part of name given
    string name = GetString();

    for (i = 0; (n = strlen (name)); i < n; i++)
    {
        printf("Your intitals are %c", toupper(name[0]));
        {
            if (isspace(name[i]))
            {
            printf("%c", toupper(name[i+1]));
            }
        printf("!\n");
        }
    }
    return 0;
}

Solution

  • Your for() syntax is wrong. There can only be 2 ; characters in there. If you want to initialize multiple variables, separate them with ,, not ;.

    for (i = 0, (n = strlen (name)); i < n; i++)