Connecting Tech Pros Worldwide Forums | Help | Site Map

Issue with while loop

Newbie
 
Join Date: Sep 2007
Posts: 1
#1: Sep 25 '07
hey guys,

I'm new to programming so please bare with me. My problem is that how do i scan/read a blank space? My WHILE loop terminates by typing "exit" no probs there, but when I need to keep the loop running i have to press "ENTER" key to continue running. How can I scan/read "ENTER" key?

Here's my while loop...

while (strcmp(end, "exit")!=0)

any suggestions? is there something I have missed out?
Thanks
Expert
 
Join Date: Aug 2007
Posts: 674
#2: Sep 26 '07

re: Issue with while loop


Pressing the enter key is like typing "a" or "b" or "z" on the keyboard. It causes the operating system to insert a character, or series of characters, depending on the operating system, to insert a newline. As the name suggests, this newline character is interpreted as creating a newline. But is an actual character, and in C it is represented by '\n'.

Note that to type anything for the program, you need to type all the letters, numbers, spaces, whatever, and then press the enter key. So yes, in your programs, you always, always, always keep entering that newline character. Depending on your program, you may have been discarding that character or ignoring it, but it exists, and you need to account for its existence.

Before you do anything with whitespace processing, can I see more code? You should be doing something like taking input with fgets, and then processing the input as you see fit.
ashitpro's Avatar
Expert
 
Join Date: Aug 2007
Posts: 389
#3: Sep 26 '07

re: Issue with while loop


ascii value for ENTER is 10
you can read it by following code

scanf("%c",&d);
here d is of type char.

(int)d;

will give you the ascii value for ENTER
Expert
 
Join Date: Aug 2007
Posts: 674
#4: Sep 27 '07

re: Issue with while loop


So your idea of reading in a character is to read in an integer and then coerce it to a character? Wonder what that string and character format specifier in scanf is used for...
Reply