0

include

#include <cs50.h>
#include <string.h>

int main(void)
{
    //get user name through stdin
    printf("Please enter your name: ");
    int lastChar = 32;
    string a = get_string();


    for(int i = 0; i < strlen(a); i++)
    {
        if(a[i] != 32 && lastChar == 32) //check if char not space, and last char was a space
        {
            if(a[i] >= 97 && a[i] <= 122) //checks for lower-case letters, and converts
            {
                printf("%c", a[i] - 32);
            }
            else
            {
                printf("%c", a[i]);
            }
            lastChar = 0;
        }
        else if (a[i] == 32) //if char is a space change lastChar to space value
        {
            lastChar = 32;
        }
        else //if char is not a space, and last char was not a space, flip lastChar to 0
        {
            lastChar = 0;
        }

    }
    printf("\n");
    return 0;
}

link to check50: https://cs50.me/checks/d327c3669687360ed79d0f0a2fff67f4b960af51

1 Answer 1

1

Ah, the confusing specification strikes again. ;-)

In one place, the spec says to prompt for the name. Later it says to write the code so that it "looks like the examples", which don't actually "prompt" for the name. The latter is the correct instruction.

If you remove the printf statement, this main problem will be eliminated. Not saying there are or aren't other issues. ;-)

If this answers your question, please click on the check mark to accept. Let's keep up on forum maintenance. ;-)

4
  • @Ciff B Hi!, thank you for your help, but I'm still lost. Are you talking about the final printf statement just above the return 0; ? And if so, how would that fix it?
    – phindex
    Commented Oct 20, 2017 at 7:53
  • 'printf("Please enter your name: ");' should be removed. Another lesson: pay close attention to the errors reported by check50. If you look at all the failed tests, you would note that they all said that they were not expecting to see " Please enter your name..."
    – Cliff B
    Commented Oct 20, 2017 at 8:05
  • Ahh, that's a good point. Thank you!
    – phindex
    Commented Oct 20, 2017 at 8:41
  • With your help, check50 just passed me green-lit all the way down. Thanks again.
    – phindex
    Commented Oct 20, 2017 at 8:45

You must log in to answer this question.

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