I wrote a function to read an expression (i.e. A+B-C) from the keyboard one character at a time, and ignoring spaces insert each char into a queue. I tried something I though would be fairly elegant, but I don't think I did it right. This is my function:

void getExpression(QueueT Queue1)
{
     char ch;
     printf("\nEnter Expression:");
     while ( (ch=getchar()) != "\n")
     {
           if (ch != " ")
           {
                  insertQueue(Queue1, ch);
           }
      }
}

When I run it, the loop never exits. I've fed it all sorts of input trying to break the while condition, but no dice. I'm currently thinking that having (ch=getchar()) in my while condition isn't working like I think it is. Any tips would be much appreciated.

Also, this is my first post, so I apologize for any formatting errors.

Etherwind> I've fed it all sorts of input trying to break the while condition, but no dice.
The only char input that will stop the while loop is the newline or ENTER key, according to your code. But you wrote it incorrectly. Instead of "\n" it must be '\n' .
There's a difference. "\n" is '\n' followed by '\0' I suggest that you take a look at the if ( ch != " " ) as well.

Edited 6 Years Ago by Aia: n/a

So do the double quotes mean string literal while the single quotes get interpolated?

And that fixed it, danke.

So do the double quotes mean string literal while the single quotes get interpolated?

And that fixed it, danke.

Not quite.
Double quote is a string literal,
Single quote is a single character.

This article has been dead for over six months. Start a new discussion instead.