[英]scanf doesn't accept input
只有我的第一行输入请求用户键入该值。 输入B不要求用户键入,并显示错误的总数。
#include <stdio.h>
#include <stdlib.h>
//BASIC CALCULATION INPUT 2 INTEGER ONE BY ONE
int main(int argc, char *argv[])
{
int a,b,c;
//REQUEST ONE INPUT
printf("Integer A: \n");
scanf("%a",&a);
//REQUEST ONE INPUT
printf("Integer B: \n");
scanf("%b",&b);
c=a+b;
//DISPLAY AMOUNT INTEGER
printf("Total: &c",c);
system("PAUSE");
return 0;
}
您的两个scanf语句都是错误的! 应该是scanf(“%d”,&a); scanf(“%d”,&b); 要获取整数类型的用户输入a和b,请使用%d。 &a是持有a值的标识符a的引用(地址)。 并且用户输出的整数c的printf语句也应该是printf(“%d”,c);
在scanf
添加一个空格以在匹配整数之前放弃所有空格 。 比如一个
scanf(" %b",&b);
^^^
space in the scanf
您的两个scanf语句都使用了错误的格式说明符。 因此行为不确定 。
scanf("%a",&a);
和
scanf("%b",&b);
a
期望将float * as its argument, but you are passing
int * . There's no format specifier
标准C中也. There's no format specifier
b`。
使用%d
扫描int。
更好的是, 完全避免使用scanf
而使用fgets
并解析该行。
另一个问题是您的printf语句:
printf("Total: &c",c);
也是错的。 它应该是:
printf("Total: %c",c);
打印c
的值。
没有像%b这样的格式说明符,这就是Input B不要求用户键入的原因,我认为这会为您提供帮助。
#include <stdio.h>
#include <stdlib.h>
//BASIC CALCULATION INPUT 2 INTEGER ONE BY ONE
int main(int argc, char *argv[])
{
int a,b,c;
//REQUEST ONE INPUT
printf("Integer A: \n");
scanf("%d",&a);
//REQUEST ONE INPUT
printf("Integer B: \n");
scanf("%d",&b);
c=a+b;
//DISPLAY AMOUNT INTEGER
printf("Total: &c",c);
system("PAUSE");
return 0;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.