62175

How to use Escape Key to end a Loop in C

Question:

I just want to end a do while loop in C like this:

#include <stdio.h> #include <stdlib.h> main() { char exit; do{ printf("PLEASE INSERT OPTION:"); exit = getchar(); }while(exit != '\027'); }//main

I think it is kind of this way.

Answer1:

27 is the decimal ASCII value for Escape, but you use an octal character code. You should say 27, or '\033' (or '\x1b' for hex which is more common). This might be the problem, assuming your terminal lets through the Escape character. Sometimes they're used for terminal-level magic, and thus "eaten" by the terminal.

Also, please note that getchar(), despite its name, returns int and <strong>not</strong> char. It can also return the non-character value EOF (if you hit <kbd>Ctrl+D</kbd> (in Unix)) so a larger type is needed.

Answer2:

I've just found the problem, look this code:

#include <stdio.h> #include <stdlib.h> main() { int exit; do{ printf("PLEASE INSERT OPTION:"); exit = getch(); }while(exit != 27); }//main

That worked. Thanks for the help. @MichaelWalz @DrewMcGowen @2501 @unwind

Recommend

  • Importing a PKCS12 Using SecItemImport
  • how to calculate (A*B*C)%10000007 where A,B,C can be at maximum 10^18
  • determine if the bits are encrypted?
  • System V shared memory permission bits: meaning, and how to change
  • Appending a File with int 0x80 (Access bits?)
  • _tkinter.TclError: expected floating-point number but got “08” (looks like invalid octal number)
  • Echo 05000 ? PHP
  • How does “\\x” work in a String?
  • SWI Prolog escape quotes
  • How to convert string to integer in UNIX
  • Fortran runtime error: End of file
  • How to get C-x C-e to display the result in octal and hexadecimal by default?
  • How to load music in Android with libGDX?
  • Integer.decode(String s)
  • Postgres psql output strings without escape characters
  • Primitive int value conversion?
  • Why is the compiler showing error “expected expression before stud”?
  • inserting hex value into mysql
  • How do I write to registers in hardware using Python?
  • Matplotlib: Custom colormap with three colors [duplicate]
  • Insert Path of a file with \\\\ in mysql using java
  • pandas parse csv with left and right quote chars
  • preg_replace
  • How to remove last utf8 char of a python string
  • Send the string to its hex equivalent
  • remove unicode characters but keep all special and English characters with preg_replace
  • Access object instance inside an event handler
  • How to read piped content in C?
  • How to detect interior vertices in groups of 2d polygons? (E.g. ZIP Codes to determine a territory)
  • Convert Type Decimal to Hex (string) in .NET 3.5
  • Does Mobilefirst provide a provision to access web services directly?
  • Why Encoding.ASCII != ASCIIEncoding.Default in C#?
  • How do I open a C file with a relative path?
  • Yii2: Config params vs. const/define
  • Ajax Loaded meta Tags
  • Installing Hadoop, Java Exception about illegal characters at index 7?
  • Different response to non-authenticated users and AJAX calls
  • Deserializing XML into class C#
  • Buffer size for converting unsigned long to string
  • KeystoneJS: Relationships in Admin UI not updating