0

I've been having this persistent issue that's taken me a day of googling and referencing and still have no idea what's up. Essentially, the code below (which is hopefully commented well enough to make it's intended purposes clear) refuses to compile, giving the error code I placed in the title of this question. "result of comparison against a string literal is unspecified". For my use of 'If (name[i] == " ")' At which point it recommends I use strcmp. (Which is for comparing strings. Not applicable)

The strangeness is that I have seen that exact code used by many other people on this site and elsewhere, and it works. I am quite stumped as to why it's not for me, which is why I've shared my entire program instead of just an excerpt, thinking the issue may be related to how I'm handling something else in the program.

(As an aside, I was reminded of the existence of "toupper" and such during my searching and will probably implement them once I figure this weird issue out.)

int main(void)
{
    ...
       // Set var pchars to 1 to indicate previous letter was space
    if (name[i] == (int)" ")
    {
        pchars = 1;
    }
    ...
}

[Edit: unrelated code removed for Honor Code compliance

4

It's usually the subtle things that give people the most trouble until they're understood.

if (name[i] == (int)" ")

You don't realize it, but you've made a simple error. You want to compare a char to a char, but you are actually telling the compiler to compare a single char to a string. The problem is that you are using double quotes. Double quotes indicate a string. Single quotes indicate a single character. Also, the casting of (int) isn't necessary. So, to fix it, try the following:

if (name[i] == ' ')

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

1
  • Ahhh thank you. It's not implicitly made clear in any of the CS50 lessons that the type of quotes used has any syntactical difference to my knowledge. (I may have missed it.) In any case, the program now properly compiles. Much obliged. – OddballGreg Sep 4 '15 at 18:05

You must log in to answer this question.

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