c - 如何在我的 C 程序中纠正这个错误?
问题描述
在这个程序中,我首先尝试编写我的简单计算器方程。然后,我会得到我的结果。当我编写这个程序时,我收到了两个警告:
- usertext 使用未初始化的内存
printf("%c", usertxt[i]);
scanf_s
: 没有足够的参数传递给格式字符串 wherescanf_s("%c", &myoperator);
#include<stdio.h>
#define SIZE 50
int i = 0;
int main() {
char usertxt[SIZE], myoperator;
printf("addition='+',subtraction='-',multiplication='*',division='/'\n");
int x,myarray[SIZE];
printf("How many numbers should be entered? ");
scanf_s("%d", &x);
for ( i = 0; i < x; i++) {
scanf_s("%c", &myoperator);
if (myoperator == '\0') {
break;
switch (myoperator) {
case '+':printf("Addition operation\n");
printf(" Enter your number: ");
scanf_s("%d", &myarray[i]);
usertxt[i] = printf("%d%c", myarray[i], myoperator);
break;
case '-':
printf("Subtraction operation\n");
printf("Enter your numbers: ");
scanf_s("%d", &myarray[i]);
usertxt[i] = printf("%d%c", myarray[i], myoperator);
break;
case '*':
printf("Multiplication operation\n");
printf("Enter your numbers: ");
scanf_s("%d", &myarray[i]);
usertxt[i] = printf("%d%c", myarray[i], myoperator);
break;
case '/':
printf("Division operation\n");
printf("Enter your numbers: ");
scanf_s("%d", &myarray[i]);
usertxt[i] = printf("%d%c", myarray[i], myoperator);
break;
};
}
}
for (int m = 0; m < i; m++) {
printf("%c", usertxt[i]);
}
解决方案
尝试使用 scanf 而不是 scanf_S
推荐阅读
- ios - 为视图控制器视图的边界原点设置动画时实际会发生什么?
- css - 如何将我的 React 汉堡菜单齐平放在网站的右上角?
- python - 如何在漂亮的汤标签列表中抓取内容?
- c - 有问题的scanf函数
- c++ - C++ 我怀疑这个函数的安全性
- javascript - 可以使用 JavaScript 设置 PsuedoClass 和通配符 CSS 吗?
- javascript - 此 javascript 片段中方括号的术语/概念是什么?
- vue.js - 如何根据计算属性显示排序图标?
- angular - 带有 Angular 的 html2canvas 无法按预期工作
- c# - 在画布元素上调用 onclick 时出现问题 (Unity3d)