YNWA
YNWA

Reputation: 700

cant get EOF to exit a do while loop, with getchar()

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

Upvotes: 0

Views: 558

Answers (3)

Peter
Peter

Reputation: 1359

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.

Upvotes: 1

J_D
J_D

Reputation: 3586

Your condition is incorrect:

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

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

Upvotes: 1

Dheeraj Vepakomma
Dheeraj Vepakomma

Reputation: 28697

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

Upvotes: 5

Related Questions