0

PGM compiles, but when I run it and type my name I'm getting a segmentation fault. Any help is greatly appreciated! Thanks!!

My code:

int main(void)
{

    // Prompts and retreves user input.
    printf("Enter Name: ");
    string name = GetString();        


    // If GetString returns NULL wont try iterating over
    // characters that arnt there.
    if (name != NULL)
    {        
        printf("%c", name[0]);                           

        for(int i = 0; i < strlen(name); i++)
        {
                if(isblank(name));
                {
                    i++;
                    printf("%c", name[i]);
                }
        }
        printf("\n");
    }   
}  
3

isblank(_), like isupper() and islower(), takes a single character as an argument. You are passing it an entire string and it's choking on it, causing a seg fault. isblank(name[i]) solves the issue nicely.

Also, I don't follow the reason for i++; inside the if loop. The for loop is incrementing i, so do you really want to increment it again inside the loop?

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

1
  • Thanks that worked, though it looks like I still have work to do. My though process was that it was going to print out the space so I wanted to increment to print out the character after the space... instead it prints every other letter. Back to the drawing board lol. – RicochetRiot Oct 6 '15 at 6:51

You must log in to answer this question.

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