I was getting segfaulted at the if statement because p had not been properly assigned. But the cout<< that I had put in as a diagnostic tool was not printing to the screen, which made me think that the error was further up. Finally I used gdb to find the exact location of the error, and saw that it occurred at the if statement. But why wouldn't the cout<< print to the screen if the error happened after the cout<< command?
As it turns out, this updated code does print the text before the segfault.
So adding the newlines allows the text to print out before I get segfaulted out of the program. What's going on? Is this some printscreen buffer flushing issue, or something else?