C中的简单程序不起作用

时间:2016-04-09 17:18:26

标签: c interactive

我是C的新手,我正在制作教程。

我决定结合两个练习,我想接受输入 只要我不这样做<返回>。这是我的代码,直到现在:

#include<stdio.h>

char letter;
int i=0;

int main() {
  while (i==0) {
    printf(">> ");
    letter = getchar();
    printf("%d\n", letter);
    if (letter==10) {     //10 is the ASCII id for <return>
      printf("End\n");
      i = 1;
    }
    printf("You've entered %c\n", letter);
  }
}

问题在于,当我第一次输入信件时,它会按我要的方式执行操作:取一个数字,然后将其返回到屏幕上。在此之后,它自己设置10

这是输出:

>> r //I've entered 'r'
114
Das Zeichen ist r
>> 10 //I've entered nothing. It entered that on it's own
End
The letter is 

这里发生了什么?为什么这样做?我没有输入任何东西或没有任何东西。

3 个答案:

答案 0 :(得分:1)

当您输入return downloadAsync(url, name); 时,您也按Enter("r"等于ASCII 10)。最简单的方法是添加另一个'\n'以使用'\ n':

getchar()

另外还有另一种简短的方法,就是在分配字母值之后如下进行比较:

int main() {
  while (i==0) {
    printf(">> ");
    letter = getchar();
    getchar();
    printf("%d\n", letter);
    if (zeichen==10) {     //10 is the ASCII id for <return>
      printf("End\n");
      i = 1;
    }
    printf("You've entered %c\n", letter);
  }
}

然而,您仍然需要将int main() { while (i==0) { printf(">> "); letter = getchar(); if(letter == '\n') continue; printf("%d\n", letter); if (zeichen==10) { //10 is the ASCII id for <return> printf("End\n"); i = 1; } printf("You've entered %c\n", letter); } } 分配给某些内容,否则您将获得未定义的行为

答案 1 :(得分:0)

break

使用i=1;语句代替Dim connection As New OleDbConnection(connString)

答案 2 :(得分:0)

如果使用Windows,请使用“conio.h”中的_getch()

此外,不是检查10(换行符不是返回),而是检查\r,这是ENTER键返回的内容。当可以使用更具描述性的转义时,切勿使用文字值:

  • 换行= \n
  • 返回= \r