Issue with while loop

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Rhals
    New Member
    • Sep 2007
    • 1

    #1

    Issue with while loop

    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
  • oler1s
    Recognized Expert Contributor
    • Aug 2007
    • 671

    #2
    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.

    Comment

    • ashitpro
      Recognized Expert Contributor
      • Aug 2007
      • 542

      #3
      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

      Comment

      • oler1s
        Recognized Expert Contributor
        • Aug 2007
        • 671

        #4
        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...

        Comment

        Working...