0

My initialize code glitches when i compile it. It asks me for my name and then it prints it out without initializing it. (its also worth pointing out that it capitilized the first letter of my first and last name and doesnt 'initialize' it)

#include <stdio.h>
#include <cs50.h>
#include <ctype.h>
#include <string.h>
#include <stdlib.h>


int main(int argc, string argv[])
{ 

  printf("Hello, what is your name?");
  string s = GetString();

for (int i = 0 ; s[i] != '\0' ; ++i)
{
    if (i == 0 || s[i-1] == ' ')
        printf("%c", toupper(s[i]));
    else
        printf("%c", s[i]);
}

}    
1
  • What is the input / output of your program? It looks fine to me.
    – ChrisG
    Jul 9, 2015 at 15:59

2 Answers 2

2

Your code is doing exactly what it is written to do, to print everything out while capitalizing the first letters.

But the assignment is to only print out the first letter of each word, and to capitalize them. You should go back and reread the specification, and alter your code to do what is wanted. Fortunately, the fix is very easy.

If this answers your question, please accept the answer to remove your question from the unanswered question pool. Let's keep up on forum maintenance. ;-)

1

You need some decision points to identify which letters to use as initials. Thankfully the specification says we just have letters and spaces, no funny characters to sort through. You'll need the toupper portion of your code, but need to restrict it to only those letters your to-be-written decision code identifies as the first letter of each name.

Writing that decision code isn't all that hard if you write out some name examples and look for what's a common indicator. Make certain then that you only use printf on those letters your decision code identifies as an initial.

You must log in to answer this question.

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