getchar() never exists?

Discuss Programming

getchar() never exists?

Postby ZiaTioN » Thu Jan 15, 2004 3:36 pm

I was wondering if any C programmers could tell me why a while loop I have looking for user input would never exit.

The while condition is the following:
Code: Select all
while ((var = getchar()) != EOF)


I am pretty positive this is the proper syntax.

The rest of the code is (I think) irrelevant to the loop statement. It is just tasks performed if this while condition is met. It should take all user input until the character return <Enter> key is pressed then perform the functions I have and exit. It does go through the functions to be performed but never exits. It waits for further user input and I eventually have to break (CTRL+C) it to stop the program.

Any ideas?

Edit:
Ok what I have done for now to acheive the result I want is:
Code: Select all
while ((var = getchar()) != '\n')


Is this the most efficeint way or the only way? It just does not seem right to me.
ZiaTioN
administrator
administrator
 
Posts: 460
Joined: Tue Apr 08, 2003 3:28 pm

Postby Void Main » Thu Jan 15, 2004 4:12 pm

If you want the program to go on when you press ENTER then the "\n" is what you want. EOF is generated by pressing "CTRL+d".
User avatar
Void Main
Site Admin
Site Admin
 
Posts: 5705
Joined: Wed Jan 08, 2003 5:24 am
Location: Tuxville, USA


Return to Programming

Who is online

Users browsing this forum: No registered users and 0 guests

cron