0

I have this code

whitespaces is a type int, so I can use the getchar function

do
{

 ...code...

whitespaces=getchar();}
while ( whitespaces != (EOF) || whitespaces!='\n');

but it doesnt exit the loop when i hit CTRL+Z (i am using windows 7)

what am I doing wrong?

EDIT : thank you, all of you...! very helpful

4

3 回答 3

5

You must use && instead of || in the while condition.

于 2012-04-17T19:51:23.147 回答
1

Your condition is incorrect:

while ( whitespaces != (EOF) && whitespaces!='\n');

A \n will automatically be different than EOF and vice-versa.

于 2012-04-17T19:51:54.197 回答
1

Try changing the || to &&. Right now, if whitespaces is equal to EOF, it's not a newline, so the while condition is always true. This is presumably not what you want.

于 2012-04-17T19:52:23.347 回答