为什么我用 C 编写的计算器不工作?
Why isn't the calculator I'm coding with C working?
我是 C 的新手,正在从事一个相当大的项目。我正在尝试制作一个计算器,到目前为止我只有加法。他们的工作方式是在加法语句中询问他们想要多少个数字,然后我使用 while() 循环来尝试获取所有信息。只是一些附带信息 - 我在 Windows 10 操作系统上使用 Cygwin 终端。
#include <stdio.h>
int main(){
char Etype;
printf("Hello! Welcome to your virtual calculator!\n");
printf("Press 'a' for addition!\n");
scanf("%c", &Etype);
if(Etype == 'a' || Etype == 'A') {
int Anum;
int x = 1;
int y;
printf("How many numbers do you want in this addition statement?\n");
scanf("%f", &Anum);
while(x < Anum) {
printf("Emter number %d\n", x);
scanf("%d", &y);
x = x + 1;
}
}
}
每当我在我的陈述中回答我想要的数字时,什么都没有发生。希望您能帮到您,如果可以,谢谢!
也许你只是忘了总结答案。
要输入整数,您应该使用 %d
而不是 %f
。
此外,x
应该用0
初始化。
您可以将代码编辑为:
#include <stdio.h>
int main(){
char Etype;
printf("Hello! Welcome to your virtual calculator!\n");
printf("Press 'a' for addition!\n");
scanf("%c", &Etype);
if(Etype == 'a' || Etype == 'A') {
int Anum;
int x = 0; // count from 0
int y;
int sum = 0; // to summary the answer
printf("How many numbers do you want in this addition statement?\n");
scanf("%d", &Anum); // use %d to input integer
while(x < Anum) {
printf("Emter number %d\n", x);
scanf("%d", &y);
sum += y; // summary
x = x + 1;
}
printf("Answer: %d\n", sum); // print the answer after calculation
}
}
或更清晰的版本:
#include <stdio.h>
int main(){
char eType[2];
printf("Hello! Welcome to your virtual calculator!\n");
printf("Press 'a' for addition!\n");
scanf("%s", eType);
if(eType[0] == 'a' || eType[0] == 'A')
{
int n, sum = 0;
printf("How many numbers do you want in this addition statement?\n");
scanf("%d", &n);
for (int i = 0; i < n; i++)
{
int x;
printf("Enter number %d\n", i);
scanf("%d", &x);
sum += x;
}
printf("Answer: %d\n", sum);
}
}
至少,您应该查看编译器警告,或者获得更好的编译器并查看编译器警告。
clang-7 -pthread -lm -o main main.c
main.c:16:29: warning: format specifies type 'float *' but the argument has type
'int *' [-Wformat]
scanf("%f", &Anum);
~~ ^~~~~
%d
1 warning generated.
我是 C 的新手,正在从事一个相当大的项目。我正在尝试制作一个计算器,到目前为止我只有加法。他们的工作方式是在加法语句中询问他们想要多少个数字,然后我使用 while() 循环来尝试获取所有信息。只是一些附带信息 - 我在 Windows 10 操作系统上使用 Cygwin 终端。
#include <stdio.h>
int main(){
char Etype;
printf("Hello! Welcome to your virtual calculator!\n");
printf("Press 'a' for addition!\n");
scanf("%c", &Etype);
if(Etype == 'a' || Etype == 'A') {
int Anum;
int x = 1;
int y;
printf("How many numbers do you want in this addition statement?\n");
scanf("%f", &Anum);
while(x < Anum) {
printf("Emter number %d\n", x);
scanf("%d", &y);
x = x + 1;
}
}
}
每当我在我的陈述中回答我想要的数字时,什么都没有发生。希望您能帮到您,如果可以,谢谢!
也许你只是忘了总结答案。
要输入整数,您应该使用 %d
而不是 %f
。
此外,x
应该用0
初始化。
您可以将代码编辑为:
#include <stdio.h>
int main(){
char Etype;
printf("Hello! Welcome to your virtual calculator!\n");
printf("Press 'a' for addition!\n");
scanf("%c", &Etype);
if(Etype == 'a' || Etype == 'A') {
int Anum;
int x = 0; // count from 0
int y;
int sum = 0; // to summary the answer
printf("How many numbers do you want in this addition statement?\n");
scanf("%d", &Anum); // use %d to input integer
while(x < Anum) {
printf("Emter number %d\n", x);
scanf("%d", &y);
sum += y; // summary
x = x + 1;
}
printf("Answer: %d\n", sum); // print the answer after calculation
}
}
或更清晰的版本:
#include <stdio.h>
int main(){
char eType[2];
printf("Hello! Welcome to your virtual calculator!\n");
printf("Press 'a' for addition!\n");
scanf("%s", eType);
if(eType[0] == 'a' || eType[0] == 'A')
{
int n, sum = 0;
printf("How many numbers do you want in this addition statement?\n");
scanf("%d", &n);
for (int i = 0; i < n; i++)
{
int x;
printf("Enter number %d\n", i);
scanf("%d", &x);
sum += x;
}
printf("Answer: %d\n", sum);
}
}
至少,您应该查看编译器警告,或者获得更好的编译器并查看编译器警告。
clang-7 -pthread -lm -o main main.c
main.c:16:29: warning: format specifies type 'float *' but the argument has type
'int *' [-Wformat]
scanf("%f", &Anum);
~~ ^~~~~
%d
1 warning generated.