创建一个简单的代码来扫描两个数字,询问用户是否想要将它们相加或相乘,然后执行运算并打印输出。
#include <stdio.h>
int main(){
int num1;
int num2;
char oper[] = "";
printf("Enter a number: ");
scanf("%d", &num1);
printf("Enter another number: ");
scanf("%d", &num2);
printf("Would you like to add or multiply these numbers? ");
scanf("%s", &oper);
if(strcmp(oper, "multiply") == 0){
int prod = num1 * num2;
printf("The product is %d", prod);
}
else if(strcmp(oper, "add") == 0){
int sum = num1 + num2;
printf("The sum is %d", sum);
}
else{
printf("Why would you input something that you knew wouldn't work?");
}
return 0;
}
您正在使用 strcmp 函数而不声明它们。您需要包含包含 strcmp 函数声明的头文件。
Use:
#include <string.h>
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)