只需创建一个简单的while循环来查找字母的ASCII值,然后创建一个循环,但是有一些东西会阻止它循环。初学者编码所以我有点迷失了!
#include <stdio.h>
main()
{
char Num, again;
again = 'A';
while (again == 'A')
{
printf("\nEnter letter");
scanf("%c", &Num);
printf("\nThe ASCII value of %c is %d\n", Num, Num);
printf("\n\nEnter A to look up another");
printf("\nor any other letter to quit");
scanf("%c", &again);
}
}
答案 0 :(得分:4)
简单地让scanf()
忽略'\n'
说明符捕获的"%c"
,就像这样
scanf(" %c", &again);
此外,您应该为其他scanf()
执行此操作,否则每个循环都会打印'\n'
字符。
答案 1 :(得分:0)
您需要在%c
之前添加空格才能使用\n
:
scanf(" %c", &again);
这是一种替代算法(使用非标准getch()
函数):
#include <stdio.h>
#include <conio.h>
int main()
{
int letter;
printf("Enter letters or <ESC> to stop:\n");
while((letter = getch()) != 27) {
printf("The ASCII value of %c is %d\n", letter, letter);
}
return 0;
}