0

My code has a bug in it which make it output the first initial after the second, would someone please be able to help me and describe what is wrong without having to change my whole code as it all works but this tiny little thing.

E.g if I enter David J Malan it outputs D JD M but if i enter David Malan it will output D M. Thank you :) P.s I have added in a line so it's easier for you to see where the error is.

int main(void)
{
    printf("Enter your name\n");
    string name = GetString();
    int i = 0;
    int n = strlen(name);
    char x = name[0];
    // here is possibly an error 
    printf("%c",toupper(x));

    for(i = 0; i < n; i++)
    {
        char y = name[i];
        if (y == ' ')
        {         
            y = toupper(name[i+1]);
            printf("%c", y);
        }
    }  
}
1
  • as a side note ` printf("Enter your name\n");` will not be accepted by cs50 check Software.
    – Wafeeq
    Jul 22 '16 at 12:23
0

I do not see such Problem as you mentioned. If you put printf("\n") after the for loop and delete the printf as i mentioned in my previous comment you program should work properly.

int main(void)
{
    //printf("Enter your name\n");
    string name = GetString();
    int i = 0;
    int n = strlen(name);
    char x = name[0];
    // here is possibly an error 
    printf("%c",toupper(x));

    for(i = 0; i < n; i++)
    {
       char y = name[i];
       if (y == ' ')
       {         
          y = toupper(name[i+1]);
          printf("%c", y);
       }
    }  
   printf("\n");
}
3
  • That fixed it! thank you Gallu :) Jul 22 '16 at 12:29
  • accept the question please if it worked.
    – Wafeeq
    Jul 22 '16 at 12:29
  • it is not actually a programming error this is because of cs50 check. but i dont know why it was not working for DAVID's Name. may be compile is annoyed of his Name :)
    – Wafeeq
    Jul 22 '16 at 12:31

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .