0

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;
}
  • You probably only want to print the 'Your initials' message once, not once per character that the user enters. You don't even know that there's a letter after the space; you could end up printing a null character which is not a good idea, though it doesn't do all that much harm. The congratulatory `"!\n"` should also be outside the `for` loop. – Jonathan Leffler Sep 24 '16 at 03:26

1 Answers1

0

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++)
Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
Barmar
  • 741,623
  • 53
  • 500
  • 612