0

Need help with initials please :-). No one's invited me to the Slack yet (borker at mit dot edu) and I'm working through it. Got from 20 errors down to 1 that I can't figure out...

My approach is to get the name. We then know that the first character and any character after a space will be an initial. So I've created an initials string with 1 + the number of spaces. I then uppercase the first letter and run through the string character by character finding spaces, and adding the uppercase letter after the space to an array of characters called initials.

I then return this to the main function and print out the initials.

My code is below. What's happening is I get an error "initials.c:78:12: error: incompatible pointer types returning 'string [count_spaces]' from a function with result type 'string' (aka 'char *') [-Werror,-Wincompatible-pointer-types]"

I suspect this has to do with the what I am putting into the array and how that relates to the different data types (char, int etc) but I'm not sure how to fix it...

Any help? Code is below. Thanks!

string GetInitials(string fname)

{

// 2 variables: the length of the name and the number of spaces; we'll use spaces as a signifier of the initials; the letter after the space will be assumed to be an initial.

int len = strlen(fname);
int count_spaces = 1;

for (int a = 0; a < len; a++)
{
    if (fname[a] == ' ')
    {
        count_spaces++;
    }
}

// We know that the first letter will be an initial, every space afterwards signifies another initial; we loop through the name to count the spaces

string initials[count_spaces];

// We create an array now that we know the number of initials there are

initials[0] = toupper(char (fname[0]));
for (int i = 1, j = 1; i < len; i++)
{
    if (fname[i] == ' ')
        toupper(char (fname[i+1]));
       initials[j] = char (fname[i]);
       j++;
}
return initials;

}

So now I fixed initializing the array with characters, and get a new error: "initials.c:79:12: error: address of stack memory associated with local variable 'initials' returned [-Werror,-Wreturn-stack-address] return initials;"

Here is the latest function:

string GetInitials(string fname)

{

// 2 variables: the length of the name and the number of spaces; we'll use spaces as a signifier of the initials; the letter after the space will be assumed to be an initial.

int len = strlen(fname);
int count_spaces = 1;

for (int a = 0; a < len; a++)
{
    if (fname[a] == ' ')
    {
        count_spaces++;
    }
}

// We know that the first letter will be an initial, every space afterwards signifies another initial; we loop through the name to count the spaces

char initials[count_spaces];

// We create an array now that we know the number of initials there are
initials[0] = toupper(fname[0]);
for (int i = 1, j = 1; i < len; i++)
{
    if (fname[i] == ' ')
    {
        toupper(fname[i+1]);
       initials[j] = fname[i];
       j++;
    }
}
return initials;

}

Any help?

1

Lets do one step at a time. This line

string initials[count_spaces];

Is wrong. What your doing is not making a string of size count_spaces. A string is an array of chars so you need to make an array of chars like like this:

char initials[count_spaces];

What you're doing is making an array of strings, or an array of an array of chars. Fix that and we'll move forward.

| improve this answer | |
  • Thanks, will edit question, getting a different problem now – borker Jun 15 '16 at 20:59
  • Paste you're whole code with the main function also. – yousafe007 Jun 15 '16 at 21:16
  • I tested your code and the problem is with you declaring this: char initials[count_spaces]; But I encourage you to do this without a function and do them inside your main function. There are some reasons for that, some of which you'll come to understand after weak 3 and 4 when you learn about pointers. You're approach shows that you've understood the code logic for how to solve initials. Do the individual steps in the main function and don't use another variable for storing the initials which you're doing. Try printing out the individual characters one by one. – yousafe007 Jun 15 '16 at 21:39
  • Thanks, I went away frustrated for a bit. I'm not sure why this function call didn't work... the implementation in main is easier but seems less elegant. Thanks for the help in any case! – borker Jun 15 '16 at 23:25
  • Sure no problem. Yeah its not elegant but once you finish week 3 and 4, come back and try this with your earlier approach. You'll be able to do it!! Happy coding! – yousafe007 Jun 15 '16 at 23:27
0

Could be because you keep referring to your variable as "char (fname)". You only specify the data type to declare a variable not to reference it. Take out those "char"s and it might work.!

| improve this answer | |
  • I tried it and now getting a separate error: initials.c:70:27: error: expected expression when I am putting the characters into the array – borker Jun 15 '16 at 20:43

You must log in to answer this question.

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