我被这个程序卡住了,帮帮我,这将是值得赞赏的。但是,一起扫描字符可能会奏效。我想知道为什么scanf在第二次没有问角色。
#include <stdio.h>
int main()
{
printf("--Mathematical operations on character to get other character--n");
char a, b;
printf("Enter the 1st character: n");
scanf("%c", &a);
printf("Enter the 2nd character: n");
scanf("%c", &b);//It's not scanning 2nd time, instead moving forward to printf function
printf("nThe ASCII equivalent for addition of %c and %c is %cn", a, b, (a + b));
printf("nThe ASCII equivalent for subtraction of %c and %c is %cn", a, b, (a - b));
printf("nThe ASCII equivalent for multiplication of %c and %c is %cn", a, b, (a * b));
printf("nThe ASCII equivalent for division of %c and %c is %cn", a, b, (a / b));
printf("nThe ASCII equivalent for modular division of %c and %c is %cn", a, b, (a % b));
return 0;
}
在scanf("%c", &a);
之后,输入流中的下一个字符是用户按回车键或回车键输入的n
。然后scanf("%c", &b);
读取该字符。
要告诉scanf
跳过n
,请使用scanf(" %c", &b);
。空格告诉scanf
读取所有空白字符,直到看到非空白字符为止。