Why doesn't pressing enter return '\n' to getch()?

前端 未结 9 1701
忘掉有多难
忘掉有多难 2020-12-06 07:43
#include 
#include 
main()
{
    char ch,name[20];
    int i=0;
    clrscr();
    printf(\"Enter a string:\");
    while((ch=getch())!=         


        
9条回答
  •  鱼传尺愫
    2020-12-06 08:33

    The keyboard return key is the carriage return, or CR. This is found by running the program and analyzing the results.

    When you press enter key, the ASCII value of Enter key is not returned, instead CR is returned.

    Hence (ch=getch())!='\n' should be changed to any of the following equivalent statements:

    ch=getch())!='\r' 
    ch=getch())!=13     // ASCII for CR: decimal
    ch=getch())!=0xd     // hexadecimal for CR
    ch=getch())!=0xD     // hexadecimal for CR
    ch=getch())!=015     // octal for CR
    

提交回复
热议问题